如何使用Prisma将读/写查询分开以从从属/主数据源?
我使用的是带有Nestjs的Prisma 3.15,需要将查询拆分为两个不同的数据源。一个是仅读取的副本,另一个是主数据库。
我不想在每个Prisma电话上手动管理这些!
如何自动拆分它们?
我可以在这样的Prismaservice中定义两个独立的客户:
@Injectable()
export class PrismaService implements OnModuleInit {
constructor(
private readClient: PrismaClient,
private writeClient: PrismaClient
) {
this.readClient = new PrismaClient({
datasources: {db: {url: process.env.PRISMA_READ_DB_URL}},
});
this.writeClient = new PrismaClient({
datasources: {db: {url: process.env.PRISMA_WRITE_DB_URL}},
});
}
但是如何在它们之间划分查询?
我可以覆盖这样的Prisma模块吗?
get user() {
// Do something with the user!!!
return this.readClient.user;
}
I'm using Prisma 3.15 with NestJS and need to split queries to two different data-sources. One is read-only replica and another is master database.
I don't want to manage these manually on each prisma call!
How can I split them automatically?
I can define two separate clients in PrismaService like this:
@Injectable()
export class PrismaService implements OnModuleInit {
constructor(
private readClient: PrismaClient,
private writeClient: PrismaClient
) {
this.readClient = new PrismaClient({
datasources: {db: {url: process.env.PRISMA_READ_DB_URL}},
});
this.writeClient = new PrismaClient({
datasources: {db: {url: process.env.PRISMA_WRITE_DB_URL}},
});
}
But how can I split queries between them?
Can I override prisma modules like this?
get user() {
// Do something with the user!!!
return this.readClient.user;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论