Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

prisma.$on gives error #56

Closed
jd4u opened this issue May 12, 2023 · 7 comments
Closed

prisma.$on gives error #56

jd4u opened this issue May 12, 2023 · 7 comments

Comments

@jd4u
Copy link

jd4u commented May 12, 2023

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
Copy link
Author

jd4u commented May 12, 2023

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

Anyway, your guidance may help resolve the issue.

@marcjulian
Copy link
Member

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

@jd4u
Copy link
Author

jd4u commented May 26, 2023

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
Copy link
Author

jd4u commented May 26, 2023

Created the prisma case just now...

Closing this topic.

@jd4u jd4u closed this as completed May 26, 2023
@marcjulian
Copy link
Member

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
Copy link
Member

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) => {});

@nikelborm
Copy link

Fixed in prisma/prisma#24133

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants