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

tur*_*ing 7 typescript nestjs prisma prisma2

目前正在进行我的第一个 NestJS 项目。我正在使用 Prisma 2,并且想在调试模式下将查询记录到控制台,以学习和检查并避免 n+1 等!

\n

我已经创建了prisma.service.ts

\n
import { Injectable, OnModuleInit, OnModuleDestroy } from \'@nestjs/common\'\nimport { PrismaClient } from \'@prisma/client\'\n\n@Injectable()\nexport class PrismaService extends PrismaClient implements OnModuleInit, OnModuleDestroy {\n    constructor() {\n        super();\n    }\n\n    async onModuleInit() {\n        await this.$connect()\n    }\n\n    async onModuleDestroy() {\n        await this.$disconnect()\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n

工作正常,我可以通过 API 使用它并访问数据库。但是,根据 Prisma 2 Docs on Logging,我需要通过

\n
import { PrismaClient } from \'@prisma/client\'\n\nconst prisma = new PrismaClient({\n  log: [\n    { level: \'warn\', emit: \'event\' },\n    { level: \'info\', emit: \'event\' },\n    { level: \'error\', emit: \'event\' },\n  ],\n
Run Code Online (Sandbox Code Playgroud)\n

然后像这样使用它:

\n
@Injectable()\nexport class TestService {\n    constructor(private prismaService: PrismaService) {\n        this.prismaService.$on(\'query\', e => {\n            console.log("Query: " + e.query)\n            console.log("Duration: " + e.duration + "ms")\n        })\n    }\n
Run Code Online (Sandbox Code Playgroud)\n

可悲的是,在编译时,我收到以下错误:

\n
TSError: \xe2\xa8\xaf Unable to compile TypeScript:\nsrc/test.service.ts:9:31 - error TS2345: Argument of type \'"query"\' is not assignable to parameter of type \'"beforeExit"\'.\n\n9        this.prismaService.$on(\'query\', e => {\n                                ~~~~~~~\nsrc/test.service.ts:10:39 - error TS2339: Property \'query\' does not exist on type \'() => Promise<void>\'.\n\n10             console.log("Query: " + e.query)\n                                         ~~~~~\nsrc/test.service.ts:11:42 - error TS2339: Property \'duration\' does not exist on type \'() => Promise<void>\'.\n\n11             console.log("Duration: " + e.duration + "ms")\n
Run Code Online (Sandbox Code Playgroud)\n

我尝试将log数组传递到super()服务中,但没有任何运气。

\n

我只是错过了一些小事吗?

\n

Kel*_* ts 17

嘿兄弟,我一直在使用 prima 2 + Nestjs,我将配置 prima 放在父级的 super 中,如下所示。它对我来说创造了奇迹。

我希望它对你有帮助;)

这是我的 prismaService.ts

prisma.service.ts

import { INestApplication, Injectable, OnModuleInit } from '@nestjs/common';
import { PrismaClient } from '@prisma/client';
    
    @Injectable()
    export class PrismaService extends PrismaClient implements OnModuleInit {
      constructor() {
        super({
          log: [
            { emit: 'event', level: 'query' },
            { emit: 'stdout', level: 'info' },
            { emit: 'stdout', level: 'warn' },
            { emit: 'stdout', level: 'error' },
          ],
          errorFormat: 'colorless',
        });
      }
      async onModuleInit() {
        await this.$connect();
      }
    
      async enableShutdownHooks(app: INestApplication) {
        this.$on('beforeExit', async (event) => {
          console.log(event.name);
          await app.close();
        });
      }
    }
Run Code Online (Sandbox Code Playgroud)
@Injectable()
export class TestService {
  constructor(private prismaService: PrismaService) {
    prismaService.$on<any>('query', (event: Prisma.QueryEvent) => {
      console.log('Query: ' + event.query);
      console.log('Duration: ' + event.duration + 'ms');
    });
  }
}

Run Code Online (Sandbox Code Playgroud)

Ps:我必须删除 dist 文件夹并再次运行。


小智 5

我使用了这个基于 Prisma GitHub 问题的解决方案。

@Injectable()
export class TestService {
  constructor(
    private prismaService: PrismaClient<Prisma.PrismaClientOptions, 'query'>,
  ) {
    this.prismaService.$on('query', (e) => {
      console.log('Query: ' + e.query);
      console.log('Duration: ' + e.duration + 'ms');
    });
  }
}
Run Code Online (Sandbox Code Playgroud)


Jes*_*ter 2

看起来像是 Prisma 客户端类型的问题。我建议您在 Prisma Github 存储库上提出问题。在那之前,您需要通过强制转换为any或来忽略提供的类型unknown