Replies: 8 comments 3 replies
-
Hi @MorenoMdz, this is already possible with Prisma Client Extension. I have added it to the Extensions Example. nestjs-prisma/examples/extensions/src/prisma.extension.ts Lines 1 to 38 in 407de1b Let me know if this helps you. I will add a section to the docs too. |
Beta Was this translation helpful? Give feedback.
-
Checkout the new section for Read Replica in the docs. |
Beta Was this translation helpful? Give feedback.
-
This is great, I will give it a shot today, thanks again @marcjulian ! |
Beta Was this translation helpful? Give feedback.
-
@marcjulian I might have to redo our setup, currently we have a import { Injectable } from '@nestjs/common'
import { ConfigService } from '@nestjs/config'
import { PrismaOptionsFactory, PrismaServiceOptions } from 'nestjs-prisma'
import { getEnvironment, isProduction, isDevelopment } from './utils'
@Injectable()
export class PrismaConfigService implements PrismaOptionsFactory {
constructor(private configService: ConfigService) {}
createPrismaOptions(): PrismaServiceOptions | Promise<PrismaServiceOptions> {
const environment = getEnvironment()
const isProd = environment === 'production'
const urlSuffix = isProduction()
? '_PROD'
: // When running locally, and we want to use the DEV database
isDevelopment() || process.env.FIREBASE_ENV === 'DEV'
? '_DEV'
: ''
const DB_URL = this.configService.get(`DATABASE_URL${urlSuffix}`)
return {
prismaOptions: {
log: isProd ? undefined : ['info', 'query'],
datasources: {
db: {
url: DB_URL,
},
},
},
explicitConnect: this.configService.get('DB_EXPLICIT_CONNECT') || false,
}
}
} And then the app.module is initialized like this: ...
imports: [
PrismaModule.forRootAsync({
isGlobal: true,
useClass: PrismaConfigService,
}),
...
] Should I just redo the setup and or try to plug in the extensions on top of this at the App.module imports section? |
Beta Was this translation helpful? Give feedback.
-
You will need to use Use import { Module } from '@nestjs/common';
import { CustomPrismaModule } from 'nestjs-prisma';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { extendedPrismaClient } from './prisma.extension';
@Module({
imports: [
CustomPrismaModule.forRootAsync({
name: 'PrismaService',
useFactory: (config: ConfigService) => {
const options = {};
return extendedPrismaClient(options);
},
inject: [ConfigService],
// import ConfigModule when `isGlobal` not true
// imports: [ConfigModule],
}),
ConfigModule.forRoot({
isGlobal: true,
}),
],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {} import { Prisma, PrismaClient } from '@prisma/client';
// used for Read Replicas example
import { readReplicas } from '@prisma/extension-read-replicas';
// add more parameters like read replicas urls
export const extendedPrismaClient = (options?: Prisma.PrismaClientOptions, ...) =>
new PrismaClient<
Prisma.PrismaClientOptions,
'query' | 'info' | 'warn' | 'error'
>(options)
// .$extends(
// readReplicas({
// url: 'postgres://localhost:5432/prisma',
// }),
// );
// add ReturnType because extendedPrismaClient is now a function
export type extendedPrismaClient = ReturnType<typeof extendedPrismaClient>; Let me know if that helps. I might need to update the docs to make it more clear to use |
Beta Was this translation helpful? Give feedback.
-
Thanks again for the response. I am still not sure I get how to configure this with our current setup.
Also, the way we have been injecting the Prisma service in our services is like |
Beta Was this translation helpful? Give feedback.
-
To clear it up a bit more. PrismaModule and PrismaService
import { Inject, Injectable, Optional } from '@nestjs/common';
import { PrismaClient } from '@prisma/client';
import { PrismaServiceOptions } from './interfaces';
import { PRISMA_SERVICE_OPTIONS } from './prisma.constants';
@Injectable()
export class PrismaService extends PrismaClient {
constructor(
@Optional()
@Inject(PRISMA_SERVICE_OPTIONS)
private readonly prismaServiceOptions: PrismaServiceOptions = {},
) {
super(prismaServiceOptions.prismaOptions);
...
}
...
}
CustomPrismaModule and CustomPrismaService
import { PrismaClientLike } from './custom-prisma-options';
import { Inject, Injectable } from '@nestjs/common';
import { CUSTOM_PRISMA_CLIENT } from './custom-prisma.constants';
@Injectable()
export class CustomPrismaService<Client extends PrismaClientLike> {
constructor(
@Inject(CUSTOM_PRISMA_CLIENT)
public client: Client,
) {}
}
import { ExtendedPrismaConfigService } from './extended-prisma-config.service';
// app.module.ts
CustomPrismaModule.forRootAsync({
name: 'PrismaService',
useClass: ExtendedPrismaConfigService,
})
// extended-prisma-config.service.ts
import { Injectable } from '@nestjs/common';
import { CustomPrismaClientFactory } from 'nestjs-prisma';
import { extendedPrismaClient } from './prisma.extension';
@Injectable()
export class ExtendedPrismaConfigService
implements CustomPrismaClientFactory<extendedPrismaClient>
{
constructor() {
// inject config service here
}
createPrismaClient(): extendedPrismaClient {
return extendedPrismaClient;
}
} ❌ I will move this into a discussion as this might help others too. |
Beta Was this translation helpful? Give feedback.
-
How do I pass middleware in CustomPrismaModule useFactory? |
Beta Was this translation helpful? Give feedback.
-
First, thanks for the solid work here!
I would like to see if it is already possible to use this service with read replicas and or see if it would be possible to add support for this extension.
https://github.com/prisma/extension-read-replicas
Thank you!
Beta Was this translation helpful? Give feedback.
All reactions