typescriptnestjsprisma

Argument of type 'string' is not assignable to parameter of type 'never' on prisma.service


import { INestApplication, Injectable, OnModuleInit } from "@nestjs/common";
import { PrismaClient } from "@prisma/client";
@Injectable()
export class PrismaService extends PrismaClient implements OnModuleInit {
  async onModuleInit() {
    await this.$connect();
    console.log("postgresql connected");
  }

  async enableShutdownHooks(app: INestApplication) {
    this.$on("beforeExit", async () => {
      await app.close();
    });
  }
}

The error is here this.$on("beforeExit", async () => {

I tried various solutions to resolve the issue, to run npx prisma generate, or update the package but any of this didn't work.

Any ideeas?


Solution

  • The beforeExit hook has been removed from the library query engine, read more here. They have an upgrade guide that may be helpful. This is the Prisma ORM 4 code they give as an example:

    const exitHandler = () => {
      // your exit handler code
    }
    
    prisma.$on('beforeExit', exitHandler)
    

    which would now not work because of this breaking change. They replaced it with

    const exitHandler = () => {
      // your exit handler code
    }
    
    process.on('exit', exitHandler)
    process.on('beforeExit', exitHandler)
    process.on('SIGINT', exitHandler)
    process.on('SIGTERM', exitHandler)
    process.on('SIGUSR2', exitHandler)
    

    They also recommend to remove the custom enableShutdownHooks methods in your service.