mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-30 00:20:07 +00:00
refactor(db): keep the SQLite-only helpers and the Postgres number parsers explicit
createStandaloneOrm builds the SQLite schema snapshot, so it now refuses a config that names another driver instead of silently building a snapshot of the wrong engine. runSchemaBootstrap takes its snapshot flag as a required argument: the reopen after a restore is the only caller that skips the copy, and an omitted flag should not be able to pick a default for it. The Postgres driver's number parsers say which values they refuse: NaN and the infinities are reported as not finite instead of as beyond 2^53, which was the wrong reason. The driver's header comment is folded into the constant docs it described.
This commit is contained in:
@@ -1,8 +1,10 @@
|
||||
import { type Configuration } from '@mikro-orm/core';
|
||||
import { PostgreSqlConnection, PostgreSqlDriver } from '@mikro-orm/postgresql';
|
||||
|
||||
/** The `pg` type id of `bigint` (`int8`): every integer column and every `count(*)` on Postgres. */
|
||||
/** `int8` (bigint), a `pg_type.oid`: every integer column and every `count(*)` on Postgres. */
|
||||
const INT8_OID = 20;
|
||||
/** `numeric`: what `sum()` of a bigint and `avg()` answer. */
|
||||
const NUMERIC_OID = 1700;
|
||||
|
||||
/**
|
||||
* Reads a `bigint` as a JavaScript number, the way better-sqlite3 hands
|
||||
@@ -17,24 +19,29 @@ export function parseInt8(text: string): number {
|
||||
return value;
|
||||
}
|
||||
|
||||
/** The `pg` type id of `numeric`: what `sum()` of a bigint and `avg()` answer. */
|
||||
const NUMERIC_OID = 1700;
|
||||
|
||||
/**
|
||||
* Reads a `numeric` as a number, as SQLite answers `sum()` and `avg()`. An
|
||||
* integral result past 2^53 is refused like a bigint; a fraction is a double,
|
||||
* the precision SQLite's REAL has.
|
||||
* Reads a `numeric` as a number, as SQLite answers `sum()` and `avg()`. `NaN`
|
||||
* and the infinities are refused, as is an integral result past 2^53, which is
|
||||
* refused like a bigint; a fraction is a double, the precision SQLite's REAL has.
|
||||
*/
|
||||
export function parseNumeric(text: string): number {
|
||||
const value = Number(text);
|
||||
if (!Number.isFinite(value) || (/^-?\d+$/.test(text) && !Number.isSafeInteger(value))) {
|
||||
if (!Number.isFinite(value)) {
|
||||
throw new Error(`A numeric of ${text} is not a finite number, which TREK does not read.`);
|
||||
}
|
||||
if (/^-?\d+$/.test(text) && !Number.isSafeInteger(value)) {
|
||||
throw new Error(`A numeric of ${text} is beyond what TREK reads exactly (2^53).`);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
/** The pool's own parsers (MikroORM keeps dates as text), plus `int8` and `numeric` as numbers. */
|
||||
class Int8NumberConnection extends PostgreSqlConnection {
|
||||
/**
|
||||
* MikroORM's `PostgreSqlConnection`, with the pool's own type parsers replaced
|
||||
* for `int8` and `numeric`. MikroORM keeps dates as text, so only these two
|
||||
* change. `mapOptions` builds the `pg` pool options, whose `types` is the
|
||||
* `pg` TypeOverrides, the object that holds the parsers.
|
||||
*/
|
||||
class NumberTypesConnection extends PostgreSqlConnection {
|
||||
override mapOptions(overrides: Parameters<PostgreSqlConnection['mapOptions']>[0]) {
|
||||
const options = super.mapOptions(overrides);
|
||||
const types = options.types as { setTypeParser(oid: number, parse: (text: string) => unknown): void };
|
||||
@@ -45,14 +52,16 @@ class Int8NumberConnection extends PostgreSqlConnection {
|
||||
}
|
||||
|
||||
/**
|
||||
* MikroORM's Postgres driver on a connection that reads `bigint` as a number.
|
||||
* The parsers belong to this pool only: nothing global in `pg` changes.
|
||||
* 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.
|
||||
*/
|
||||
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`); replaced before anything connects.
|
||||
(this as unknown as { connection: PostgreSqlConnection }).connection = new Int8NumberConnection(config);
|
||||
// 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.
|
||||
(this as unknown as { connection: PostgreSqlConnection }).connection = new NumberTypesConnection(config);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user