mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-06 10:44:23 +00:00
refactor(db): type the database settings and inject one engine
The settings for the database were one flat record whose url and pool size
were meaningful only for Postgres, so every reader had to re-check the URL.
They are now a discriminated union per engine (DbEngineSettings): the
Postgres arm carries its URL and a pool size already capped at the ceiling,
and the SQLite arm carries nothing else. The env schema uses the same ceiling
constant, so the boot check and the derivation cannot drift apart.
The engine was resolved from the environment by every caller that needed it
(the lifecycle, the unit of work, the backup adapters), each building its own
instance. The engine is now provided once per container by
DatabaseEngineModule and injected everywhere, so the state an engine keeps
(Postgres's reopen hook) is the same wherever it is used. resolveEngine stays
for the MikroORM CLI config and the DI factory, and nothing else calls it.
The engine's reopen no longer carries a `reopenSwapsConnection` flag. The
engine itself decides what a reopen needs: SQLite closes and reconnects the
ORM's connection before the schema bootstrap (its handle is swapped under
Kysely's cache), and Postgres only migrates, because a restore replaces the
contents of the database the pool already reaches. The Postgres pool is never
closed on a reopen, since ending it waits for connections lent out during the
restore.
The backup ports answer every method the same way. verify() now throws a
DomainError for a refused archive on both engines, and the restore path maps
it back to the same { success: false, error, status } result, so the HTTP
response of a refused restore (status and body) is unchanged. The restore
step that swaps the database and then always reopens it is shared in
nest/backup/database-swap.ts, with the error-description helper in
nest/common, instead of two copies that could drift.
This commit is contained in:
@@ -0,0 +1,14 @@
|
||||
/** The databases TREK can run on. MySQL and MariaDB are out of scope (server/CLAUDE.md). */
|
||||
export type EngineId = 'sqlite' | 'postgres';
|
||||
|
||||
/**
|
||||
* Which database this process runs on, as `deriveDb` reads it from
|
||||
* `TREK_DB_URL`. The Postgres arm carries what its pool needs, so nothing
|
||||
* downstream has to check the URL again.
|
||||
*/
|
||||
export type DbEngineSettings = { engine: 'sqlite' } | { engine: 'postgres'; url: string; poolMax: number };
|
||||
|
||||
/** The most connections the Postgres pool opens (`TREK_DB_POOL_MAX` is refused above it). */
|
||||
export const DB_POOL_MAX_CEILING = 100;
|
||||
/** The pool size when `TREK_DB_POOL_MAX` is unset. */
|
||||
export const DB_POOL_MAX_DEFAULT = 10;
|
||||
@@ -14,6 +14,7 @@
|
||||
* call, which is what keeps the runtime env-mutation semantics the test suite
|
||||
* depends on. Zod validation runs once at boot (env.schema.ts), never here.
|
||||
*/
|
||||
import { DB_POOL_MAX_CEILING, DB_POOL_MAX_DEFAULT, type DbEngineSettings } from './db-settings';
|
||||
import { imageVersion } from './image-version';
|
||||
import {
|
||||
csvList,
|
||||
@@ -336,16 +337,23 @@ export function deriveBackup(raw: RawEnv) {
|
||||
};
|
||||
}
|
||||
|
||||
/** The database settings `readEnv().db` answers with. */
|
||||
export type DbSettings = ReturnType<typeof deriveDb>;
|
||||
|
||||
export function deriveDb(raw: RawEnv) {
|
||||
const durability = resolveDurability(raw.TREK_DB_JOURNAL_MODE, raw.TREK_DB_SYNCHRONOUS);
|
||||
const url = raw.TREK_DB_URL?.trim() || undefined;
|
||||
const engine: DbEngineSettings = url
|
||||
? {
|
||||
engine: 'postgres',
|
||||
// The connection URL carries the password, so it is never logged (db/engine/redact-url.ts).
|
||||
url,
|
||||
poolMax: Math.min(positiveIntOr(raw.TREK_DB_POOL_MAX, DB_POOL_MAX_DEFAULT), DB_POOL_MAX_CEILING),
|
||||
}
|
||||
: { engine: 'sqlite' };
|
||||
return {
|
||||
/** Which database this process runs on: Postgres when TREK_DB_URL names one, the SQLite file otherwise. */
|
||||
engine: (url ? 'postgres' : 'sqlite') as 'sqlite' | 'postgres',
|
||||
/** The Postgres connection URL; it carries the password, so never log it (db/engine/redact-url.ts). */
|
||||
url,
|
||||
/** The largest number of connections the Postgres pool opens. */
|
||||
poolMax: Math.min(positiveIntOr(raw.TREK_DB_POOL_MAX, 10), 100),
|
||||
...engine,
|
||||
/** Where pg_dump and pg_restore are; unset, the usual install directories are tried (never PATH). */
|
||||
pgBinDir: raw.TREK_DB_PG_BIN_DIR?.trim() || undefined,
|
||||
trekDbFile: raw.TREK_DB_FILE,
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
* listed here pass through untouched (Zod strips unknown keys, it does not
|
||||
* reject them).
|
||||
*/
|
||||
import { DB_POOL_MAX_CEILING } from './db-settings';
|
||||
import {
|
||||
isP256PrivateKey,
|
||||
isServiceKeyShape,
|
||||
@@ -167,7 +168,7 @@ export const envSchema = z.object({
|
||||
// A Postgres database instead of the SQLite file. Its value is a secret (it
|
||||
// carries the password), so it is masked in the boot report.
|
||||
TREK_DB_URL: postgresUrl,
|
||||
TREK_DB_POOL_MAX: integer(1, 100),
|
||||
TREK_DB_POOL_MAX: integer(1, DB_POOL_MAX_CEILING),
|
||||
TREK_DB_PG_BIN_DIR: anyString,
|
||||
TREK_DB_FILE: anyString,
|
||||
// The two SQLite pragmas are deliberately unvalidated: a typo here must fall
|
||||
|
||||
@@ -2,6 +2,7 @@ export { readEnv, validateEnvAtBoot } from './env';
|
||||
export { getAppUrl, getMcpSafeUrl } from './app-url';
|
||||
export { runningVersion } from './running-version';
|
||||
export type { AppEnv, RawEnv } from './env';
|
||||
export type { DbSettings } from './derive';
|
||||
export {
|
||||
deriveAll,
|
||||
deriveApp,
|
||||
|
||||
Reference in New Issue
Block a user