notiz-dev / nestjs-prisma

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

@prisma/client did not initialize in Docker #49

Closed MeesEgberts closed 1 year ago

MeesEgberts commented 1 year ago

when I remove the PrismaService from the app.module.ts the error message changes to:

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

This is true because I inject a custom Prisma service into my services. The code that causes this is in the main.ts file because of the shutdown hook.

import { PrismaService } from 'nestjs-prisma';
...
const prismaService: PrismaService = app.get(PrismaService);
await prismaService.enableShutdownHooks(app);

This seems an essential part to me but whenever I remove these two lines the error is gone and it all seems to work! Yay! But I still need to figure out how to put this shutdown hook back without destroying my apps.

marcjulian commented 1 year ago

This is true because I inject a custom Prisma service into my services. The code that causes this is in the main.ts file because of the shutdown hook.

Hi @MeesEgberts are you using CustomPrismaModule and CustomPrismaService perhaps?

Here is an example how to enableShutdownHooks for CustomPrismaService

import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { CustomPrismaService } from 'nestjs-prisma';
import { extendedPrismaClient } from './prisma.extension';

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

  const customPrismaService: CustomPrismaService<extendedPrismaClient> =
    app.get('PrismaService');
  await customPrismaService.enableShutdownHooks(app);

  await app.listen(3000);
}
bootstrap();

Does this help you? I have update the extensions example too.

Might add that to the docs

MeesEgberts commented 1 year ago

@marcjulian I'm indeed using the ` but when I add your example to mymain.ts` I get the following error:

 Nest could not find PrismaService element (this provider does not exist in the current context)
marcjulian commented 1 year ago

How do you registered your CustomPrismaModule? Please provide an example, otherwise I cannot help.

The name property for the CustomPrismaService must be matching

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

@Module({
  imports: [
    CustomPrismaModule.forRootAsync({
      name: 'PrismaService', // 👈 must be the same name here
      useFactory: () => {
        return extendedPrismaClient;
      },
    }),
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}

// main.ts
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { CustomPrismaService } from 'nestjs-prisma';
import { extendedPrismaClient } from './prisma.extension';

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

  const customPrismaService: CustomPrismaService<extendedPrismaClient> =
    app.get('PrismaService'); // 👈 use the same name as in app.module.ts
  await customPrismaService.enableShutdownHooks(app);

  await app.listen(3000);
}
bootstrap();
MeesEgberts commented 1 year ago

Thanks! That solved my problem! I was using the wrong name for my service 😅

marcjulian commented 1 year ago

I will add this to the docs to clarify. CustomPrismaModule is new so a bit more docs around it is needed.