Files
TREK/server/tests/postgres/build-template.ts
T
Isaias Tavares 2e3762afe1 feat(db): run TREK on PostgreSQL
TREK runs on PostgreSQL when TREK_DB_URL is set, and on SQLite as before
otherwise: an engine port under src/db/engine, the dialect helpers under
src/db/dialect, a Postgres baseline for the migrations up to the cut,
pg_dump/pg_restore backups, a probe of every repository method against a
real server and a Postgres test lane in CI.
2026-11-17 23:03:33 +01:00

37 lines
1.3 KiB
TypeScript

import { buildOrmConfig, resolveEngine } from '../../src/db/engine';
import { MikroORM } from '@mikro-orm/core';
/**
* Brings the template database TREK_DB_URL names to the current schema, the
* way a first boot on an empty Postgres database does: the engine's own
* bootstrap (baseline, the migrations after its cut, seeders), with no
* pre-migrate copy, since the database is new.
*/
async function main(): Promise<void> {
const engine = resolveEngine();
if (engine.id !== 'postgres') throw new Error('build-template.ts runs on the Postgres engine only (TREK_DB_URL).');
const config = buildOrmConfig(engine);
const orm = await MikroORM.init({
...config,
// As src/db/standalone-orm.ts: the bootstrap runs SQL and seeders, which
// need no entity metadata.
entities: [],
entitiesTs: [],
discovery: { warnWhenNoEntities: false },
migrations: { ...config.migrations, silent: true },
});
try {
await engine.schemaBootstrap(orm, { snapshot: false });
process.stdout.write(`[test-db] Postgres template ready: ${engine.describe()}\n`);
} finally {
await orm.close(true);
}
}
main().catch((err: unknown) => {
process.stderr.write(
`[test-db] failed to build the Postgres template: ${err instanceof Error ? err.stack : String(err)}\n`,
);
process.exit(1);
});