prisma.$on not working with nestjs implementation
Hello,
I am trying to make a small logger in my nest application for all my prisma queries.
But when I try to implement the on event method of logging the queries typescript does not pet me (cf image)
What can I do ? Is it a problem on my part ?
Thank you very much š
info:

3 Replies
You've gone full bot mode! Let me fetch that AI answer for you ASAP.
This is a common TypeScript issue when using Prisma with NestJS: the $on method for logging (e.g., this.$on('query', ...)) gives a type error like Argument of type '"query"' is not assignable to parameter of type '"beforeExit"'.
Why does this happen?
When you extend PrismaClient in a NestJS service, TypeScript cannot infer the correct event types for $on unless you explicitly provide the generics to PrismaClient. By default, only 'beforeExit' is allowed unless you specify logging options and the corresponding generic. How to fix:
You need to pass the correct generics to PrismaClient when extending it. For query logging, do this: Alternatively, you can define your options as a constant and use typeof options as the generic for less redundancy: This approach is confirmed to work and is a TypeScript limitation, not a Prisma bug. For more details, see the official answer and user confirmations in the linked discus sions and issues here and here. š¤ Alright, thanks for letting us know that there's still some help needed. A team member will reply to you shortly. In the meantime, please let us know any additional info, questions, or which parts of the answer may need clarification.
When you extend PrismaClient in a NestJS service, TypeScript cannot infer the correct event types for $on unless you explicitly provide the generics to PrismaClient. By default, only 'beforeExit' is allowed unless you specify logging options and the corresponding generic. How to fix:
You need to pass the correct generics to PrismaClient when extending it. For query logging, do this: Alternatively, you can define your options as a constant and use typeof options as the generic for less redundancy: This approach is confirmed to work and is a TypeScript limitation, not a Prisma bug. For more details, see the official answer and user confirmations in the linked discus sions and issues here and here. š¤ Alright, thanks for letting us know that there's still some help needed. A team member will reply to you shortly. In the meantime, please let us know any additional info, questions, or which parts of the answer may need clarification.
Hey! Was the bot answer helpful? Are you still observing this issue?
Hey !
Yes the first answer was the correct one š
Thank you very much