The most complete open-source travel planner: real-time collaboration, bookings, budgets, packing lists, journals, offline PWA, SSO and an AI-ready MCP server, with planet-wide place search and routing built in.
TypeScript budget-trackercollaborativedockeritinerarymcp-serveroffline-firstopen-sourceopenstreetmappacking-listpwareal-timeself-hostedselfhostedtraveltravel-apptravel-journaltravel-plannertripitwanderlog
import type { PackingTemplateItems } from '../entities/PackingTemplateItems.entity';
import type { DB } from '../kysely/db';
import { toRowId } from '../row-id';
import { type AssertRowKeys } from './_shared/rows';
import { TrekRepository } from './_shared/trek-repository';
/** A bare `packing_template_items` row — every scalar column, incl. the `persist(false)` `category_id` relation mirror. */
export interface PackingTemplateItemRow {
id: number;
category_id: number;
name: string;
sort_order: number;
weight_grams: number | null;
quantity: number;
bag_name: string | null;
}
const _packingTemplateItemRowKeys: AssertRowKeys<PackingTemplateItemRow, PackingTemplateItems> = true;
/** PK50's apply-template projection (`applyTemplate`). */
export interface PackingTemplateApplyRow {
name: string;
category: string;
weight_grams: number | null;
quantity: number;
bag_name: string | null;
}
type PackingTemplateItemsKyselyDB = Pick<DB, 'packing_template_items' | 'packing_template_categories'>;
/** The table `insertTemplateItem` (PK57/PK86) inserts into; `id` is the rowid and `InsertOptional` in the generated type. */
type PackingTemplateItemsInsertKyselyDB = Pick<DB, 'packing_template_items'>;
/** `packing_template_items` — the leaf tier of the packing template tree. Kysely throughout: `category_id` is `persist(false)`. */
export class PackingTemplateItemsRepository extends TrekRepository<PackingTemplateItems> {
private db() {
return this.kysely<PackingTemplateItemsKyselyDB>();
}
/**
* PK50 (`applyTemplate`) — `SELECT ti.name, tc.name as category,
* ti.weight_grams, ti.quantity, ti.bag_name FROM
* packing_template_items ti JOIN packing_template_categories tc ON
* ti.category_id = tc.id WHERE tc.template_id = ? ORDER BY tc.sort_order,
* ti.sort_order`. `template_id: number` (Plan 4 Task 8b, U6 — the
* program's gate-level id parsing carry: its one caller, `applyTemplate`,
* is only reached with a `toRowId`-parsed/Zod-typed id).
*/
async listForApply(template_id: number): Promise<PackingTemplateApplyRow[]> {
return await this.db()
.selectFrom('packing_template_items as ti')
.innerJoin('packing_template_categories as tc', 'tc.id', 'ti.category_id')
.select(['ti.name', 'tc.name as category', 'ti.weight_grams', 'ti.quantity', 'ti.bag_name'])
.where('tc.template_id', '=', template_id)
.orderBy('tc.sort_order', 'asc')
.orderBy('ti.sort_order', 'asc')
.execute();
}
/**
* PK67 (`getPackingTemplate`) — `SELECT ti.* FROM packing_template_items
* ti JOIN packing_template_categories tc ON ti.category_id = tc.id WHERE
* tc.template_id = ? ORDER BY ti.sort_order, ti.id`.
*/
async listForTemplate(template_id: number | string): Promise<PackingTemplateItemRow[]> {
return await this.db()
.selectFrom('packing_template_items as ti')
.innerJoin('packing_template_categories as tc', 'tc.id', 'ti.category_id')
.selectAll('ti')
.where('tc.template_id', '=', template_id as number)
.orderBy('ti.sort_order', 'asc')
.orderBy('ti.id', 'asc')
.execute();
}
/**
* PK57 (`saveAsTemplate`'s per-item loop)/PK86 (`createTemplateItem`) —
* `INSERT INTO packing_template_items (category_id, name, sort_order)
* VALUES (?, ?, ?)` (PK86), and since #1131 PK57's `INSERT INTO
* packing_template_items (category_id, name, sort_order, weight_grams,
* quantity, bag_name) VALUES (?, ?, ?, ?, ?, ?)`. One method, both call
* sites; the optional keys PK86 leaves out stay `undefined`, which
* Kysely drops from the column list, so they take their SQL defaults.
* Returns the new row's id. Named `insertTemplateItem`, not `insert` —
* the latter collides with `TrekRepository`/`EntityRepository`'s own
* same-named method.
*/
async insertTemplateItem(row: {
category_id: number | string;
name: string;
sort_order: number;
weight_grams?: number | null;
quantity?: number;
bag_name?: string | null;
}): Promise<number> {
const inserted = await this.kysely<PackingTemplateItemsInsertKyselyDB>()
.insertInto('packing_template_items')
.values({
category_id: row.category_id as number,
name: row.name,
sort_order: row.sort_order,
weight_grams: row.weight_grams,
quantity: row.quantity,
bag_name: row.bag_name,
})
.returning('id')
.executeTakeFirstOrThrow();
return inserted.id;
}
/** PK85 (`createTemplateItem`) — `SELECT MAX(sort_order) as max FROM packing_template_items WHERE category_id = ?`. */
async maxSortOrder(category_id: number | string): Promise<number | null> {
const row = await this.db()
.selectFrom('packing_template_items')
.select((eb) => eb.fn.max('sort_order').as('max'))
.where('category_id', '=', category_id as number)
.executeTakeFirst();
return row?.max ?? null;
}
/** PK87/PK89 — `SELECT * FROM packing_template_items WHERE id = ?`, same text at two call sites (`createTemplateItem`'s re-select, `updateTemplateItem`'s re-select). */
async findById(id: number | string): Promise<PackingTemplateItemRow | undefined> {
return await this.db()
.selectFrom('packing_template_items')
.selectAll()
.where('id', '=', id as number)
.executeTakeFirst();
}
/**
* PK63 (`scopedTemplateItem`) — `SELECT ti.* FROM packing_template_items
* ti JOIN packing_template_categories tc ON ti.category_id = tc.id WHERE
* ti.id = ? AND tc.template_id = ?`, the template-scoping guard shared by
* `updateTemplateItem` and `deleteTemplateItem` (two call sites).
*/
async findScoped(id: number | string, template_id: number | string): Promise<PackingTemplateItemRow | undefined> {
// Route ids: a spelling toRowId refuses is no item, on every engine.
const itemId = toRowId(id);
const templateId = toRowId(template_id);
if (itemId === null || templateId === null) return undefined;
return await this.db()
.selectFrom('packing_template_items as ti')
.innerJoin('packing_template_categories as tc', 'tc.id', 'ti.category_id')
.selectAll('ti')
.where('ti.id', '=', itemId)
.where('tc.template_id', '=', templateId)
.executeTakeFirst();
}
/** PK88 (`updateTemplateItem`) — `UPDATE packing_template_items SET name = ? WHERE id = ?`. */
async updateName(id: number | string, name: string): Promise<void> {
await this.db()
.updateTable('packing_template_items')
.set({ name })
.where('id', '=', id as number)
.execute();
}
/** PK90 (`deleteTemplateItem`) — `DELETE FROM packing_template_items WHERE id = ?`. */
async delete(id: number | string): Promise<void> {
await this.nativeDelete({ id: id as number });
}
}