mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-23 21:33:19 +00:00
fix(db): dialect helpers dispatch on the platform; lint gate for the repository layer
This commit is contained in:
@@ -1,13 +1,19 @@
|
||||
import { raw, type RawQueryFragment } from '@mikro-orm/core';
|
||||
import { raw, type Platform, type RawQueryFragment } from '@mikro-orm/core';
|
||||
import { SqlitePlatform } from '@mikro-orm/sql';
|
||||
|
||||
/**
|
||||
* The only place a repository may spell a database function.
|
||||
*
|
||||
* Every helper takes an already-qualified column reference (`'t.created_at'`)
|
||||
* and returns a fragment for the *current* dialect. The ORM exists so a
|
||||
* Postgres driver can follow; when it does, this file gets a second
|
||||
* implementation and no repository changes. Values are never interpolated —
|
||||
* the day count in `dateAdd` is checked to be an integer before it is spelled.
|
||||
* Every helper takes the MikroORM `Platform` the caller's EntityManager is
|
||||
* running on (`em.getPlatform()`) plus an already-qualified column reference
|
||||
* (`'t.created_at'`), and dispatches on that platform at runtime. TREK ships
|
||||
* one binary and the database is chosen by configuration, so the dialect is a
|
||||
* runtime property of the live connection, not a build-time swap: when the
|
||||
* Postgres driver arrives this file grows a second branch and no repository
|
||||
* changes. An unknown platform fails closed rather than guessing a spelling.
|
||||
*
|
||||
* Values are never interpolated — the day count in `dateAdd` is checked to be
|
||||
* an integer before it is spelled.
|
||||
*/
|
||||
|
||||
function column(ref: string): string {
|
||||
@@ -17,19 +23,28 @@ function column(ref: string): string {
|
||||
return ref;
|
||||
}
|
||||
|
||||
function unsupported(platform: Platform): never {
|
||||
throw new Error(`sql-functions: no implementation for platform ${platform.constructor.name}`);
|
||||
}
|
||||
|
||||
/** The calendar date (`YYYY-MM-DD`) of a timestamp column. */
|
||||
export function dateOf(ref: string): RawQueryFragment {
|
||||
return raw(`date(${column(ref)})`);
|
||||
export function dateOf(platform: Platform, ref: string): RawQueryFragment {
|
||||
if (platform instanceof SqlitePlatform) return raw(`date(${column(ref)})`);
|
||||
return unsupported(platform);
|
||||
}
|
||||
|
||||
/** The timestamp column shifted by whole days, as a calendar date. */
|
||||
export function dateAdd(ref: string, days: number): RawQueryFragment {
|
||||
export function dateAdd(platform: Platform, ref: string, days: number): RawQueryFragment {
|
||||
if (!Number.isInteger(days)) throw new Error(`sql-functions: dateAdd needs an integer day count, got ${days}`);
|
||||
const sign = days < 0 ? '-' : '+';
|
||||
return raw(`date(${column(ref)}, '${sign}${Math.abs(days)} days')`);
|
||||
if (platform instanceof SqlitePlatform) {
|
||||
const sign = days < 0 ? '-' : '+';
|
||||
return raw(`date(${column(ref)}, '${sign}${Math.abs(days)} days')`);
|
||||
}
|
||||
return unsupported(platform);
|
||||
}
|
||||
|
||||
/** The database clock, in the same text the column defaults produce. */
|
||||
export function currentTimestamp(): RawQueryFragment {
|
||||
return raw('CURRENT_TIMESTAMP');
|
||||
export function currentTimestamp(platform: Platform): RawQueryFragment {
|
||||
if (platform instanceof SqlitePlatform) return raw('CURRENT_TIMESTAMP');
|
||||
return unsupported(platform);
|
||||
}
|
||||
|
||||
@@ -56,8 +56,8 @@ export async function runSchemaBootstrap(orm: AnyOrm): Promise<void> {
|
||||
export function attachOrm(orm: AnyOrm): void {
|
||||
registerReinitializeHook(async () => {
|
||||
const connection = orm.em.getConnection();
|
||||
// The old handle is already closed by this point; better-sqlite3's close()
|
||||
// is idempotent, so letting Kysely destroy it again is harmless.
|
||||
// The handle is owned and closed by `database.ts`; the bound driver's
|
||||
// `destroy()` is a no-op (see `orm-driver.ts`), so this close never reaches it.
|
||||
await connection.close(true);
|
||||
await connection.connect();
|
||||
await runSchemaBootstrap(orm);
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { Type, type EntityProperty, type Platform, type TransformContext } from '@mikro-orm/core';
|
||||
import { Type, type EntityProperty, type Platform } from '@mikro-orm/core';
|
||||
|
||||
/** The text every DATETIME column holds and every API response emits. */
|
||||
export const DB_TIMESTAMP_RE = /^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/;
|
||||
@@ -25,7 +25,9 @@ type TimestampValue = string | null | undefined;
|
||||
* text on the way in, and text is written as text on the way out.
|
||||
*/
|
||||
export class DbTimestampType extends Type<TimestampValue, TimestampValue> {
|
||||
override convertToDatabaseValue(value: TimestampValue, platform: Platform, context?: TransformContext): TimestampValue {
|
||||
// Both converters drop the base class's `platform`/`context` parameters: the
|
||||
// wire format is the same text on every dialect, so neither is consulted.
|
||||
override convertToDatabaseValue(value: TimestampValue): TimestampValue {
|
||||
if (value == null) {
|
||||
return value;
|
||||
}
|
||||
@@ -33,14 +35,10 @@ export class DbTimestampType extends Type<TimestampValue, TimestampValue> {
|
||||
if ((value as unknown) instanceof Date) {
|
||||
return dbNow(value as unknown as Date);
|
||||
}
|
||||
if (typeof value === 'string') {
|
||||
return value;
|
||||
}
|
||||
// Default: convert to string
|
||||
return String(value);
|
||||
return value;
|
||||
}
|
||||
|
||||
override convertToJSValue(value: TimestampValue, platform: Platform, context?: TransformContext): TimestampValue {
|
||||
override convertToJSValue(value: TimestampValue): TimestampValue {
|
||||
if (value == null) {
|
||||
return value;
|
||||
}
|
||||
@@ -48,16 +46,12 @@ export class DbTimestampType extends Type<TimestampValue, TimestampValue> {
|
||||
if ((value as unknown) instanceof Date) {
|
||||
return dbNow(value as unknown as Date);
|
||||
}
|
||||
// Handle millisecond integers (legacy format)
|
||||
if (typeof value === 'number' || (value as unknown) instanceof Number) {
|
||||
const numValue = typeof value === 'number' ? value : (value as unknown as number);
|
||||
return dbNow(new Date(numValue));
|
||||
// Handle millisecond integers (legacy format) — a Date written by the stock
|
||||
// DateTimeType reads back as a number.
|
||||
if (typeof value === 'number') {
|
||||
return dbNow(new Date(value as unknown as number));
|
||||
}
|
||||
if (typeof value === 'string') {
|
||||
return value;
|
||||
}
|
||||
// Default: convert to string
|
||||
return String(value);
|
||||
return value;
|
||||
}
|
||||
|
||||
override getColumnType(prop: EntityProperty, platform: Platform): string {
|
||||
|
||||
@@ -152,8 +152,8 @@ import { APP_FILTER, APP_GUARD, APP_INTERCEPTOR, APP_PIPE } from '@nestjs/core';
|
||||
LlmParseModule,
|
||||
ManagedExtModule,
|
||||
MikroOrmModule.forRoot(mikroOrmConfig),
|
||||
// Not part of DatabaseModule: 41 e2e suites compose that module without the
|
||||
// ORM, and an EntityManager-dependent provider there fails their containers.
|
||||
// Not part of DatabaseModule: the e2e suites that compose `DatabaseModule`
|
||||
// without the ORM would fail on an EntityManager-dependent provider there.
|
||||
OrmModule,
|
||||
],
|
||||
providers: [
|
||||
|
||||
@@ -5,8 +5,9 @@ import { UnitOfWork } from './unit-of-work';
|
||||
* The ORM-side container module: the home of providers that depend on MikroORM
|
||||
* itself rather than on the raw better-sqlite3 handle `DatabaseModule` owns.
|
||||
*
|
||||
* Deliberately separate from `DatabaseModule`, which 41 e2e suites compose
|
||||
* standalone (`Test.createTestingModule({ imports: [DatabaseModule, ...] })`)
|
||||
* Deliberately separate from `DatabaseModule`, which the e2e suites that
|
||||
* compose `DatabaseModule` without the ORM build standalone
|
||||
* (`Test.createTestingModule({ imports: [DatabaseModule, ...] })`)
|
||||
* with no ORM in the container: a provider there that injects the core
|
||||
* `EntityManager` fails every one of them at `compile()`, because Nest
|
||||
* instantiates providers eagerly. Nothing is imported here — the
|
||||
|
||||
Reference in New Issue
Block a user