Skip to content

Commit 0f35009

Browse files
committed
fix: remove outdated section about shutdown hooks with prisma
This section has not been relevant since the introduction of the library engine as the default in Prisma 3. Now, in Prisma 5, the code example does not compile anymore since the `beforeExit` event was removed from Prisma Client (except when generating the client with the binary engine type, which most users should not do unless they have good reasons). Note that even in Prisma 2 or when switching to the binary engine, using `beforeExit` would've only been necessary to be able to run database queries in lifecycle hooks, and wouldn't be necessary otherwise. The justification for using `beforeExit` that was present in this section was not fully accurate because while the bug it refers to (prisma/prisma#3773) was fixed together with the introduction of `beforeExit` hook (which I believe led to the confusion), those were not strictly related changes.
1 parent 330ddd6 commit 0f35009

File tree

1 file changed

+2
-28
lines changed

1 file changed

+2
-28
lines changed

content/recipes/prisma.md

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -248,24 +248,18 @@ When setting up your NestJS application, you'll want to abstract away the Prisma
248248
Inside the `src` directory, create a new file called `prisma.service.ts` and add the following code to it:
249249

250250
```typescript
251-
import { INestApplication, Injectable, OnModuleInit } from '@nestjs/common';
251+
import { Injectable, OnModuleInit } from '@nestjs/common';
252252
import { PrismaClient } from '@prisma/client';
253253

254254
@Injectable()
255255
export class PrismaService extends PrismaClient implements OnModuleInit {
256256
async onModuleInit() {
257257
await this.$connect();
258258
}
259-
260-
async enableShutdownHooks(app: INestApplication) {
261-
this.$on('beforeExit', async () => {
262-
await app.close();
263-
});
264-
}
265259
}
266260
```
267261

268-
> info **Note** The `onModuleInit` is optional — if you leave it out, Prisma will connect lazily on its first call to the database. We don't bother with `onModuleDestroy`, since Prisma has its own shutdown hooks where it will destroy the connection. For more info on `enableShutdownHooks`, please see [Issues with `enableShutdownHooks`](recipes/prisma#issues-with-enableshutdownhooks)
262+
> info **Note** The `onModuleInit` is optional — if you leave it out, Prisma will connect lazily on its first call to the database.
269263
270264
Next, you can write services that you can use to make database calls for the `User` and `Post` models from your Prisma schema.
271265

@@ -518,26 +512,6 @@ This controller implements the following routes:
518512

519513
- `/post/:id`: Delete a post by its `id`
520514

521-
#### Issues with `enableShutdownHooks`
522-
523-
Prisma interferes with NestJS `enableShutdownHooks`. Prisma listens for shutdown signals and will call `process.exit()` before your application shutdown hooks fire. To deal with this, you would need to add a listener for Prisma `beforeExit` event.
524-
525-
```typescript
526-
// main.ts
527-
...
528-
import { PrismaService } from './services/prisma/prisma.service';
529-
...
530-
async function bootstrap() {
531-
...
532-
const prismaService = app.get(PrismaService);
533-
await prismaService.enableShutdownHooks(app)
534-
...
535-
}
536-
bootstrap()
537-
```
538-
539-
You can [read more](https://github.com/prisma/prisma/issues/2917#issuecomment-708340112) about Prisma handling of shutdown signal, and `beforeExit` event.
540-
541515
#### Summary
542516

543517
In this recipe, you learned how to use Prisma along with NestJS to implement a REST API. The controller that implements the routes of the API is calling a `PrismaService` which in turn uses Prisma Client to send queries to a database to fulfill the data needs of incoming requests.

0 commit comments

Comments
 (0)