Read Replica Type Annotation

How do I properly type the read replica global variable.
No description
1 Reply
Eco 🌤
Eco 🌤OP•5w ago
Just inferred the types instead..
import "../../envConfig";
import { env } from "@/env/server";
import { withReplicas } from "drizzle-orm/mysql-core";
import { drizzle } from "drizzle-orm/mysql2";
import config from "./config";
import * as relations from "./relations";
import * as schema from "./schema";

const DRIZZLE_CONFIG = {
connection: config.primary,
schema: {
...schema,
...relations,
},
mode: "default" as const,
casing: "snake_case" as const,
} as const;

const primary = drizzle({ ...DRIZZLE_CONFIG, connection: config.primary });
const replica1 = drizzle({ ...DRIZZLE_CONFIG, connection: config.read[0] });
const replica2 = drizzle({ ...DRIZZLE_CONFIG, connection: config.read[1] });
const drizzleDb = withReplicas(primary, [replica1, replica2]);

declare const global: {
drizzleGlobal: typeof drizzleDb;
};

const db = global.drizzleGlobal ?? drizzleDb;

export { db };

if (env.NODE_ENV !== "production") global.drizzleGlobal = db;
import "../../envConfig";
import { env } from "@/env/server";
import { withReplicas } from "drizzle-orm/mysql-core";
import { drizzle } from "drizzle-orm/mysql2";
import config from "./config";
import * as relations from "./relations";
import * as schema from "./schema";

const DRIZZLE_CONFIG = {
connection: config.primary,
schema: {
...schema,
...relations,
},
mode: "default" as const,
casing: "snake_case" as const,
} as const;

const primary = drizzle({ ...DRIZZLE_CONFIG, connection: config.primary });
const replica1 = drizzle({ ...DRIZZLE_CONFIG, connection: config.read[0] });
const replica2 = drizzle({ ...DRIZZLE_CONFIG, connection: config.read[1] });
const drizzleDb = withReplicas(primary, [replica1, replica2]);

declare const global: {
drizzleGlobal: typeof drizzleDb;
};

const db = global.drizzleGlobal ?? drizzleDb;

export { db };

if (env.NODE_ENV !== "production") global.drizzleGlobal = db;
Want results from more Discord servers?
Add your server