Skip to content

Commit 5d75a91

Browse files
Merge pull request #1921 from meeech/prisma-recipe-fix
Prisma recipe fix
2 parents 52cbdcd + 1131507 commit 5d75a91

File tree

1 file changed

+28
-4
lines changed

1 file changed

+28
-4
lines changed

content/recipes/prisma.md

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

249249
```typescript
250-
import { Injectable, OnModuleInit, OnModuleDestroy } from '@nestjs/common';
250+
import { INestApplication, Injectable, OnModuleInit, OnModuleDestroy } from '@nestjs/common';
251251
import { PrismaClient } from '@prisma/client';
252252

253253
@Injectable()
254254
export class PrismaService extends PrismaClient
255-
implements OnModuleInit, OnModuleDestroy {
255+
implements OnModuleInit {
256+
256257
async onModuleInit() {
257258
await this.$connect();
258259
}
259260

260-
async onModuleDestroy() {
261-
await this.$disconnect();
261+
async enableShutdownHooks(app: INestApplication) {
262+
this.$on('beforeExit', async () => {
263+
await app.close();
264+
});
262265
}
263266
}
264267
```
265268

269+
> 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`](#issues-with-enableShutdownHooks)
270+
266271
Next, you can write services that you can use to make database calls for the `User` and `Post` models from your Prisma schema.
267272

268273
Still inside the `src` directory, create a new file called `user.service.ts` and add the following code to it:
@@ -516,6 +521,25 @@ This controller implements the following routes:
516521

517522
- `/post/:id`: Delete a post by its `id`
518523

524+
#### Issues with `enableShutdownHooks`
525+
526+
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.
527+
528+
```typescript
529+
// main.ts
530+
...
531+
import { PrismaService } from './services/prisma/prisma.service';
532+
...
533+
bootstrap() {
534+
...
535+
const prismaService: PrismaService = app.get(PrismaService);
536+
prismaService.enableShutdownHooks(app)
537+
...
538+
}
539+
```
540+
541+
You can [read more](https://github.com/prisma/prisma/issues/2917#issuecomment-708340112) about Prisma handling of shutdown signal, and `beforeExit` event.
542+
519543
#### Summary
520544

521545
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)