fix(db): generator emits the referenced column for non-PK foreign keys; OAuth client joins through the relation; FK parity un-skipped

This commit is contained in:
jubnl
2026-09-22 21:53:34 +02:00
parent 80a0fdc4d9
commit 8ba55bda30
10 changed files with 479 additions and 145 deletions
-23
View File
@@ -1,6 +1,5 @@
import { raw, type Platform, type RawQueryFragment } from '@mikro-orm/core';
import { SqlitePlatform } from '@mikro-orm/sql';
import { sql, type Expression } from 'kysely';
/**
* The only place a repository may spell a database function.
@@ -101,25 +100,3 @@ export function lower(platform: Platform, ref: string): RawQueryFragment & symbo
return unsupported(platform);
}
/**
* `CURRENT_TIMESTAMP`, for a query built through `em.getKysely()` — the
* escape hatch for a recursive CTE (`OauthTokensRepository.collectChainIds`)
* or a join the QueryBuilder's relation metadata cannot express correctly
* (`OauthTokensRepository.listActiveByUser`/`listAllActiveWithClientAndUser`
* — `oauth_tokens.client_id` references `oauth_clients.client_id`, not its
* primary key, and the generated entity's `client` relation has no
* `referencedColumnNames` override, so a QueryBuilder `.innerJoin('ot.client',
* …)` silently joins on the wrong column; see those methods' docstrings).
* Kysely has its own expression system, entirely separate from MikroORM's
* `raw()` — `currentTimestamp` above returns a value this file's OTHER
* consumers (the QueryBuilder, `nativeUpdate`) accept, but a Kysely query
* cannot use a `RawQueryFragment` as an operand. This is the one place a
* repository may reach for the Kysely spelling of the same literal, kept
* here rather than in a repository file so `no-restricted-syntax`'s ban on
* spelling raw SQL directly in `src/db/repositories/**` still holds — this
* file is exempt from that rule by design (it IS the one approved place).
*/
export function currentTimestampKysely(platform: Platform): Expression<string> {
if (platform instanceof SqlitePlatform) return sql<string>`CURRENT_TIMESTAMP`;
return unsupported(platform);
}