Currently on my first NestJS project. I am using Prisma 2, and would like to log the queries to the console in debug mode, to learn and inspect and avoid n+1 etc!
I have created the prisma.service.ts
:
import { Injectable, OnModuleInit, OnModuleDestroy } from '@nestjs/common'
import { PrismaClient } from '@prisma/client'
@Injectable()
export class PrismaService extends PrismaClient implements OnModuleInit, OnModuleDestroy {
constructor() {
super();
}
async onModuleInit() {
await this.$connect()
}
async onModuleDestroy() {
await this.$disconnect()
}
}
Works fine, I can use it across the API and access the DB. However, according to the Prisma 2 Docs on Logging, I need to pass
import { PrismaClient } from '@prisma/client'
const prisma = new PrismaClient({
log: [
{ level: 'warn', emit: 'event' },
{ level: 'info', emit: 'event' },
{ level: 'error', emit: 'event' },
],
and then use it like this:
@Injectable()
export class TestService {
constructor(private prismaService: PrismaService) {
this.prismaService.$on('query', e => {
console.log("Query: " + e.query)
console.log("Duration: " + e.duration + "ms")
})
}
Sadly, when compiling, I get these errors:
TSError: тип Unable to compile TypeScript:
src/test.service.ts:9:31 - error TS2345: Argument of type '"query"' is not assignable to parameter of type '"beforeExit"'.
9 this.prismaService.$on('query', e => {
~~~~~~~
src/test.service.ts:10:39 - error TS2339: Property 'query' does not exist on type '() => Promise<void>'.
10 console.log("Query: " + e.query)
~~~~~
src/test.service.ts:11:42 - error TS2339: Property 'duration' does not exist on type '() => Promise<void>'.
11 console.log("Duration: " + e.duration + "ms")
I tried passing the log
array into the super()
in the service, without any luck.
Am I just missing something small?