/** * Configuration helpers for the db package. * * Reads from environment variables. Throws clearly if a required var * is missing. */ export interface DbConfig { /** Postgres connection URL, e.g. postgres://user:pass@host:5432/db */ databaseUrl: string; /** Redis connection URL, e.g. redis://host:6379 */ redisUrl: string; /** When true, callers should use the in-memory fallback instead. */ useInMemory: boolean; } const REQUIRED_FOR_DB = ['DATABASE_URL', 'REDIS_URL'] as const; export function loadConfig(env: NodeJS.ProcessEnv = process.env): DbConfig { const databaseUrl = env.DATABASE_URL; const redisUrl = env.REDIS_URL; // Allow opt-out for dev (e.g. when Docker isn't available) const forceInMemory = env.USE_IN_MEMORY === '1' || env.USE_IN_MEMORY === 'true'; if (forceInMemory) { return { databaseUrl: '', redisUrl: '', useInMemory: true }; } const missing = REQUIRED_FOR_DB.filter((k) => !env[k]); if (missing.length > 0) { // Don't crash on import — let the caller decide. The API's // bootstrap() logs a warning and falls back to in-memory mode. return { databaseUrl: databaseUrl ?? '', redisUrl: redisUrl ?? '', useInMemory: true, }; } return { databaseUrl: databaseUrl!, redisUrl: redisUrl!, useInMemory: false, }; }