Files
TREK/server/tests/unit/nest/database/database-engine.module.test.ts
T
Isaias Tavares 98533f3dbe 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.
2026-11-05 01:04:21 -03:00

30 lines
1.1 KiB
TypeScript

/**
* DatabaseEngineModule: one engine per container, shared by every injector.
*/
import {
DatabaseEngineModule,
DATABASE_ENGINE,
DATABASE_SETTINGS,
} from '../../../../src/nest/database/database-engine.module';
import { Test } from '@nestjs/testing';
import { describe, expect, it } from 'vitest';
describe('DatabaseEngineModule', () => {
it('ENGINE-DI-001: provides one engine instance, so state an engine keeps is the same wherever it is injected', async () => {
const moduleRef = await Test.createTestingModule({ imports: [DatabaseEngineModule] }).compile();
const engine = moduleRef.get(DATABASE_ENGINE);
expect(moduleRef.get(DATABASE_ENGINE)).toBe(engine);
expect(engine.id).toBe('sqlite');
});
it('ENGINE-DI-002: the settings are read from the environment once and the engine is resolved from them', async () => {
const moduleRef = await Test.createTestingModule({ imports: [DatabaseEngineModule] }).compile();
const settings = moduleRef.get(DATABASE_SETTINGS);
expect(settings.engine).toBe('sqlite');
expect(moduleRef.get(DATABASE_SETTINGS)).toBe(settings);
});
});