Files
probably-rendering-one-more…/apps/waterfall-wiki/src/lib/db.ts
T
codex-bot ad398db781
waterfall-wiki CI and deployment / verify-and-deploy (push) Has been cancelled
studio CI and deployment / verify-and-deploy (push) Successful in 9m15s
Restore PROMPT branding, rename waterfall-wiki, and add system tag workflows
2026-10-27 00:35:59 +08:00

14 lines
661 B
TypeScript

import { Pool, type PoolClient } from "pg";
const globalDb = globalThis as unknown as { promptPool?: Pool };
export function db() {
if (!process.env.DATABASE_URL) throw new Error("DATABASE_URL is required");
return globalDb.promptPool ??= new Pool({ connectionString: process.env.DATABASE_URL, max: 10 });
}
export async function transaction<T>(run: (client: PoolClient) => Promise<T>): Promise<T> {
const client = await db().connect();
try { await client.query("BEGIN"); const result = await run(client); await client.query("COMMIT"); return result; }
catch (error) { await client.query("ROLLBACK"); throw error; }
finally { client.release(); }
}