refactor(db): open the database through a DatabaseLifecycle provider

db/database.ts opened the connection as a side effect of being imported,
so the moment anything reached it the database was open. index.ts had to
defer loading the app until a first-boot restore was done, and nothing
in the container decided when the connection opened or closed.

The module now opens the connection on first use and opens nothing at
import. A connection closed on purpose (a restore, the shutdown) stays
closed until it is reopened, as before. The new DatabaseLifecycle
provider owns the decisions: buildApp() opens through it, which also
binds the ORM to later swaps (the hook attachOrm used to register) and
runs the schema bootstrap in the same order as before, and the shutdown
closes through it. Its file property is the one resolved database path.

The module keeps every export, so the test suites that replace it with
vi.mock keep working; buildDbMock gains the new openDb.
This commit is contained in:
Maurice
2026-10-08 15:21:05 +02:00
parent 5aa9ac8529
commit 0a4c6fbace
18 changed files with 456 additions and 96 deletions
+15 -7
View File
@@ -1,4 +1,3 @@
import { attachOrm, runSchemaBootstrap } from './db/orm';
import { applyGlobalMiddleware, routingCspOrigins } from './middleware/globalMiddleware';
import { readEnv } from './app-config';
import { nestLogLevels } from './app-config/nest-log-levels';
@@ -16,6 +15,7 @@ import { SettingsService } from './nest/settings/settings.service';
import { StorageService } from './nest/storage/storage.service';
import { MikroORM } from '@mikro-orm/core';
import { withRequestContext } from './nest/database/request-context';
import { DatabaseLifecycle } from './nest/database/database-lifecycle.service';
import type { INestApplication } from '@nestjs/common';
import type { ConfigType } from '@nestjs/config';
import { NestFactory } from '@nestjs/core';
@@ -60,6 +60,13 @@ import nodeHttp from 'node:http';
*/
let boundHttpServer: nodeHttp.Server | null = null;
/**
* The provider that owns the database connection, for index.ts to close it on
* shutdown. Re-exported so index.ts reaches it through the one module it loads
* after a first-boot restore.
*/
export { DatabaseLifecycle };
/**
* The http server buildApp created and bound the ws gateway to.
*
@@ -82,13 +89,14 @@ export async function buildApp(): Promise<INestApplication> {
rawBody: true,
logger: nestLogLevels(readEnv().app.logLevel),
});
// Schema first, before ANY consumer reads it. `database.ts` only opens the
// connection now; migrating and seeding is MikroORM's job and it is async, so
// this is the earliest point it can happen. It has to stay above the
// SettingsService resolution below, which is the boot's first DB read.
// Schema first, before ANY consumer reads it. The connection lifecycle is the
// DatabaseLifecycle provider's: it opens the connection (the ORM's first
// connect usually already has), binds the ORM to later swaps, and runs the
// legacy baseline, the migrations and the seeders. That is async, so this is
// the earliest point it can happen. It has to stay above the SettingsService
// resolution below, which is the boot's first DB read.
const orm = app.get(MikroORM);
attachOrm(orm);
await runSchemaBootstrap(orm);
await app.get(DatabaseLifecycle).open();
const instance = app.getHttpAdapter().getInstance();
// The http server is created HERE, not by the caller after buildApp returns,
// and that ordering is the whole point: Nest binds gateways during app.init(),