Typescript 使用Prisma 2和NestJS进行日志记录-依赖项注入问题?

Typescript 使用Prisma 2和NestJS进行日志记录-依赖项注入问题?,typescript,nestjs,prisma,prisma2,Typescript,Nestjs,Prisma,Prisma2,目前正在我的第一个NestJS项目中。我正在使用Prisma 2,并希望在调试模式下将查询记录到控制台,以了解和检查并避免n+1等 我已经创建了prisma.service.ts: import { Injectable, OnModuleInit, OnModuleDestroy } from '@nestjs/common' import { PrismaClient } from '@prisma/client' @Injectable() export class PrismaServ

目前正在我的第一个NestJS项目中。我正在使用Prisma 2,并希望在调试模式下将查询记录到控制台,以了解和检查并避免n+1等

我已经创建了
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()
    }
}
工作正常,我可以跨API使用它并访问数据库。但是,根据Prisma 2上的文档,我需要通过

import { PrismaClient } from '@prisma/client'

const prisma = new PrismaClient({
  log: [
    { level: 'warn', emit: 'event' },
    { level: 'info', emit: 'event' },
    { level: 'error', emit: 'event' },
  ],
然后像这样使用它:

@Injectable()
export class TestService {
    constructor(private prismaService: PrismaService) {
        this.prismaService.$on('query', e => {
            console.log("Query: " + e.query)
            console.log("Duration: " + e.duration + "ms")
        })
    }
遗憾的是,在编译时,我会遇到以下错误:

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")
t错误:⨯ 无法编译类型脚本:
src/test.service.ts:9:31-错误TS2345:类型为“query”的参数不能分配给类型为“beforexit”的参数。
9.prismaService.$on('query',e=>{
~~~~~~~
src/test.service.ts:10:39-错误TS2339:类型“()=>Promise”上不存在属性“query”。
10控制台日志(“查询:+e.Query”)
~~~~~
src/test.service.ts:11:42-错误TS2339:类型“()=>Promise”上不存在属性“duration”。
11控制台日志(“持续时间:+e.Duration+“ms”)
我尝试将
log
数组传递到服务中的
super()


我只是遗漏了一些小问题吗?

看起来Prisma客户端类型有问题。我建议您在Prisma Github回购上打开一个问题。在此之前,您需要通过强制转换到
any
unknown
来忽略提供的类型