// DB-Connection-Types: provider-agnostic via db/api.ts. // createConnection delegiert an postgres-provider (default) oder bun-provider (DB_PROVIDER=bun). import type { DbConnection, PgClient } from "@cosmicdrift/kumiko-types/db-connection"; import postgres from "postgres"; import { readPositiveIntEnv } from "../utils/env-parse"; // Raw client types (postgres-js | Bun.SQL) — the name used across query/ // event-store/pipeline call sites. The structural pool handle from ./api is // `DbPoolHandle` (createConnection's return type) to avoid colliding with this. export type * from "@cosmicdrift/kumiko-types/db-connection"; export type { DbConnectionOptions, DbPoolHandle } from "./api"; export { createConnection } from "./api"; // Legacy: postgres-js only. Neue Aufrufer: createConnection() aus api.ts. // guard:dup-ok — andere Layer als createPgConnection (gibt DbConnection zurück, nicht postgres-Instanz) export function createDbConnection( url: string, options: import("./api").DbConnectionOptions = {}, ): { db: DbConnection; client: PgClient; close: () => Promise; } { const pgOptions: Parameters[1] = {}; if (options.maxConnections !== undefined) pgOptions.max = options.maxConnections; if (options.idleTimeoutSeconds !== undefined) pgOptions.idle_timeout = options.idleTimeoutSeconds; if (options.connectTimeoutSeconds !== undefined) { pgOptions.connect_timeout = options.connectTimeoutSeconds; } const client = postgres(url, pgOptions); return { db: client, client, close: async () => { await client.end(); }, }; } export function dbConnectionOptionsFromEnv( env: Readonly> = process.env, ): import("./api").DbConnectionOptions { const opts: import("./api").DbConnectionOptions & { maxConnections?: number; idleTimeoutSeconds?: number; connectTimeoutSeconds?: number; } = {}; const max = readPositiveIntEnv(env, "DATABASE_POOL_MAX"); const idle = readPositiveIntEnv(env, "DATABASE_POOL_IDLE_TIMEOUT"); const connect = readPositiveIntEnv(env, "DATABASE_POOL_CONNECT_TIMEOUT"); if (max !== undefined) opts.maxConnections = max; if (idle !== undefined) opts.idleTimeoutSeconds = idle; if (connect !== undefined) opts.connectTimeoutSeconds = connect; return opts; }