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

this.$on("beforeExit") doesn't work anymore on 5.0.0 #20171

Closed
maxime4000 opened this issue Jul 11, 2023 · 6 comments
Closed

this.$on("beforeExit") doesn't work anymore on 5.0.0 #20171

maxime4000 opened this issue Jul 11, 2023 · 6 comments

Comments

@maxime4000
Copy link

Bug description

Using prisma v5 and having a prismaService, the new code doesn't work anymore.
https://docs.nestjs.com/recipes/prisma#use-prisma-client-in-your-nestjs-services

How to reproduce

https://docs.nestjs.com/recipes/prisma#use-prisma-client-in-your-nestjs-services

Expected behavior

beforeExit should be a typing option ? now it's never

Prisma information

// Add your schema.prisma
// Add your code using Prisma Client

V5 latest no schema really required

Environment & setup

  • OS: macos
  • Database: not important but is mongodb
  • Node.js version: 18.16.0

Prisma Version

v5.0.0
@maxime4000 maxime4000 added the kind/bug A reported bug. label Jul 11, 2023
@janpio
Copy link
Member

janpio commented Jul 11, 2023

Yes, this is a breaking change, see our release notes and upgrade guide:

Removal of the beforeExit hook from the library query engine

We removed the beforeExit hook from the default library Query Engine. We recommend using the built-in Node.js exit events.

-prisma.$on('beforeExit', () => { /* your code */ })

+process.on('beforeExit', () => { /* your code */ })

We recommend taking a look at the upgrade guide to learn how you can migrate to Prisma 5.

https://github.com/prisma/prisma/releases/tag/5.0.0

We'll make sure that NestJS guide is updated now.

@janpio janpio added 5.0.0 bug/0-unknown Bug is new, does not have information for reproduction or reproduction could not be confirmed. topic: NestJS topic: beforeExit labels Jul 11, 2023
@janpio janpio changed the title this.$on("beforeExit" doesn't work anymore on 5.0.0 this.$on("beforeExit") doesn't work anymore on 5.0.0 Jul 11, 2023
@janpio
Copy link
Member

janpio commented Jul 11, 2023

I'll keep this issue open until we have actually updated that guide - this way it is easier for other people to find.

Thanks for reporting so quickly by the way!

@janpio janpio reopened this Jul 11, 2023
@millsp millsp added team/client Issue for team Client. kind/docs and removed bug/0-unknown Bug is new, does not have information for reproduction or reproduction could not be confirmed. kind/bug A reported bug. labels Jul 12, 2023
@divyansh7924
Copy link

divyansh7924 commented Jul 12, 2023

@maxime4000
This works, replace prismaService.enableShutdownHooks(app); with app.enableShutdownHooks() and remove function enableShutdownHooks from PrismaService.

Final implementation would look something like this, https://docs.nestjs.com/fundamentals/lifecycle-events#application-shutdown

@Jolg42 Jolg42 added this to the 5.1.0 milestone Jul 12, 2023
@aqrln
Copy link
Member

aqrln commented Jul 12, 2023

Hi @maxime4000 and anyone else who faces the same issue! As already correctly pointed out above, you should use the normal shutdown hooks and lifecycle events in NestJS as you would in any other NestJS application not using Prisma. The Prisma guide in the NestJS documentation and our guides and examples are unfortunately a little bit outdated in this regard: this kind of special integration for shutdown hooks with Prisma hasn't been required for a while already since the introduction of the library engine, but it continued to work as we kept the beforeExit event for backwards compatibility until Prisma 5. Sorry for this omission, we will make sure to update everything as soon as possible!

To migrate to the standard NestJS shutdown hooks, you need to do the following steps:

  1. Replace the invocation of the custom prismaService.enableShutdownHooks(app) method in the application entry point with the built-in app.enableShutdownHooks().
  2. (optional, only if applicable) If any custom logic except app.close() was added to the beforeExit event handler, it must be moved to onModuleDestroy, beforeAppicationShutdown or onApplicationShutdown lifecycle hooks. The difference between them is described in NestJS documentation. onModuleDestroy would be the closest equivalent in terms of control flow, but other methods may be more appropriate — use your best judgement.
  3. Remove the custom enableShutdownHooks method completely from your PrismaService class as it’s not needed anymore.

@DevVictorr
Copy link

I read it in the docs and that way it worked for me

async enableShutdownHooks(app: INestApplication) {
    process.on('beforeExit', () => {
      app.close();
    });
  }

@aqrln
Copy link
Member

aqrln commented Jul 13, 2023

Docs and examples have been updated on both NestJS and our side, I'll go ahead and close this issue.

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

No branches or pull requests

7 participants