mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-27 07:07:31 +00:00
7f6f2eb97c
toRowId moves to src/db/row-id.ts so repositories can use it (src/db sits below src/nest), together with a new withRowId helper; src/nest/common/row-id.ts re-exports both. Its docstring is rewritten to state the rule plainly. TripsRepository read its number | string trip ids raw: SQLite matched '0x1' against nothing, Postgres 14 and 15 refused the cast with a 500, and 16 and later read it as trip 1. Its access reads, DaysRepository.existsInTrip and the packing template lookups now parse the id first, so an id toRowId refuses is no row on every engine. TripAccessGuard, TripOwnerGuard and JourneyAssistantGuard used Number(), which read '0x1' and '1e0' as row 1; they now use toRowId.
38 lines
1.7 KiB
TypeScript
38 lines
1.7 KiB
TypeScript
// The rule itself lives in the data layer, so a repository that still takes a
|
|
// request's string id reads it the same way (src/db sits below src/nest).
|
|
import { toRowId, withRowId } from '../../db/row-id';
|
|
|
|
export { toRowId, withRowId };
|
|
|
|
/**
|
|
* The text the legacy raw-SQL layer stored for an `accommodation_id` bound
|
|
* as a plain JS number. `better-sqlite3` binds every JS `number` as a SQLite
|
|
* REAL, whole or not (`typeof(?)` reports 'real'), and SQLite stores a
|
|
* numeric value written into a TEXT column as its text form, so `14` was
|
|
* stored as `'14.0'`, never `'14'`. `String(id)` gives `'14'`, a different
|
|
* stored shape, and a read held to the legacy compare (`reservationStayIs`,
|
|
* which `restampLinkedReservation` uses) would miss every row written that
|
|
* way.
|
|
*
|
|
* Use this wherever a `number` id is written into the `accommodation_id`
|
|
* TEXT column, so the stored text matches what the legacy statement stored
|
|
* for the same input; never `String(id)` for that column. The reads go
|
|
* through src/db/repositories/_shared/reservation-accommodation.ts, which
|
|
* compares this column the same way on both engines; this helper is for
|
|
* writes only.
|
|
*/
|
|
export function legacyBoundIntegerText(id: number): string {
|
|
return `${id}.0`;
|
|
}
|
|
|
|
/**
|
|
* An id variable of an MCP resource URI (`trek://trips/{tripId}/...`), one
|
|
* value or the first of a list, as a positive row id, or null. Read through
|
|
* toRowId: Number() took '0x1' and '1e0' for 1 and passed 1e20 to Postgres,
|
|
* which refuses it as out of range for bigint.
|
|
*/
|
|
export function uriRowId(value: string | string[]): number | null {
|
|
const id = toRowId(Array.isArray(value) ? value[0] : value);
|
|
return id !== null && id > 0 ? id : null;
|
|
}
|