feat: add reviewed postgres migration foundation

This commit is contained in:
Jesse_Chen
2026-07-20 22:12:55 +08:00
parent 1afbc23e19
commit 69b38ee6ed
8 changed files with 750 additions and 3 deletions
+18
View File
@@ -0,0 +1,18 @@
import { drizzle, type NodePgDatabase } from "drizzle-orm/node-postgres";
import { Pool } from "pg";
export type DomainDatabase = { pool: Pool; db: NodePgDatabase };
export function createDomainDatabase(
connectionString: string,
maxConnections = 5,
): DomainDatabase {
const pool = new Pool({
connectionString,
max: maxConnections,
idleTimeoutMillis: 30_000,
connectionTimeoutMillis: 5_000,
application_name: "jyotisha-web",
});
return { pool, db: drizzle(pool) };
}
+16
View File
@@ -0,0 +1,16 @@
export type DatabaseUrlKey =
| "IDENTITY_DATABASE_URL"
| "APP_DATABASE_URL"
| "ADMIN_DATABASE_URL";
export function readDatabaseUrl(
env: NodeJS.ProcessEnv,
key: DatabaseUrlKey,
): string {
const value = env[key]?.trim();
if (!value) throw new Error(`${key} is required`);
if (!value.startsWith("postgresql://")) {
throw new Error(`${key} must be a PostgreSQL URL`);
}
return value;
}