mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-20 02:56:10 +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,
|
||||
|
||||
@@ -8,7 +8,7 @@ import { REFUSAL } from '../../legacy-baseline';
|
||||
*/
|
||||
|
||||
/** The PostgreSQL majors TREK supports and CI runs: the oldest and the newest. */
|
||||
export const SUPPORTED_SERVER_MAJORS = { oldest: 14, newest: 18 } as const;
|
||||
const SUPPORTED_SERVER_MAJORS = { oldest: 14, newest: 18 } as const;
|
||||
|
||||
/**
|
||||
* True when the current role is a superuser or may run programs or write
|
||||
|
||||
@@ -7,7 +7,7 @@ import { accommodationIdMatches } from './_shared/reservation-accommodation';
|
||||
import { publicReservationExpr, publicStayExists, type ReservationVisibilityKyselyDB } from './_shared/reservation-visibility';
|
||||
import type { DayAssignmentRow } from './day-assignment-rows';
|
||||
import { TrekRepository } from './_shared/trek-repository';
|
||||
import { passContextQuery, type PassContextDB } from './_shared/reservation-pass-context';
|
||||
import { passContextQuery } from './_shared/reservation-pass-context';
|
||||
import { originColumnValues, type ReservationOriginColumns } from './_shared/reservation-origin-columns';
|
||||
import { idByExternalQuery, joinedReservationQuery, type ReservationJoinDB } from './_shared/reservation-joined-query';
|
||||
import type { DB } from '../kysely/db';
|
||||
@@ -350,9 +350,7 @@ type FlightsMissingEndpointsKyselyDB = Pick<DB, 'reservations' | 'reservation_en
|
||||
|
||||
export class ReservationsRepository extends TrekRepository<Reservations> {
|
||||
// For its Wallet pass (_shared/reservation-pass-context.ts).
|
||||
findPassContext(id: number, tripId: number) {
|
||||
return passContextQuery(this.kysely<PassContextDB>(), this.getEntityManager().getPlatform(), id, tripId);
|
||||
}
|
||||
findPassContext(id: number, tripId: number) { return passContextQuery(this.getEntityManager(), id, tripId); }
|
||||
|
||||
private joinedQuery() {
|
||||
return joinedReservationQuery(this.kysely<ReservationJoinDB>(), this.getEntityManager().getPlatform());
|
||||
|
||||
@@ -792,10 +792,9 @@ export class TripsRepository extends TrekRepository<Trips> {
|
||||
* FROMs has no single alias to chain a `.select()`/`.where()` off (unlike
|
||||
* `accessibleTripsQuery`'s single-FROM join), so this is Kysely
|
||||
* (`this.kysely()`, the same escape hatch `tripSelectQuery`'s own
|
||||
* docstring documents). The two literal branches are `eb.lit(1)`/`eb.lit(0)`,
|
||||
* written into the statement as integers: a bound `eb.val(1)` reaches
|
||||
* Postgres untyped, and a UNION column of untyped values comes back as
|
||||
* text ('1'), which the public API's `is_owner === 1` never matched.
|
||||
* docstring documents). The literals are `eb.lit(1)`/`eb.lit(0)`, inline integers: a bound
|
||||
* `eb.val(1)` reaches Postgres untyped, and a UNION column of untyped values comes back
|
||||
* as text ('1'), which the public API's `is_owner === 1` never matched.
|
||||
*/
|
||||
async listTravellerUsernames(trip_id: number): Promise<TravellerUsernameRow[]> {
|
||||
const owner = this.kysely<TravellerUsernameKyselyDB>()
|
||||
|
||||
@@ -2,7 +2,9 @@ import { Users } from '../entities/Users.entity';
|
||||
import { toRow, type AssertRowKeys } from './_shared/rows';
|
||||
import { coalesce, columnIncrementedBy, currentTimestamp, lower, lowerParam } from '../dialect/sql-functions';
|
||||
import { TrekRepository } from './_shared/trek-repository';
|
||||
import { uniqueViolationKey } from '../dialect/driver-errors';
|
||||
import { guardIdentity } from './_shared/user-identity';
|
||||
|
||||
export { UserIdentityTakenError } from './_shared/user-identity';
|
||||
|
||||
/**
|
||||
* The three third-party keys that belong to the instance rather than to a
|
||||
@@ -251,40 +253,6 @@ export interface NewAdminUserRow {
|
||||
role: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* A write the database refused because another account already holds that
|
||||
* email address (in any case, `idx_users_email_lower`) or username. The
|
||||
* services check first and answer 409; this is the same answer for the write
|
||||
* that lost a race against another one, instead of a 500.
|
||||
*/
|
||||
export class UserIdentityTakenError extends Error {
|
||||
constructor(readonly field: 'email' | 'username') {
|
||||
super(`${field} already taken`);
|
||||
this.name = 'UserIdentityTakenError';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The keys that name the email and the username in a unique violation: SQLite
|
||||
* names the column (`users.email`) or the index, Postgres the constraint
|
||||
* (`users_email_unique`, `users_username_unique`) or the index
|
||||
* (`idx_users_email_lower`).
|
||||
*/
|
||||
const EMAIL_KEY = /users\.email|users_email_unique|idx_users_email/;
|
||||
const USERNAME_KEY = /users\.username|users_username_unique/;
|
||||
|
||||
/** Runs a users write, turning a unique violation on email or username into {@link UserIdentityTakenError}. */
|
||||
async function guardIdentity<T>(write: () => Promise<T>): Promise<T> {
|
||||
try {
|
||||
return await write();
|
||||
} catch (err) {
|
||||
const key = uniqueViolationKey(err);
|
||||
if (key !== null && EMAIL_KEY.test(key)) throw new UserIdentityTakenError('email');
|
||||
if (key !== null && USERNAME_KEY.test(key)) throw new UserIdentityTakenError('username');
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
/** AD11 (`admin.service.ts#updateUser`, inside `uow.transactional`) — the columns the legacy COALESCE-shaped UPDATE can touch; a key the caller omits is left unchanged, the same net effect as binding NULL into `COALESCE(?, column)`. */
|
||||
export interface AdminEditPatch {
|
||||
username?: string;
|
||||
|
||||
@@ -1,11 +1,9 @@
|
||||
import type { DB } from '../../kysely/db';
|
||||
import { accommodationIdMatches } from './reservation-accommodation';
|
||||
import type { Platform } from '@mikro-orm/core';
|
||||
|
||||
import type { Kysely } from 'kysely';
|
||||
import type { SqlEntityManager } from '@mikro-orm/sql';
|
||||
|
||||
/** The tables `passContextQuery` reads. */
|
||||
export type PassContextDB = Pick<DB, 'reservations' | 'trips' | 'places' | 'day_accommodations'>;
|
||||
type PassContextDB = Pick<DB, 'reservations' | 'trips' | 'places' | 'day_accommodations'>;
|
||||
|
||||
/** What a booking's Wallet pass needs beyond the booking: where it is, and the trip's name. */
|
||||
export interface ReservationPassContext {
|
||||
@@ -19,15 +17,17 @@ export interface ReservationPassContext {
|
||||
* The trip's title and the place a booking is at (its own place, else its
|
||||
* stay's), for its Apple Wallet pass (`ReservationsRepository.findPassContext`).
|
||||
* Scoped to the trip, so a booking of another trip reads as none. Kept out of
|
||||
* the repository file, which is at its size baseline.
|
||||
* the repository file, which is at its size baseline. `em` is the repository's
|
||||
* own, which has checked the request context.
|
||||
*/
|
||||
export async function passContextQuery(
|
||||
kysely: Kysely<PassContextDB>,
|
||||
platform: Platform,
|
||||
em: SqlEntityManager,
|
||||
id: number,
|
||||
trip_id: number,
|
||||
): Promise<ReservationPassContext | undefined> {
|
||||
const row = await kysely
|
||||
const platform = em.getPlatform();
|
||||
const row = await em
|
||||
.getKysely<PassContextDB>()
|
||||
.selectFrom('reservations as r')
|
||||
.innerJoin('trips as t', 't.id', 'r.trip_id')
|
||||
.leftJoin('places as p', 'p.id', 'r.place_id')
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
import { uniqueViolationKey } from '../../dialect/driver-errors';
|
||||
|
||||
/**
|
||||
* A write the database refused because another account already holds that
|
||||
* email address (in any case, `idx_users_email_lower`) or username. The
|
||||
* services check first and answer 409; this is the same answer for the write
|
||||
* that lost a race against another one, instead of a 500.
|
||||
*/
|
||||
export class UserIdentityTakenError extends Error {
|
||||
constructor(readonly field: 'email' | 'username') {
|
||||
super(`${field} already taken`);
|
||||
this.name = 'UserIdentityTakenError';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The keys that name the email and the username in a unique violation: SQLite
|
||||
* names the column (`users.email`) or the index, Postgres the constraint
|
||||
* (`users_email_unique`, `users_username_unique`) or the index
|
||||
* (`idx_users_email_lower`).
|
||||
*/
|
||||
const EMAIL_KEY = /users\.email|users_email_unique|idx_users_email/;
|
||||
const USERNAME_KEY = /users\.username|users_username_unique/;
|
||||
|
||||
/** Runs a users write, turning a unique violation on email or username into {@link UserIdentityTakenError}. */
|
||||
export async function guardIdentity<T>(write: () => Promise<T>): Promise<T> {
|
||||
try {
|
||||
return await write();
|
||||
} catch (err) {
|
||||
const key = uniqueViolationKey(err) ?? '';
|
||||
if (EMAIL_KEY.test(key)) throw new UserIdentityTakenError('email');
|
||||
if (USERNAME_KEY.test(key)) throw new UserIdentityTakenError('username');
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
@@ -39,7 +39,7 @@ export function printsWhen(pattern: string, text: string): string {
|
||||
}
|
||||
|
||||
/** What the server says about itself beside its current schema. */
|
||||
export interface FakeServer {
|
||||
interface FakeServer {
|
||||
/** `server_version_num`; 170000 by default, which the default tool version (17.11) reaches. */
|
||||
versionNum?: string;
|
||||
/** Whether TREK's role could reach the database host. */
|
||||
|
||||
@@ -31,7 +31,6 @@ import {
|
||||
checkEntities,
|
||||
dumpEntities,
|
||||
fixDbTimestampClassFieldTypes,
|
||||
fixFlagClassFieldTypes,
|
||||
fixJsonClassFieldTypes,
|
||||
generateEntities,
|
||||
injectJoinColumns,
|
||||
@@ -46,6 +45,7 @@ import {
|
||||
writeRepositoriesIfMissing,
|
||||
type DefaultFixup,
|
||||
} from '../../../scripts/generate-entities';
|
||||
import { fixFlagClassFieldTypes } from '../../../scripts/generate-entities-flags';
|
||||
import { Users } from '../../../src/db/entities/Users.entity';
|
||||
import { UsersRepository } from '../../../src/db/repositories/Users.repository';
|
||||
import { createSnapshotTestDb } from '../../helpers/db-mock';
|
||||
@@ -207,7 +207,7 @@ describe('RULE1c_markBooleanColumns', () => {
|
||||
const plain = 'export class Addons {\n enabled?: boolean | null;\n}\n';
|
||||
expect(fixFlagClassFieldTypes(plain, fixups)).toBe(plain);
|
||||
expect(() => fixFlagClassFieldTypes('export class Addons {\n enabled?: number | null;\n}\n', fixups)).toThrow(
|
||||
/no IType<boolean, number> to simplify/,
|
||||
/typed IType<boolean, number> to simplify/,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user