IssueRaiser

Ability to extend `PrismaClient` class w/ Client Extensions before instantiation

## Problem Within NestJS, the common solution to implementing an injectable Prisma Client instance is to extend `PrismaClient` and add the `onModuleInit` and `enableShutdownHooks` functions required by NestJS. This typically looks like: ```ts import { Injectable, OnModuleInit, INestApplication } from '@nestjs/common' import { PrismaClient } from '@prisma/client' @Injectable() export class PrismaService extends PrismaClient implements OnModuleInit { async onModuleInit() { await this.$connect() } async enableShutdownHooks(app: INestApplication) { this.$on('beforeExit', async () => { await app.close() }) } } ``` You can use `clientExtensions` to add these functions, however, they can not adjust the type of `PrismaClient` and this scenario requires you to export a properly typed, uninstantiated class for NestJS to use. An example of adding the functions is below: ```ts const prisma = new PrismaClient().$extends({ client: { async onModuleInit() { await Prisma.getExtensionContext(this).$connect(); }, async enableShutdownHooks(app: INestApplication) { Prisma.getExtensionContext(this).$on('beforeExit', async () => { await app.close(); }); }, }, }); ``` ## Suggested solution Add a static method to `PrismaClient` that is available when using the `clientExtensions` preview features that allows you to add methods to the class (and adjust the type) before instantiation. This resulting class describe above wo

Posted by sabinadams on Apr 4, 2023, 9:19 PM

Tags

Related issues

Raise real-world problems, find people facing the same issue, and discuss solutions.2,500 issues0 comments

github (SaaS)

Ability to extend `PrismaClient` class w/ Client Extensions before instantiation

sabinadamsApr 4, 2023, 9:19 PM

## Problem Within NestJS, the common solution to implementing an injectable Prisma Client instance is to extend `PrismaClient` and add the `onModuleInit` and `enableShutdownHooks` functions required by NestJS. This typically looks like: ```ts import { Injectable, OnModuleInit, INestApplication } from '@nestjs/common' import { PrismaClient } from '@prisma/client' @Injectable() export class PrismaService extends PrismaClient implements OnModuleInit { async onModuleInit() { await this.$connect() } async enableShutdownHooks(app: INestApplication) { this.$on('beforeExit', async () => { await app.close() }) } } ``` You can use `clientExtensions` to add these functions, however, they can not adjust the type of `PrismaClient` and this scenario requires you to export a properly typed, uninstantiated class for NestJS to use. An example of adding the functions is below: ```ts const prisma = new PrismaClient().$extends({ client: { async onModuleInit() { await Prisma.getExtensionContext(this).$connect(); }, async enableShutdownHooks(app: INestApplication) { Prisma.getExtensionContext(this).$on('beforeExit', async () => { await app.close(); }); }, }, }); ``` ## Suggested solution Add a static method to `PrismaClient` that is available when using the `clientExtensions` preview features that allows you to add methods to the class (and adjust the type) before instantiation. This resulting class describe above wo

Comments (0)

No comments yet. Start the discussion.

Comment on this issue