fix(recovery): restore source removed by production documentation merge
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
/**
|
||||
* src/repositories/db.ts
|
||||
* ========================
|
||||
* Lazy Postgres connection for P0 capability repositories (src/repositories/*).
|
||||
*
|
||||
* Deliberately separate from lib/db.ts: that module is tuned for the
|
||||
* Vercel-hosted edge Postgres behind app/api/orders + app/api/subscriptions
|
||||
* (`ssl: "require"`, because that's what Vercel Postgres needs) and lives in
|
||||
* lib/, which src/ (the blocking-typechecked capability layer,
|
||||
* .dependency-cruiser.cjs's "no-src-importing-app-or-components" rule's
|
||||
* sibling concern) does not depend on. The P0 capability layer's Postgres
|
||||
* instance is the self-hosted VPS Postgres from docs/08-infrastructure.md
|
||||
* (infra/docker-compose.yml's `postgres` service, same private docker network
|
||||
* as the FastAPI engine, no TLS terminated in front of it) -- forcing
|
||||
* `ssl: "require"` here would break every real connection to it. SSL is
|
||||
* opt-in via PGSSL=require for deployments that do put TLS in front.
|
||||
*
|
||||
* Same lazy/env-guarded shape as lib/db.ts, and the same production-safety
|
||||
* decision as every other P0 dependency: src/platform/dependency-mode.ts's
|
||||
* `resolveStoreMode` is what actually decides postgres-vs-memory (and fails
|
||||
* hard in production with no URL) -- this module only opens the connection
|
||||
* once that decision has already been made.
|
||||
*/
|
||||
import postgres from "postgres";
|
||||
|
||||
export type RepositorySql = ReturnType<typeof postgres>;
|
||||
|
||||
/** DATABASE_URL, falling back to Vercel Postgres's env names -- same precedence lib/db.ts's dbUrl() uses, kept independent so src/ has no import on lib/. */
|
||||
export function repositoryDatabaseUrl(env: NodeJS.ProcessEnv = process.env): string | undefined {
|
||||
return env.DATABASE_URL || env.POSTGRES_URL || env.POSTGRES_PRISMA_URL;
|
||||
}
|
||||
|
||||
let _sql: RepositorySql | null = null;
|
||||
let _urlUsed: string | undefined;
|
||||
|
||||
/** Returns a memoized connection for `url`; re-opens if a different URL is requested (e.g. tests pointing at a fresh testcontainer). */
|
||||
export function getRepositorySql(url: string): RepositorySql {
|
||||
if (_sql && _urlUsed === url) return _sql;
|
||||
_sql = postgres(url, {
|
||||
prepare: false,
|
||||
idle_timeout: 20,
|
||||
max: 5,
|
||||
ssl: process.env.PGSSL === "require" ? "require" : false,
|
||||
});
|
||||
_urlUsed = url;
|
||||
return _sql;
|
||||
}
|
||||
|
||||
/** Test-only escape hatch: force the next getRepositorySql call to open a fresh connection. */
|
||||
export function resetRepositorySqlForTests(): void {
|
||||
_sql = null;
|
||||
_urlUsed = undefined;
|
||||
}
|
||||
|
||||
/** Test-only: gracefully close the memoized connection (so a vitest run doesn't hang on an open socket) and clear it. */
|
||||
export async function closeRepositorySqlForTests(): Promise<void> {
|
||||
const sql = _sql;
|
||||
_sql = null;
|
||||
_urlUsed = undefined;
|
||||
if (sql) await sql.end({ timeout: 5 });
|
||||
}
|
||||
Reference in New Issue
Block a user