notiz-dev / nestjs-prisma

Easy Prisma support for your NestJS application
https://nestjs-prisma.dev
MIT License
587 stars 50 forks source link

prisma.$on gives error #56

Closed jd4u closed 1 year ago

jd4u commented 1 year ago

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 1 year ago

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

Anyway, your guidance may help resolve the issue.

marcjulian commented 1 year ago

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

jd4u commented 1 year ago

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 1 year ago

Created the prisma case just now...

Closing this topic.

marcjulian commented 1 year ago

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();
  }
}
marcjulian commented 1 year ago

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?

https://github.com/notiz-dev/nestjs-prisma/blob/11ead056cae374e6870c735c6691ddf3b55a1398/examples/extensions/src/prisma.extension.ts#L3-L13

https://github.com/notiz-dev/nestjs-prisma/blob/11ead056cae374e6870c735c6691ddf3b55a1398/examples/extensions/src/main.ts#L13

nikelborm commented 5 months ago

Fixed in https://github.com/prisma/prisma/pull/24133