mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-20 11:06:06 +00:00
25f5e34558
A port for the engine TREK runs on, without changing what it runs on. src/db/engine/ names the engine (SqliteEngine, PostgresEngine) and builds the MikroORM config from it; src/mikro-orm.config.ts exports that config for the CLI and the Nest container alike. Without TREK_DB_URL the config is the one it was: the shared SQLite driver on TREK_DB_FILE or data/travel.db. - TREK_DB_URL takes a postgres:// or postgresql:// URL and is masked in the boot report; TREK_DB_POOL_MAX sizes the pool (1 to 100, default 10). TREK_DB_URL beside TREK_DB_FILE, TREK_DB_JOURNAL_MODE or TREK_DB_SYNCHRONOUS refuses the boot as an ambiguous configuration. - MikroORM builds the pg pool from the URL's host, port, user, password and database and drops the rest of the query, so the engine reads sslmode and sslrootcert itself. require, verify-ca and verify-full all verify the server's certificate, as the pg driver reads them; a mode that would not verify, or a misspelt one, refuses. - Postgres cannot run TREK yet, so a boot with TREK_DB_URL is refused with that reason, in the boot check and again where the config is built, before anything connects. - @mikro-orm/postgresql moves to dependencies; pg is plain JavaScript. - lint:engine-dispatch (CI) holds the engine checks outside src/db/dialect/ and src/db/engine/ (isSqlite(, isPostgres(, an instanceof on a platform, a comparison on the engine's name) to a baseline that starts and stays empty.
51 lines
2.1 KiB
JavaScript
51 lines
2.1 KiB
JavaScript
#!/usr/bin/env node
|
|
/*
|
|
* lint:engine-dispatch: code outside the dialect layer and the engine port
|
|
* never asks which database it runs on.
|
|
*
|
|
* What differs between SQLite and Postgres has two homes: the SQL helpers in
|
|
* src/db/dialect/ and the engine implementations in src/db/engine/ (the
|
|
* driver, the schema bootstrap, maintenance, backups). Anywhere else, an
|
|
* `isSqlite(`/`isPostgres(` check, an `instanceof` on a platform class or a
|
|
* comparison on the engine's name (`readEnv().db.engine === 'postgres'`) is a
|
|
* branch a third engine would have to find by searching, so each file is held
|
|
* to its entry in scripts/engine-dispatch-baseline.json and a file without one
|
|
* holds none (scripts/lib/count-ratchet.mjs has the rules). Comments do not
|
|
* count.
|
|
*
|
|
* npm run lint:engine-dispatch check against the baseline (CI)
|
|
* npm run lint:engine-dispatch -- --update lower the counts; never raises or adds an entry
|
|
*/
|
|
import { cliMain, stripComments } from './lib/count-ratchet.mjs';
|
|
|
|
const DISPATCH = new RegExp(
|
|
[
|
|
/\bis(?:Sqlite|Postgres)\s*\(/.source,
|
|
/\binstanceof\s+(?:Sqlite|BasePostgreSql|PostgreSql)Platform\b/.source,
|
|
// A comparison on the engine's name: `db.engine === 'postgres'`, `engine.id !== 'sqlite'`.
|
|
/\.(?:engine|id)\s*[!=]==?\s*['"](?:sqlite|postgres)['"]/.source,
|
|
].join('|'),
|
|
'g',
|
|
);
|
|
|
|
/** Where an engine may be named: the dialect helpers and the engine port. */
|
|
const ENGINE_HOMES = ['src/db/dialect/', 'src/db/engine/'];
|
|
|
|
/** The engine checks in one file's text, comments left out. */
|
|
export function countEngineDispatch(text) {
|
|
return stripComments(text).match(DISPATCH)?.length ?? 0;
|
|
}
|
|
|
|
export const check = {
|
|
name: 'engine-dispatch',
|
|
script: 'lint:engine-dispatch',
|
|
root: 'src',
|
|
baseline: 'scripts/engine-dispatch-baseline.json',
|
|
unit: 'engine check(s)',
|
|
advice: 'Dispatch through a helper in src/db/dialect/ or the DatabaseEngine in src/db/engine/ instead.',
|
|
include: (file) => file.endsWith('.ts') && !ENGINE_HOMES.some((home) => file.startsWith(home)),
|
|
count: (text) => countEngineDispatch(text),
|
|
};
|
|
|
|
cliMain(check, import.meta.url);
|