P
Prisma•6mo ago
Ewen

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:
prisma : 6.11.0
@prisma/client : 6.11.0
Computed binaryTarget : rhel-openssl-3.0.x
Operating System : linux
Architecture : x64
Node.js : v20.19.1
TypeScript : 5.8.3
Query Engine (Node-API) : libquery-engine 9c30299f5a0ea26a96790e13f796dc6094db3173 (at node_modules/prisma/node_modules
/@prisma/engines/libquery_engine-rhel-openssl-3.0.x.so.node)
PSL : @prisma/prisma-schema-wasm 6.11.0-18.9c30299f5a0ea26a96790e13f796dc6094db3173
Schema Engine : schema-engine-cli 9c30299f5a0ea26a96790e13f796dc6094db3173 (at node_modules/prisma/node_modul
es/@prisma/engines/schema-engine-rhel-openssl-3.0.x)
Default Engines Hash : 9c30299f5a0ea26a96790e13f796dc6094db3173
Studio : 0.511.0
prisma : 6.11.0
@prisma/client : 6.11.0
Computed binaryTarget : rhel-openssl-3.0.x
Operating System : linux
Architecture : x64
Node.js : v20.19.1
TypeScript : 5.8.3
Query Engine (Node-API) : libquery-engine 9c30299f5a0ea26a96790e13f796dc6094db3173 (at node_modules/prisma/node_modules
/@prisma/engines/libquery_engine-rhel-openssl-3.0.x.so.node)
PSL : @prisma/prisma-schema-wasm 6.11.0-18.9c30299f5a0ea26a96790e13f796dc6094db3173
Schema Engine : schema-engine-cli 9c30299f5a0ea26a96790e13f796dc6094db3173 (at node_modules/prisma/node_modul
es/@prisma/engines/schema-engine-rhel-openssl-3.0.x)
Default Engines Hash : 9c30299f5a0ea26a96790e13f796dc6094db3173
Studio : 0.511.0
No description
3 Replies
Prisma AI Help
Prisma AI Help•6mo ago
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:
import { Injectable } from '@nestjs/common';
import { PrismaClient, Prisma } from '@prisma/client';

@Injectable()
export class PrismaService extends PrismaClient<Prisma.PrismaClientOptions, "query" | "info" | "warn" | "error"> {
constructor() {
super({
log: [
{ emit: 'event', level: 'query' },
{ emit: 'event', level: 'info' },
{ emit: 'event', level: 'warn' },
{ emit: 'event', level: 'error' },
],
});
this.$on('query', (e) => {
console.log('Query executed: ' + e.query);
console.log('Duration: ' + e.duration + 'ms');
});
}
}
import { Injectable } from '@nestjs/common';
import { PrismaClient, Prisma } from '@prisma/client';

@Injectable()
export class PrismaService extends PrismaClient<Prisma.PrismaClientOptions, "query" | "info" | "warn" | "error"> {
constructor() {
super({
log: [
{ emit: 'event', level: 'query' },
{ emit: 'event', level: 'info' },
{ emit: 'event', level: 'warn' },
{ emit: 'event', level: 'error' },
],
});
this.$on('query', (e) => {
console.log('Query executed: ' + e.query);
console.log('Duration: ' + e.duration + 'ms');
});
}
}
Alternatively, you can define your options as a constant and use typeof options as the generic for less redundancy:
const options = {
log: [
{ emit: 'event', level: 'query' } as const,
{ emit: 'event', level: 'info' } as const,
{ emit: 'event', level: 'warn' } as const,
{ emit: 'event', level: 'error' } as const,
],
};

@Injectable()
export class PrismaService extends PrismaClient<typeof options> {
constructor() {
super(options);
this.$on('query', (e) => {
console.log(e.query);
});
}
}
const options = {
log: [
{ emit: 'event', level: 'query' } as const,
{ emit: 'event', level: 'info' } as const,
{ emit: 'event', level: 'warn' } as const,
{ emit: 'event', level: 'error' } as const,
],
};

@Injectable()
export class PrismaService extends PrismaClient<typeof options> {
constructor() {
super(options);
this.$on('query', (e) => {
console.log(e.query);
});
}
}
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.
Nurul
Nurul•5mo ago
Hey! Was the bot answer helpful? Are you still observing this issue?
Ewen
EwenOP•5mo ago
Hey ! Yes the first answer was the correct one šŸ™‚ Thank you very much

Did you find this page helpful?