everything-dev
Version:
A consolidated product package for building Module Federation apps with oRPC APIs.
71 lines (69 loc) • 2.91 kB
JavaScript
const require_defaults = require("./defaults.cjs");
let node_fs = require("node:fs");
let node_path = require("node:path");
let dotenv = require("dotenv");
//#region src/db/binding.ts
/**
* Materialize the environment a spawned tool needs to reach the binding's
* database: the canonical secret name mapped to the resolved URL. The process
* that resolves bindings (the CLI composition root) pushes this into spawned
* children so they never re-derive secrets from ambient state.
*/
function bindingEnv(binding) {
return { [binding.secretName]: binding.url };
}
/**
* The zero-config local database: a persistent, file-backed PGlite data dir
* resolved per workspace process (`.bos/db` relative to the workspace cwd).
* No ports, no docker — concurrent projects never collide. This default is a
* runtime-driver concern only: drizzle-kit commands (db:push/db:studio/
* db:migrate) need a real postgres DSN in the environment.
*
* Single-sourced in `defaults.json` (read by plain-node tooling like the
* regression env too) — never hardcode the literal elsewhere. Scaffolded
* templates and plugin secret defaults deliberately keep their own frozen
* copy: a database location is pinned at init, it must not silently follow
* a framework upgrade.
*/
const PGLITE_DEFAULT_URL = require_defaults.pgliteDefaultUrl;
function findEnvFile(startDir) {
if (!startDir) return void 0;
let current = startDir;
for (let i = 0; i < 15; i++) {
const candidate = (0, node_path.join)(current, ".env");
if ((0, node_fs.existsSync)(candidate)) return candidate;
const parent = (0, node_path.dirname)(current);
if (parent === current) return void 0;
current = parent;
}
}
/**
* Resolve the connection URL for a workspace identity.
*
* Resolution order:
* 1. the canonical secret in the provided environment
* 2. the canonical secret in the nearest `.env` walked up from the
* workspace directory (existing environment wins — values are not
* overridden, matching dotenv semantics)
* 3. the persistent local default (`PGLITE_DEFAULT_URL`) — consumed by the
* runtime driver (`createDatabaseDriver`), so boot migrations apply
* zero-config against the workspace's `.bos/db` data dir
*
* drizzle-kit commands need a postgres DSN; feeding them the pglite default
* fails loudly at connect time.
*/
function resolveDatabaseUrl(identity, options = {}) {
const direct = (options.env ?? process.env)[identity.secretName];
if (direct) return direct;
const envFile = findEnvFile(identity.workspaceDir);
if (envFile) try {
const fromFile = (0, dotenv.parse)((0, node_fs.readFileSync)(envFile, "utf-8"))[identity.secretName];
if (fromFile) return fromFile;
} catch {}
return PGLITE_DEFAULT_URL;
}
//#endregion
exports.PGLITE_DEFAULT_URL = PGLITE_DEFAULT_URL;
exports.bindingEnv = bindingEnv;
exports.resolveDatabaseUrl = resolveDatabaseUrl;
//# sourceMappingURL=binding.cjs.map