2

目前在我的第一个 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 Docs on Logging,我需要通过

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")

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

我只是缺少一些小东西吗?

4

4 回答 4

7

嘿兄弟,我一直在使用 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();
        });
      }
    }
@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');
    });
  }
}

Ps:我不得不删除dist文件夹并再次运行。

于 2021-07-13T03:44:58.943 回答
2

我使用了这个基于 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');
    });
  }
}
于 2021-11-16T13:53:13.090 回答
2

看起来像是 Prisma Client 类型的问题。我建议你在 Prisma Github repo 上打开一个问题。在此之前,您需要通过转换为any或忽略提供的类型unknown

于 2021-05-12T20:34:07.433 回答
0

您可以在PrismaClient泛型中指定所需的事件。

@Injectable()
export class PrismaService extends PrismaClient<Prisma.PrismaClientOptions, 'query' | 'error'> implements OnModuleInit {
  private readonly logger = new Logger(PrismaService.name);

  constructor() {
    super({
      log: [
        {
          emit: 'event',
          level: 'query',
        },
        {
          emit: 'event',
          level: 'error',
        },
        {
          emit: 'stdout',
          level: 'info',
        },
        {
          emit: 'stdout',
          level: 'warn',
        },
      ],
    });
  }

  async onModuleInit() {
    this.$on('error', (event) => {
      this.logger.verbose(event.target);
    });
    await this.$connect();
  }

  async enableShutdownHooks(app: INestApplication) {
    this.$on('beforeExit', async () => {
      await app.close();
    });
  }
}
于 2022-01-22T21:04:10.023 回答