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

How do I enable logging with CustomPrismaModule? #102

Open
cjmyles opened this issue Aug 28, 2024 · 5 comments
Open

How do I enable logging with CustomPrismaModule? #102

cjmyles opened this issue Aug 28, 2024 · 5 comments

Comments

@cjmyles
Copy link

cjmyles commented Aug 28, 2024

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)
@marcjulian
Copy link
Member

Hi @cjmyles, you have full control over your PrismaClient instance with the extension. You should be able to add $on to your new PrismaClient() before calling $extends(). There was, not sure if still is, a problem with $on that it returns void so that $extends() cant be called.

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' },
  ],
})
.$on(...) // should go in here
.$extends({
  model: {
     ...
  },
});

export type ExtendedPrismaClient = typeof extendedPrismaClient;

@marcjulian
Copy link
Member

marcjulian commented Aug 29, 2024

I might have to update the docs to explain a bit more the differences between PrismaService and CustomPrismaService.

Because you use CustomPrismaModule in your app.module.ts imports you can only access CustomPrismaService with your extended PrismaClient.

The PrismaService only has access to the default PrismaClient at the default generated location.

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

  // log query events
  const prismaService: PrismaService = app.get(PrismaService); // ❌ this won't work because PrismaSerivce is not provided, only CustomPrismaService. But `$on` is not a function for `CustomPrismaService` because you have full control of your PrismaClient instance
  prismaService.$on('query', (event) => {
    console.log(event);
  });

If you have any questions, please let me know.

@cjmyles
Copy link
Author

cjmyles commented Aug 29, 2024

@marcjulian Thanks for the responses. I'm with you now. Until the bug is fixed regarding $on() returning void, it looks like I can't use both logging and client functions then. I can see the FIXME comment in the prisma extension example but I can't find an issue in the issues list. Is this something we need to create?

@marcjulian
Copy link
Member

@cjmyles yes would be good to create a Issue for $on() returning void in the prisma repo

@cjmyles
Copy link
Author

cjmyles commented Sep 3, 2024

@marcjulian Done. See prisma/prisma#25153

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

4 participants
@cjmyles @marcjulian and others