Gathering detailed insights and metrics for prisma-client-lib-hooks
Gathering detailed insights and metrics for prisma-client-lib-hooks
Gathering detailed insights and metrics for prisma-client-lib-hooks
Gathering detailed insights and metrics for prisma-client-lib-hooks
npm install prisma-client-lib-hooks
Typescript
Module System
Node Version
NPM Version
58.6
Supply Chain
93
Quality
70.4
Maintenance
50
Vulnerability
98.2
License
Total Downloads
2,135
Last Day
2
Last Week
2
Last Month
14
Last Year
129
Minified
Minified + Gzipped
Latest Version
0.0.6
Package Id
prisma-client-lib-hooks@0.0.6
Unpacked Size
50.72 kB
Size
9.44 kB
File Count
21
NPM Version
6.9.0
Node Version
10.16.0
Cumulative downloads
Total Downloads
Last Day
0%
2
Compared to previous day
Last Week
-66.7%
2
Compared to previous week
Last Month
-17.6%
14
Compared to previous month
Last Year
10.3%
129
Compared to previous year
1
3
This package is to add hook functionality to prisma-client-lib package.
IMPORTANT: Prisma Photon will have these capabilities built in, but until it's a full release I ended up creating this for myself.
I built this package mainly for synchronization. I prefer to keep any validations, permission or query variable changes specific to a graphql resolver. This allows me to see the uni-direction of the data. The main purpose was if a model is used throughout the app and the CUD operation might come from multiple places. Primary use case is synchronization with other datasources.
1$ npm install prisma-client-lib prisma-client-lib-hooks
Context that gets passed has the model name, and the parsed ending of the crud operation so that you can concatenate it with another method if needed
1export interface BaseCtx<T extends string> { 2 modelName: T; 3 modelNameFnEnding: string; 4}
The create method didn't have a before because I cannot get the id prior to the operation.
Take a look at ./examples/src/hooks
Example of hook:
1// hook.ts 2import { prisma } from "../generated/prisma-client"; 3import { Hook, BaseCtx } from "../../../"; 4 5interface Ctx extends BaseCtx<string> { 6 addToContext: boolean; 7} 8 9export class ExampleHook extends Hook { 10 models: { [k: string]: boolean } = { 11 Post: true, 12 }; 13 createAfter = async (ids: string[], ctx: Ctx) => { 14 // do something here 15 }; 16 deleteBefore = async (ids: string[], ctx: Ctx) => { 17 // do something here 18 ctx.addToContext = true; 19 }; 20 21 deleteAfter = async (ids: string[], ctx: Ctx) => { 22 // do something here 23 console.log(ctx.addToContext); 24 // result: true; 25 // ctx state is passed per hook, per CUD 26 }; 27 updateAfter = async (ids: string[], ctx: Ctx) => { 28 /* 29 you should use the original initiated prisma that 30 was generated for doing CRUD actions inside of hooks, 31 this will prevent circular function calls and keep 32 it unidirectional 33 */ 34 if (ctx.modelName === 'User') { 35 await prisma.deleteManyPosts({}) 36 } 37 }; 38 updateBefore = async (ids: string[], ctx: Ctx) => { 39 // do something here 40 }; 41} 42}
Example of initiating prisma with hooks:
1// index.ts 2import { ExampleHook } from './hook' 3import { Prisma, models, ClientConstructor } from "../generated/prisma-client/index"; 4import { typeDefs } from "../generated/prisma-client/prisma-schema"; 5 6import { makePrismaClientClass } from "../../../"; 7 8 9export const HookedPrisma = makePrismaClientClass<ClientConstructor<Prisma>>({ 10 typeDefs, 11 models, 12 endpoint: `${process.env["PRISMA_ENDPOINT"]}`, 13 hooks: [new ExampleHook()] 14}); 15export const hookedPrisma = new HookedPrisma(); 16
No vulnerabilities found.
No security vulnerabilities found.