NestJS example needed
Prisma extensions are fairly new and there are not many examples of how to use them with the NestJS framework. I think this official extension would be a great place to have some documentation on how you could use this extension with NestJS.
On nestjs we usually call PrismaService that is automatically injected globally on the framework. For me i didn't use this extension and just create a PrismaReadservice
import { Injectable } from '@nestjs/common';
import { Prisma, PrismaClient } from '@prisma/client';
import { logger } from '../logger/logger';
@Injectable()
export class PrismaReadService extends PrismaClient<
Prisma.PrismaClientOptions,
'query' | 'info' | 'warn' | 'error'
> {
constructor() {
let dbReadUrl = process.env.DATABASE_READ_URL;
if (!dbReadUrl) {
throw new Error('NO DATABASE URL DEFINED');
}
super({
datasourceUrl: dbReadUrl
});
}
}
So i just use it as prismaService for write and PrismaReadService. It works but is not like this extension.
@Injectable()
export class AuthService {
constructor(
private readonly prisma: PrismaService,
private readonly prismaRead: PrismaReadService,
Ofc will your responsibility in your code to not using read service for writing something, the code will not complain.
On nestjs we usually call PrismaService that is automatically injected globally on the framework. For me i didn't use this extension and just create a PrismaReadservice
import { Injectable } from '@nestjs/common'; import { Prisma, PrismaClient } from '@prisma/client'; import { logger } from '../logger/logger'; @Injectable() export class PrismaReadService extends PrismaClient< Prisma.PrismaClientOptions, 'query' | 'info' | 'warn' | 'error' > { constructor() { let dbReadUrl = process.env.DATABASE_READ_URL; if (!dbReadUrl) { throw new Error('NO DATABASE URL DEFINED'); } super({ datasourceUrl: dbReadUrl }); } }So i just use it as prismaService for write and PrismaReadService. It works but is not like this extension.
@Injectable() export class AuthService { constructor( private readonly prisma: PrismaService, private readonly prismaRead: PrismaReadService,Ofc will your responsibility in your code to not using read service for writing something, the code will not complain.
It worked well for me, thank you!
Here is a working example:
https://github.com/prisma/prisma/issues/18628#issuecomment-1611889400
Implemented an example for NestJS: https://github.com/dminglv/nestjs-prisma-postgres-cluster
Here is a working example:
I have tried most of the solutions commented in that issue, but when trying to create a custom Prisma service with the readReplicas extension, I get a "maximum stack call exceeded" error that points to the extension code. I don't get the same error with other extensions.
@Injectable()
export class DatabaseService extends PrismaClient implements OnModuleInit, OnModuleDestroy {
readonly extendedClient = prismaExtendedClient(this)
constructor() {
super()
new Proxy(this, {
get: (target, property) => Reflect.get(property in this.extendedClient ? this.extendedClient : target, property),
})
}
async onModuleInit() {
await this.$connect()
}
async onModuleDestroy() {
await this.$disconnect()
}
}