mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-20 11:06:06 +00:00
refactor(db): keep the touched files within their size baselines
The users identity guard moves to repositories/_shared/user-identity.ts (Users.repository.ts re-exports UserIdentityTakenError), the flag text pass of the entity generator to scripts/generate-entities-flags.ts, and the Wallet pass query takes the repository's EntityManager. The size baseline of Users.repository.ts is lowered to what it holds now (1445 to 1421); a few exports only their own file used are no longer exported.
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
/**
|
||||
* The text pass for Rule 1c of `generate-entities.ts` (the flag columns in
|
||||
* `BOOLEAN_COLUMNS`, rendered as `p.type(IntegerFlagType)`), kept in a file of
|
||||
* its own because the generator is at its size baseline.
|
||||
*
|
||||
* The renderer gives such a column's class field the type
|
||||
* `IType<boolean, number>`: it sees a boolean runtime type over a number
|
||||
* column and marks it ambiguous. The entity's field is a plain boolean
|
||||
* (`IntegerFlagType` converts on the way in and out), so the wrapper is taken
|
||||
* off here, as `fixJsonClassFieldTypes` does for a JSON column. A field that
|
||||
* already reads `boolean` is left alone; any other shape fails, naming it.
|
||||
*/
|
||||
|
||||
/** A property Rule 1c retyped: its entity class and its name. */
|
||||
interface FlagFixup {
|
||||
className: string;
|
||||
propName: string;
|
||||
}
|
||||
|
||||
export function fixFlagClassFieldTypes(source: string, fixups: readonly FlagFixup[]): string {
|
||||
let result = source;
|
||||
for (const { className, propName } of fixups) {
|
||||
const name = propName.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
||||
const field = new RegExp(`^([ \\t]*${name}[?!]?: )(?:IType<boolean\\s*,\\s*number>|boolean)(.*)$`, 'm');
|
||||
const match = field.exec(result);
|
||||
if (!match) {
|
||||
throw new Error(
|
||||
`generate-entities: fixFlagClassFieldTypes found no class field for "${className}.${propName}" ` +
|
||||
'typed IType<boolean, number> to simplify; check the renderer shape.',
|
||||
);
|
||||
}
|
||||
result = result.slice(0, match.index) + `${match[1]}boolean${match[2]}` + result.slice(match.index + match[0].length);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
@@ -63,6 +63,7 @@ import {
|
||||
type ImportsResolver,
|
||||
type Platform,
|
||||
} from '@mikro-orm/core';
|
||||
import { fixFlagClassFieldTypes } from './generate-entities-flags';
|
||||
import { EntityGenerator } from '@mikro-orm/entity-generator';
|
||||
import { Migrator } from '@mikro-orm/migrations';
|
||||
import { MikroORM as SqliteMikroORM } from '@mikro-orm/sqlite';
|
||||
@@ -292,22 +293,17 @@ export function RULE1b_markJsonColumns(metadata: EntityMetadata[]): JsonColumnFi
|
||||
return fixups;
|
||||
}
|
||||
|
||||
/** Rule 1c (metadata level): mark known boolean columns so they render `p.type(IntegerFlagType)` (see BOOLEAN_COLUMNS above). */
|
||||
/** Rule 1c (metadata level): mark known boolean columns so they render `p.type(IntegerFlagType)` (BOOLEAN_COLUMNS). */
|
||||
export function RULE1c_markBooleanColumns(metadata: EntityMetadata[]): RetypedScalarFixup[] {
|
||||
const fixups: RetypedScalarFixup[] = [];
|
||||
for (const meta of metadata) {
|
||||
for (const prop of Object.values(meta.properties)) {
|
||||
if (prop.kind !== undefined && prop.kind !== ReferenceKind.SCALAR) continue;
|
||||
if (!BOOLEAN_COLUMNS.has(`${meta.tableName}.${prop.fieldNames[0]}`)) continue;
|
||||
// `IntegerFlagType` (src/db/types), not `p.boolean()`: MikroORM's own
|
||||
// boolean declares a `boolean` column on Postgres and writes true/false,
|
||||
// where every other flag, and this one on SQLite, is the integer 0/1.
|
||||
// `IntegerFlagType` (src/db/types) stores 0/1 on both engines; `p.boolean()` is a boolean column on Postgres.
|
||||
// The renderer wraps the class field in `IType<boolean, number>` and adds `.columnType('INTEGER')`;
|
||||
// `fixFlagClassFieldTypes` and `stripRedundantColumnType` undo both.
|
||||
prop.type = 'IntegerFlagType';
|
||||
// `runtimeType` moves with `type`: the class field is a boolean. The
|
||||
// renderer still wraps it in `IType<boolean, number>` (the column's own
|
||||
// type is a number) and adds a `.columnType('INTEGER')`; the fixups
|
||||
// returned here drive the text passes that undo both
|
||||
// (`fixFlagClassFieldTypes`, `stripRedundantColumnType`).
|
||||
prop.runtimeType = 'boolean';
|
||||
// The introspected DB default is always the raw SQLite value (`0`/`1`,
|
||||
// a number, since SQLite has no boolean storage class either) —
|
||||
@@ -1667,33 +1663,6 @@ export function fixJsonClassFieldTypes(source: string, fixups: readonly JsonColu
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* A Rule 1c flag's class field renders as `IType<boolean, number>`: the
|
||||
* renderer sees a boolean runtime type over a number column and marks it
|
||||
* ambiguous. The entity's field is a plain boolean (`IntegerFlagType` does the
|
||||
* conversion), so the wrapper is taken off here, as `fixJsonClassFieldTypes`
|
||||
* does for a JSON column.
|
||||
*/
|
||||
export function fixFlagClassFieldTypes(source: string, fixups: readonly RetypedScalarFixup[]): string {
|
||||
let result = source;
|
||||
for (const { className, propName } of fixups) {
|
||||
const match = findClassFieldLine(result, propName);
|
||||
if (!match) {
|
||||
throw new Error(`generate-entities: fixFlagClassFieldTypes could not find the class field line for "${className}.${propName}".`);
|
||||
}
|
||||
const line = match[1];
|
||||
const newLine = line.replace(/IType<boolean\s*,\s*number>/, 'boolean');
|
||||
if (newLine === line) {
|
||||
if (/:\s*boolean\b/.test(line)) continue; // already the plain type
|
||||
throw new Error(
|
||||
`generate-entities: fixFlagClassFieldTypes found the class field for "${className}.${propName}" but no IType<boolean, number> to simplify — check the renderer shape.`,
|
||||
);
|
||||
}
|
||||
result = result.slice(0, match.index) + newLine + result.slice(match.index + line.length);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Inserts the `JSON_INTERFACES` declaration for every distinct type name this
|
||||
* file's JSON columns reference, right before `export class <Name>` — the
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
"src/db/repositories/Places.repository.ts": 1435,
|
||||
"src/db/repositories/Reservations.repository.ts": 1551,
|
||||
"src/db/repositories/Trips.repository.ts": 1120,
|
||||
"src/db/repositories/Users.repository.ts": 1445,
|
||||
"src/db/repositories/Users.repository.ts": 1421,
|
||||
"src/nest/atlas/atlas.service.ts": 1174,
|
||||
"src/nest/auth/auth.service.ts": 1143,
|
||||
"src/nest/budget/budget.service.ts": 1607,
|
||||
|
||||
Reference in New Issue
Block a user