mirror of
https://github.com/outbackdingo/pangolin.git
synced 2026-01-28 02:20:01 +00:00
add replica connections for pg
This commit is contained in:
2730
package-lock.json
generated
2730
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@@ -1,16 +1,32 @@
|
||||
import { drizzle as DrizzlePostgres } from "drizzle-orm/node-postgres";
|
||||
import { readConfigFile } from "@server/lib/readConfigFile";
|
||||
import { withReplicas } from "drizzle-orm/pg-core";
|
||||
|
||||
function createDb() {
|
||||
const config = readConfigFile();
|
||||
|
||||
const connectionString = config.postgres?.connection_string;
|
||||
const replicaConnections = config.postgres?.replicas || [];
|
||||
|
||||
if (!connectionString) {
|
||||
throw new Error("Postgres connection string is not defined in the configuration file.");
|
||||
throw new Error(
|
||||
"A primary db connection string is required in the configuration file."
|
||||
);
|
||||
}
|
||||
|
||||
return DrizzlePostgres(connectionString);
|
||||
const primary = DrizzlePostgres(connectionString);
|
||||
const replicas = [];
|
||||
|
||||
if (!replicaConnections.length) {
|
||||
replicas.push(primary);
|
||||
} else {
|
||||
for (const conn of replicaConnections) {
|
||||
const replica = DrizzlePostgres(conn.connection_string);
|
||||
replicas.push(replica);
|
||||
}
|
||||
}
|
||||
|
||||
return withReplicas(primary, replicas as any);
|
||||
}
|
||||
|
||||
export const db = createDb();
|
||||
|
||||
@@ -121,9 +121,16 @@ export const configSchema = z.object({
|
||||
}),
|
||||
postgres: z
|
||||
.object({
|
||||
connection_string: z.string().optional()
|
||||
connection_string: z.string(),
|
||||
replicas: z
|
||||
.array(
|
||||
z.object({
|
||||
connection_string: z.string()
|
||||
})
|
||||
)
|
||||
.optional()
|
||||
})
|
||||
.default({}),
|
||||
.optional(),
|
||||
traefik: z
|
||||
.object({
|
||||
http_entrypoint: z.string().optional().default("web"),
|
||||
|
||||
Reference in New Issue
Block a user