rspace-online/shared/db/pool.ts

30 lines
888 B
TypeScript

/**
* Shared PostgreSQL connection pool for all rSpace modules.
*
* Uses the `postgres` (postgres.js) library already in package.json.
* Each module uses its own schema (SET search_path) but shares this pool.
*/
import postgres from "postgres";
const DATABASE_URL =
process.env.DATABASE_URL || "postgres://rspace:rspace@rspace-db:5432/rspace";
/** Global shared connection */
export const sql = postgres(DATABASE_URL, {
max: 20,
idle_timeout: 30,
connect_timeout: 10,
});
/**
* Run a module's schema migration SQL.
* Called once at startup for modules that need a database.
*/
export async function runMigration(schema: string, migrationSQL: string): Promise<void> {
await sql.unsafe(`SET search_path TO ${schema}, public`);
await sql.unsafe(migrationSQL);
await sql.unsafe(`SET search_path TO public`);
console.log(`[DB] Migration complete for schema: ${schema}`);
}