nestjs-prisma icon indicating copy to clipboard operation
nestjs-prisma copied to clipboard

How do I enable logging with CustomPrismaModule?

Open cjmyles opened this issue 6 months ago • 5 comments

I am utilising the CustomPrismaModule in order to make use of client extensions. I have a file prisma.extension.ts that has the following code:

export const extendedPrismaClient = new PrismaClient<
  Prisma.PrismaClientOptions,
  'query' | 'info' | 'warn' | 'error'
>({
  log: [
    { level: 'query', emit: 'event' },
    { level: 'info', emit: 'event' },
    { level: 'warn', emit: 'event' },
    { level: 'error', emit: 'event' },
  ],
}).$extends({
  model: {
     ...
  },
});

export type ExtendedPrismaClient = typeof extendedPrismaClient;

This is then instantiated in my app.module.ts file:

imports: [
    CustomPrismaModule.forRootAsync({
      name: 'PrismaService',
      isGlobal: true,
      useFactory: () => {
        return extendedPrismaClient;
      },
    }),

According to the docs I need to add the following to my main.ts file:

async function bootstrap() {
  const app = await NestFactory.create(AppModule);

  // log query events
  const prismaService: PrismaService = app.get(PrismaService);
  prismaService.$on('query', (event) => {
    console.log(event);
  });

However, this results in the following error:

Nest could not find PrismaService element (this provider does not exist in the current context)

cjmyles avatar Aug 28 '24 07:08 cjmyles