fix(db): sort NULLs where SQLite sorts them on Postgres

SQLite takes NULL as the smallest value and Postgres as the largest, so any
list ordered by a nullable column (bookings without a time, trips without
dates) came back in another order on Postgres. Instead of every ORDER BY
spelling its NULL position, the Postgres engine renders SQLite's wherever a
statement names none: a Kysely query compiler for this.kysely() statements
and a MikroORM platform for find() and the query builder. An explicit
position is kept.

A new Postgres-lane suite checks the answers the probe cannot see: the NULL
order, a stay linked as '14.0' in the public reads, the traveller owner
flag, the identity errors for a lost race, and the plugin caps under a race
on the pool.
This commit is contained in:
Maurice
2026-11-17 12:12:55 +01:00
parent 60642cc18b
commit c3f6177be1
4 changed files with 288 additions and 5 deletions
@@ -0,0 +1,68 @@
import { PostgreSqlPlatform } from '@mikro-orm/postgresql';
import {
PostgresQueryCompiler,
RawNode,
type DatabaseIntrospector,
type Dialect,
type Kysely,
type OperationNode,
type OrderByItemNode,
} from 'kysely';
/**
* Where NULLs sort, as SQLite sorts them, for every ORDER BY TREK sends to
* Postgres.
*
* SQLite takes NULL as the smallest value: first in ascending order, last in
* descending order. Postgres takes it as the largest, the other way round, so
* a list ordered by a nullable column (bookings without a time, trips without
* dates, items without a position) would come back in another order on each
* engine. Rather than every ORDER BY spelling its NULL position, which a new
* one would forget, the Postgres engine renders the position SQLite implies
* wherever a statement does not name one: Kysely statements through the query
* compiler below, MikroORM's own (`find`, the query builder) through the
* platform's ORDER BY rendering. An explicit `nulls first`/`nulls last` is
* left as written. ORDER BY spelled inside raw SQL text is outside both.
*/
/** The NULL position SQLite gives an ORDER BY item of this direction. */
function sqliteNulls(descending: boolean): 'first' | 'last' {
return descending ? 'last' : 'first';
}
/** Whether a Kysely ORDER BY item's direction is descending (`desc`); no direction is ascending. */
function isDescending(direction: OperationNode | undefined): boolean {
if (!direction || !RawNode.is(direction)) return false;
return direction.sqlFragments.join('').trim().toLowerCase() === 'desc';
}
/** Postgres's query compiler, with SQLite's NULL position on every ORDER BY item that names none. */
export class SqliteNullOrderCompiler extends PostgresQueryCompiler {
protected override visitOrderByItem(node: OrderByItemNode): void {
super.visitOrderByItem(node.nulls ? node : { ...node, nulls: sqliteNulls(isDescending(node.direction)) });
}
}
/** `dialect` with {@link SqliteNullOrderCompiler} as its query compiler; the rest is its own. */
export function withSqliteNullOrder(dialect: Dialect): Dialect {
return {
createAdapter: () => dialect.createAdapter(),
createDriver: () => dialect.createDriver(),
createIntrospector: (db: Kysely<unknown>): DatabaseIntrospector => dialect.createIntrospector(db),
createQueryCompiler: () => new SqliteNullOrderCompiler(),
};
}
/** One rendered MikroORM ORDER BY item (`"t"."start_date" desc`), with SQLite's NULL position when it names none. */
export function withSqliteNullPosition(item: string): string {
if (/\snulls\s+(first|last)\s*$/i.test(item)) return item;
return `${item} nulls ${sqliteNulls(/\sdesc\s*$/i.test(item))}`;
}
/** MikroORM's Postgres platform, rendering ORDER BY items with SQLite's NULL position. */
export class SqliteNullOrderPlatform extends PostgreSqlPlatform {
override getOrderByExpression(column: string, direction: string, collation?: string): string[] {
return super.getOrderByExpression(column, direction, collation).map(withSqliteNullPosition);
}
}
@@ -1,3 +1,4 @@
import { SqliteNullOrderPlatform, withSqliteNullOrder } from './null-order';
import { type Configuration } from '@mikro-orm/core';
import { PostgreSqlConnection, PostgreSqlDriver } from '@mikro-orm/postgresql';
@@ -49,19 +50,29 @@ class NumberTypesConnection extends PostgreSqlConnection {
types.setTypeParser(NUMERIC_OID, parseNumeric);
return options;
}
/** The pool's Kysely dialect, compiling every ORDER BY with SQLite's NULL position (`null-order.ts`). */
override createKyselyDialect(overrides: Parameters<PostgreSqlConnection['createKyselyDialect']>[0]) {
return withSqliteNullOrder(super.createKyselyDialect(overrides)) as ReturnType<
PostgreSqlConnection['createKyselyDialect']
>;
}
}
/**
* MikroORM's Postgres driver on a connection that reads `bigint` and `numeric`
* as numbers. The parsers belong to this pool only: nothing global in `pg`
* changes.
* as numbers, and on a platform and a query compiler that sort NULLs where
* SQLite does (`null-order.ts`). The parsers belong to this pool only:
* nothing global in `pg` changes.
*/
export class TrekPostgreSqlDriver extends PostgreSqlDriver {
constructor(config: Configuration) {
super(config);
// PostgreSqlDriver's constructor hardcodes its connection class, as
// SqliteDriver's does (`db/orm-driver.ts`). It keeps the connection in a
// field, which is not public, so it is replaced here, before anything connects.
// PostgreSqlDriver's constructor hardcodes its connection and platform
// classes, as SqliteDriver's does (`db/orm-driver.ts`). It keeps both in
// fields, which are not public, so they are replaced here, before anything
// connects and before the configuration reads the platform.
(this as unknown as { connection: PostgreSqlConnection }).connection = new NumberTypesConnection(config);
(this as unknown as { platform: SqliteNullOrderPlatform }).platform = new SqliteNullOrderPlatform();
}
}