import { assertProductionDatabaseTransport, type DatabaseTransportEnvironment, type DatabaseTransportPolicy, createDatabaseTransportPolicy, } from "../database/transport.js"; import { loadHostConfiguration } from "../host/index.js"; /** * The database transport policy for the small Core-owned executables — `pnpm migrations:plan`, * `pnpm migrations:apply`, `pnpm modules:enable`, and the owner-bootstrap commands — resolved from * the process environment. * * The mapping itself belongs to `loadHostConfiguration`, which `startApi` and `pnpm doctor` also * call, so `NODE_ENV` and the two TLS variables have exactly one interpretation across every entry * point. Environment *reading* still happens here, in the executable layer: the reusable Core * database and migration constructors keep taking explicit typed inputs and never touch * `process.env`. */ export function readDatabaseTransportFromEnvironment(): { readonly environment: DatabaseTransportEnvironment; readonly transport: DatabaseTransportPolicy; } { const host = loadHostConfiguration(process.env); const transport = createDatabaseTransportPolicy(host.databaseTransport, { environment: host.environment, }); return { environment: host.environment, transport }; } /** * Resolves the policy and asserts it is safe for the given database URL before any client, pool, * or connection is constructed. Throws the sanitized `DatabaseTransportError` otherwise. */ export function resolveExecutableDatabaseTransport(databaseUrl: string): DatabaseTransportPolicy { const { environment, transport } = readDatabaseTransportFromEnvironment(); assertProductionDatabaseTransport(databaseUrl, transport, environment); return transport; }