mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-10-18 21:16:47 +00:00
724c88c201
A mechanical prettier --write over server/src, server/tests, server/scripts and shared/src with the workspaces' .prettierrc; nothing but layout and import order changes, and every file keeps the same set of imports. shared is formatted throughout and its format baseline is empty. The server baseline keeps the 35 files that Prettier would grow past their lint:size limit (they need a split first, which is not a formatting change), and the generated entity files stay as their generator writes them.
38 lines
1.4 KiB
TypeScript
38 lines
1.4 KiB
TypeScript
import { withRequestContext } from '../../../src/nest/database/request-context';
|
|
import type { EntityManager } from '@mikro-orm/core';
|
|
|
|
/**
|
|
* Anything carrying the ORM's global EntityManager: `app.get(MikroORM)` in a
|
|
* `buildApp()` suite, the `TestOrm` from `createTestOrm(db)` in a unit or
|
|
* partial-module suite, or a `MikroORM` a test built itself.
|
|
*/
|
|
export interface FactoryOrm {
|
|
em: EntityManager;
|
|
}
|
|
|
|
/**
|
|
* Runs one factory step in a request context of its own, the way production
|
|
* runs an MCP call or a cron tick (`withRequestContext`).
|
|
*
|
|
* The app's ORM keeps `allowGlobalContext: false`, so a write outside a
|
|
* context would be refused; and a fresh fork per step means no identity map
|
|
* survives into the next step, where `resetTestDb()` may already have deleted
|
|
* the rows it remembers. Inside the context `orm.em` resolves to that fork.
|
|
*/
|
|
export function inContext<T>(orm: FactoryOrm, fn: (em: EntityManager) => Promise<T>): Promise<T> {
|
|
return withRequestContext(orm, () => fn(orm.em));
|
|
}
|
|
|
|
const sequences = new Map<string, number>();
|
|
|
|
/**
|
|
* The next number for a named default (`user3@test.example.com`). Per worker
|
|
* process, never reset: like the legacy factories, names stay unique across
|
|
* the tests of one file even when `resetTestDb()` runs between them.
|
|
*/
|
|
export function nextSeq(name: string): number {
|
|
const n = (sequences.get(name) ?? 0) + 1;
|
|
sequences.set(name, n);
|
|
return n;
|
|
}
|