diff --git a/server/scripts/size-baseline.json b/server/scripts/size-baseline.json index 013f14ac9..a21195eb3 100644 --- a/server/scripts/size-baseline.json +++ b/server/scripts/size-baseline.json @@ -2,7 +2,7 @@ "scripts/db-call-graph.mjs": 1036, "scripts/generate-entities.ts": 2259, "src/db/repositories/Places.repository.ts": 1435, - "src/db/repositories/Reservations.repository.ts": 1563, + "src/db/repositories/Reservations.repository.ts": 1554, "src/db/repositories/Trips.repository.ts": 1121, "src/db/repositories/Users.repository.ts": 1445, "src/nest/atlas/atlas.service.ts": 1177, diff --git a/server/src/db/repositories/Reservations.repository.ts b/server/src/db/repositories/Reservations.repository.ts index f4c450962..54dffbd09 100644 --- a/server/src/db/repositories/Reservations.repository.ts +++ b/server/src/db/repositories/Reservations.repository.ts @@ -5,7 +5,7 @@ import { coalesceOverride, columnRef, dayDistance } from '../dialect/sql-functio import { castIntegerKysely, concatKysely, startsWithIsoDateKysely, substringKysely } from '../dialect/kysely-functions'; import { publicReservationExpr, publicStayExists, type ReservationVisibilityKyselyDB } from './_shared/reservation-visibility'; import type { DayAssignmentRow } from './day-assignment-rows'; -import { selectReservationsWithJoins, type ReservationJoinKyselyDB, type ReservationJoinRow } from './reservation-join'; +import type { ReservationAllColumnsRow, ReservationJoinKyselyDB, ReservationJoinRow } from './reservation-rows'; import { TrekRepository } from './_shared/trek-repository'; import type { DB } from '../kysely/db'; import { ROADTRIP_RESERVATION_TYPES } from '@trek/shared'; @@ -19,8 +19,9 @@ import type { PublicApiUnscheduledReservationRow, } from './reservation-public-api-rows'; -// The joined and public API row types moved to their own modules; re-exported so callers keep one import path. +// The row types moved to their own modules; re-exported so callers keep one import path. export type { + ReservationAllColumnsRow, ReservationJoinRow, PublicApiAccommodationRow, PublicApiScheduledReservationRow, @@ -348,8 +349,29 @@ type SharePublicAccommodationKyselyDB = ReservationVisibilityKyselyDB & Pick; export class ReservationsRepository extends TrekRepository { + /** + * `accommodation_id` is TEXT and `ap.id` INTEGER, so the text side is cast + * for the join: SQLite coerces the bare comparison, Postgres refuses it. + */ private joinedQuery() { - return selectReservationsWithJoins(this.kysely(), this.getEntityManager().getPlatform()); + const platform = this.getEntityManager().getPlatform(); + return this.kysely() + .selectFrom('reservations as r') + .leftJoin('days as d', 'd.id', 'r.day_id') + .leftJoin('places as p', 'p.id', 'r.place_id') + .leftJoin('day_accommodations as ap', (join) => + join.on((eb) => eb(castIntegerKysely(platform, eb, 'r.accommodation_id'), '=', eb.ref('ap.id'))), + ) + .leftJoin('places as acc_p', 'acc_p.id', 'ap.place_id') + .selectAll('r') + .select([ + 'd.day_number as day_number', + 'p.name as place_name', + 'ap.place_id as accommodation_place_id', + 'acc_p.name as accommodation_name', + 'ap.start_day_id as accommodation_start_day_id', + 'ap.end_day_id as accommodation_end_day_id', + ]); } /** @@ -1485,34 +1507,3 @@ export interface RoadtripCarrierRow { metadata: string | null; day_plan_position: number | null; } - -/** `SELECT *` — every scalar column of `Reservations`, RS35's shape. */ -export interface ReservationAllColumnsRow { - id: number; - trip_id: number; - day_id: number | null; - end_day_id: number | null; - place_id: number | null; - assignment_id: number | null; - title: string; - accommodation_id: string | null; - reservation_time: string | null; - reservation_end_time: string | null; - location: string | null; - confirmation_number: string | null; - notes: string | null; - status: string | null; - type: string | null; - created_at: string | null; - metadata: string | null; - day_plan_position: number | null; - needs_review: number; - external_source: string | null; - external_id: string | null; - external_owner_user_id: number | null; - external_synced_at: string | null; - sync_enabled: number | null; - external_hash: string | null; - url: string | null; - ingest_state: string; -} diff --git a/server/src/db/repositories/reservation-join.ts b/server/src/db/repositories/reservation-join.ts deleted file mode 100644 index 32501b60e..000000000 --- a/server/src/db/repositories/reservation-join.ts +++ /dev/null @@ -1,71 +0,0 @@ -/** - * RS18/RR1's joined read of reservations: the row shape, the Kysely tables it - * binds and the statement. It lives beside the reservations repository so - * that file stays readable; the repository re-exports the row type, so - * callers keep importing it from there. - */ -import { castIntegerKysely } from '../dialect/kysely-functions'; -import type { DB } from '../kysely/db'; -import type { ReservationAllColumnsRow } from './Reservations.repository'; -import type { Platform } from '@mikro-orm/core'; - -import type { Kysely } from 'kysely'; - -/** - * RS18/RR1's shared joined projection — `SELECT r.*, d.day_number, p.name as - * place_name, r.assignment_id, ap.place_id as accommodation_place_id, - * acc_p.name as accommodation_name, ap.start_day_id as - * accommodation_start_day_id, ap.end_day_id as accommodation_end_day_id FROM - * reservations r LEFT JOIN days d ON r.day_id = d.id LEFT JOIN places p ON - * r.place_id = p.id LEFT JOIN day_accommodations ap ON r.accommodation_id = - * ap.id LEFT JOIN places acc_p ON ap.place_id = acc_p.id`. The service - * folds `accommodation_id` (`Math.trunc(Number(x))`), `day_positions`, - * `endpoints` and `travelers` on afterward (unchanged from the legacy split). - */ -export interface ReservationJoinRow extends ReservationAllColumnsRow { - day_number: number | null; - place_name: string | null; - accommodation_place_id: number | null; - accommodation_name: string | null; - accommodation_start_day_id: number | null; - accommodation_end_day_id: number | null; -} - -/** - * Kysely typing for the RS18/RR1 join — `reservations.accommodation_id` is a - * bare `p.text().nullable()` column with no FK to `day_accommodations` (§18.1 - * of the inventory), so the QueryBuilder's relation-path `.join()` cannot - * express `LEFT JOIN day_accommodations ap ON r.accommodation_id = ap.id` at - * all — the same escape hatch `DayAssignmentsRepository.effectiveStart`/ - * `TripsRepository.tripSelectQuery` use. The `id`/`trip_id` the callers pass - * stay `number | string` (raw-bind, D4's T5 seam) and are cast to the column - * type only at the bind, so this join keeps the legacy statement's exact - * raw-bind flexibility. - */ -export type ReservationJoinKyselyDB = Pick; - -/** - * The joined projection, without a WHERE or ORDER BY: each caller adds its - * own. `accommodation_id` is TEXT and `ap.id` INTEGER, so the text side is - * cast for the join (SQLite coerces the bare comparison, Postgres refuses it - * with 42883). - */ -export function selectReservationsWithJoins(db: Kysely, platform: Platform) { - return db - .selectFrom('reservations as r') - .leftJoin('days as d', 'd.id', 'r.day_id') - .leftJoin('places as p', 'p.id', 'r.place_id') - .leftJoin('day_accommodations as ap', (join) => - join.on((eb) => eb(castIntegerKysely(platform, eb, 'r.accommodation_id'), '=', eb.ref('ap.id'))), - ) - .leftJoin('places as acc_p', 'acc_p.id', 'ap.place_id') - .selectAll('r') - .select([ - 'd.day_number as day_number', - 'p.name as place_name', - 'ap.place_id as accommodation_place_id', - 'acc_p.name as accommodation_name', - 'ap.start_day_id as accommodation_start_day_id', - 'ap.end_day_id as accommodation_end_day_id', - ]); -} diff --git a/server/src/db/repositories/reservation-rows.ts b/server/src/db/repositories/reservation-rows.ts new file mode 100644 index 000000000..fb153f673 --- /dev/null +++ b/server/src/db/repositories/reservation-rows.ts @@ -0,0 +1,68 @@ +/** + * The rows `ReservationsRepository` answers with for a whole reservation, and + * the Kysely tables its joined read binds: what a reservation is read as, kept + * apart from the statements that read it. The repository re-exports the row + * types, so callers keep importing them from there. + */ +import type { DB } from '../kysely/db'; + +/** `SELECT *` — every scalar column of `Reservations`, RS35's shape. */ +export interface ReservationAllColumnsRow { + id: number; + trip_id: number; + day_id: number | null; + end_day_id: number | null; + place_id: number | null; + assignment_id: number | null; + title: string; + accommodation_id: string | null; + reservation_time: string | null; + reservation_end_time: string | null; + location: string | null; + confirmation_number: string | null; + notes: string | null; + status: string | null; + type: string | null; + created_at: string | null; + metadata: string | null; + day_plan_position: number | null; + needs_review: number; + external_source: string | null; + external_id: string | null; + external_owner_user_id: number | null; + external_synced_at: string | null; + sync_enabled: number | null; + external_hash: string | null; + url: string | null; + ingest_state: string; +} + +/** + * RS18/RR1's shared joined projection — `SELECT r.*, d.day_number, p.name as + * place_name, r.assignment_id, ap.place_id as accommodation_place_id, + * acc_p.name as accommodation_name, ap.start_day_id as + * accommodation_start_day_id, ap.end_day_id as accommodation_end_day_id FROM + * reservations r LEFT JOIN days d ON r.day_id = d.id LEFT JOIN places p ON + * r.place_id = p.id LEFT JOIN day_accommodations ap ON r.accommodation_id = + * ap.id LEFT JOIN places acc_p ON ap.place_id = acc_p.id`. The service + * folds `accommodation_id` (`Math.trunc(Number(x))`), `day_positions`, + * `endpoints` and `travelers` on afterward (unchanged from the legacy split). + */ +export interface ReservationJoinRow extends ReservationAllColumnsRow { + day_number: number | null; + place_name: string | null; + accommodation_place_id: number | null; + accommodation_name: string | null; + accommodation_start_day_id: number | null; + accommodation_end_day_id: number | null; +} + +/** + * The tables the RS18/RR1 join binds (`ReservationsRepository.joinedQuery`). + * `reservations.accommodation_id` is a TEXT column with no foreign key to + * `day_accommodations`, so the QueryBuilder's relation joins cannot express + * `LEFT JOIN day_accommodations ap ON r.accommodation_id = ap.id`; the join + * is written in Kysely, as `DayAssignmentsRepository.effectiveStart` and + * `TripsRepository.tripSelectQuery` do. + */ +export type ReservationJoinKyselyDB = Pick;