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
Support Prisma Client Extensions (Preview) #43
Comments
Example to use Prisma Client Extensions.
datasource db {
provider = "sqlite"
url = "file:dev.db"
}
generator client {
provider = "prisma-client-js"
previewFeatures = ["clientExtensions"]
}
model User {
id String @id @default(cuid())
email String @unique
name String?
}
import { PrismaClient } from '@prisma/client';
/**
* TS error: "Inferred type of this node exceeds the maximum length the compiler will serialize" with "declaration": true in tsconfig
*
* Change "declaration": false in `tsconfig.json`
*
* https://github.com/prisma/prisma/issues/16536#issuecomment-1332055501
*/
export const prismaClient = new PrismaClient().$extends({
model: {
user: {
findByEmail: async (email: string) => {
return prismaClient.user.findFirstOrThrow({ where: { email } });
},
},
},
});
export type prismaClient = typeof prismaClient;
import { Module } from '@nestjs/common';
import { CustomPrismaModule } from 'nestjs-prisma';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { prismaClient } from './prisma.extension';
@Module({
imports: [
CustomPrismaModule.forRootAsync({
name: 'PrismaService',
useFactory: () => {
return prismaClient;
},
}),
],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
import { Inject, Injectable } from '@nestjs/common';
import { CustomPrismaService } from 'nestjs-prisma';
import { prismaClient } from './prisma.extension';
@Injectable()
export class AppService {
constructor(
// ✅ use `prismaClient` from extension for correct type-safety
@Inject('PrismaService')
private prismaService: CustomPrismaService<prismaClient>,
) {}
users() {
return this.prismaService.client.user.findMany();
}
user(userId: string) {
// 🦾 use new `findByEmail`
return this.prismaService.client.user.findByEmail(userId);
}
} |
Thank you for the example. That helps a lot to understand how to use extension with new dev release version, but is there a way we can use extension in existing PrismaService or anyway to override that? |
hi @shoaibsharif haven't found a way to use extension with the existing |
Prisma release with in version 4.7.0 Prisma Client Extensions (Preview)
This should be possible with the new
CustomPrismaModule
/CustomPrismaService
(#40) where you can need to provide aPrismaClient
instance.The text was updated successfully, but these errors were encountered: