读副本
当你的项目涉及一组读副本实例,并且你需要一种便捷的方法来管理来自读副本的
SELECT 查询,以及在主实例上执行创建、删除和更新操作时,你可以在 Drizzle 中利用 withReplicas() 函数
import { sql } from 'drizzle-orm';
import { drizzle } from 'drizzle-orm/cockroach';
import { boolean, jsonb, cockroachTable, int4, string, timestamp, withReplicas } from 'drizzle-orm/cockroach-core';
const usersTable = cockroachTable("users", {
id: int4().primaryKey(),
name: string().notNull(),
verified: boolean().notNull().default(false),
jsonb: jsonb().$type<string[]>(),
createdAt: timestamp("created_at", { withTimezone: true }).notNull().defaultNow(),
});
const primaryDb = drizzle("postgres://user:password@host:port/primary_db");
const read1 = drizzle("postgres://user:password@host:port/read_replica_1");
const read2 = drizzle("postgres://user:password@host:port/read_replica_2");
const db = withReplicas(primaryDb, [read1, read2]);你现在可以像之前一样使用 db 实例。Drizzle 将会自动
处理读副本和主实例之间的选择
// 从 read1 连接或 read2 连接读取
await db.select().from(usersTable)
// 使用主数据库执行删除操作
await db.delete(usersTable).where(eq(usersTable.id, 1))你可以使用 $primary 键来强制即使是读取操作也使用主实例
// 从主库读取
await db.$primary.select().from(usersTable);使用 Drizzle,你还可以为选择读副本指定自定义逻辑。 你可以进行加权决策,或使用任何其他自定义选择方法来随机选择读副本。 下面是一个自定义逻辑用于选择读副本的实现示例, 其中第一个副本有 70% 的概率被选中,第二个副本有 30% 的概率被选中。
请记住,你可以为读副本实现任何类型的随机选择方法
const db = withReplicas(primaryDb, [read1, read2], (replicas) => {
const weight = [0.7, 0.3];
let cumulativeProbability = 0;
const rand = Math.random();
for (const [i, replica] of replicas.entries()) {
cumulativeProbability += weight[i]!;
if (rand < cumulativeProbability) return replica;
}
return replicas[0]!
});
await db.select().from(usersTable)