prisma-model-events-extension
v1.0.2
Published
A Prisma extension used to dispatch events before and/or after operations on models
Downloads
5
Readme
Prisma Model Events Extension
Disclaimer
This Prisma client extension is heavily inspired by the AvantaR/prisma-event-dispatcher. Since middlewares are deprecated since version 4.16.0., this replaces it with a query extension.
Installation
npm i prisma-model-events-extension
Usage
const client = new PrismaClient().$extends(
PrismaModelExtension.setup(config).getExtension(),
);
Configuration
To use the extension, you have to pass an array of configuration objects per model of the following form:
interface ModelEventsConfig {
model: string; // your model name
actions: PrismaAction[]; // an array of supported Prisma operations;
when: When[]; // 'before', 'after'
}
Supported Prisma operations
| 'findUnique'
| 'findMany'
| 'findFirst'
| 'create'
| 'createMany'
| 'update'
| 'updateMany'
| 'upsert'
| 'delete'
| 'deleteMany'
| 'executeRaw'
| 'queryRaw'
| 'aggregate'
| 'count'
Behaviour
Ths extension uses the eventemitter2 package and emits events named as such (all lowercase):
when.model.operation; // e.g. before.user.findmany or after.user.create
Feel free to listen to them elsewhere in your code.