feat: add reviewed postgres migration foundation
This commit is contained in:
@@ -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) };
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
Reference in New Issue
Block a user