notiz-dev/nestjs-prisma

prisma.$on gives error

jd4u opened this issue ยท 7 comments

jd4u commented

After updating to latest version 4.14.0 of prisma, the following error is coming. nestjs-prisma is 0.20.0

Error: Argument of type '"error"' is not assignable to parameter of type '"beforeExit"'
The error is only while using nestjs-prisma (PrismaService or CustomPrismaService)

image

In the above screenshot, you can see all option tried. The prisma1 is created directly and works.

Do guide further...

jd4u commented

After removing nestjs-prisma from project and restructuring code, the error persists.

Anyway, your guidance may help resolve the issue.

Hi @jd4u is this still an issue? Is this issue reported in the prisma repo?

jd4u commented

not yet... the issue still persist in latest version of prisma without nestjs-prisma. I've communicated on slack, but for long not received any response.

Not yet reported on prisma.

jd4u commented

Created the prisma case just now...

Closing this topic.

Can you try the following to satisfy the PrismaClient types

Instance of PrismaClient with PrismaClientOptions types and log enabled.

// prisma.ts
import { Prisma, PrismaClient } from '@prisma/client';

export const prismaClient = 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' },
  ],
});

export type prismaClient = typeof prismaClient;

Use prismaClient to register it for CustomPrismaService:

import { Module } from '@nestjs/common';
import { CustomPrismaModule } from 'nestjs-prisma';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { prismaClient } from './prisma';

@Module({
  imports: [
    CustomPrismaModule.forRoot({
      name: 'PrismaService',
      client: prismaClient,
    }),
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}

Use the type of prismaClient for injecting CustomPrismaService

import { Inject, Injectable } from '@nestjs/common';
import { CustomPrismaService } from 'nestjs-prisma';
import { prismaClient } from './prisma';

@Injectable()
export class AppService {
  constructor(
    // โœ… use `prismaClient` from extension for correct type-safety
    @Inject('PrismaService')
    private prismaService: CustomPrismaService<prismaClient>,
  ) {

    prismaService.client.$on('error', async () => {});
  }

  users() {
    return this.prismaService.client.user.findMany();
  }
}

I have updated the Extension Example to include the PrismaClientOptions and log option. Hope this will help you. This would be a great addition to the docs as well. Would you like to write this and create a PR?

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({

customPrismaService.client.$on('error', (e) => {});