如何使用Prisma将读/写查询分开以从从属/主数据源?

发布于 2025-02-08 08:28:43 字数 743 浏览 2 评论 0原文

我使用的是带有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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文