mirror of
https://github.com/fosrl/pangolin.git
synced 2025-06-26 23:28:47 +02:00
add replica connections for pg
This commit is contained in:
parent
5c682fe923
commit
b86ef93211
3 changed files with 2608 additions and 153 deletions
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()
|
||||
})
|
||||
.default({}),
|
||||
)
|
||||
.optional()
|
||||
})
|
||||
.optional(),
|
||||
traefik: z
|
||||
.object({
|
||||
http_entrypoint: z.string().optional().default("web"),
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue