-
-
Notifications
You must be signed in to change notification settings - Fork 49
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
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
Using extension packages (read replica) #77
Comments
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. |
Checkout the new section for Read Replica in the docs. |
This is great, I will give it a shot today, thanks again @marcjulian ! |
@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? |
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 |
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 |
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. |
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
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!
The text was updated successfully, but these errors were encountered: