A tour can now point at the packing list of its trip, as a whole or at one of its categories (say "Hiking gear" for a hike), and shows it in its detail. The new Packing list section reads the list live as the user may see it: the backpack tile with the item count on its corner and the category's own colour, how many are packed with a thin progress bar, the first items ticked off or not with their quantity, how many more there are, and a button that opens the trip's Lists tab. Who may change the tour links one, changes it or lets go through a dialog that lists the whole list and every category with its colour and item count, and writes only on its confirm. It is a reference, never a copy: the items stay in Packing, are ticked off and edited there, and each traveller sees in the tour only the items Packing shows them, so an item kept to oneself never shows through a tour. A category that is renamed, cleared or only holds items of others reads as missing, an empty one as empty, a link that could not be read says so with a retry, and while Lists are switched off the link stays, dimmed, and can still be let go. Removing the link, or the tour, leaves the list and every item as they are. Nothing suggests or changes an item by itself. On the server the link is a row of its own in the tours domain (tour_packing_links, migration 046110), one per tour and gone with it. GET, PUT and DELETE /api/trips/:tripId/tours/:placeId/packing-link read it with the trip and change it with place_edit; linking needs the Lists addon and, for a category, an item of it the user can see. The writes answer the tour with its link and tell the trip through tours:changed, so the offline client queues them as writes of the tour and replays them like one. A copied trip keeps its tours' links, a tour saved as new or taken over does not. The MCP tools get_tour_packing, link_tour_packing and unlink_tour_packing run over the same service and checks; the read also hands the tour's facts as context for suggesting what to pack, which only becomes an item through the packing tools. The empty category placeholder now lives in @trek/shared so both sides leave it out alike. All 27 locales carry the new strings, and the wiki describes the link and the tools.
Test factories over the ORM
The server tests used to seed and read their database with raw SQLite on the
synchronous better-sqlite3 handle (testDb.prepare(...).run(...)): thousands of
hand-written statements that skip the repositories, restate the schema by hand
and run on no other engine. The helpers in this folder do the same work through
MikroORM, the way the app does, and are what new fixtures use.
npm run lint:test-sql holds the raw statements that are left to a shrinking
baseline (see Ratchet).
Getting an ORM handle
Every helper takes a FactoryOrm (anything with the ORM's global em) as its
first argument and runs in a request context of its own, so the same call works
in every kind of suite:
| Suite | Handle |
|---|---|
buildApp() integration test |
const orm = nestApp.get(MikroORM) (from @mikro-orm/core) after the app is built |
partial-module e2e (Test.createTestingModule) |
const orm = await createTestOrm(db) from ../helpers/test-orm, closed with await orm.close() |
unit test over createSnapshotTestDb() |
createTestOrm(testDb), as above |
The handle must sit on the migrated schema (createSnapshotTestDb()): an
entity reads and writes every mapped column, so a hand-rolled CREATE TABLE
with a few columns is not enough. A suite that still builds its own tables
switches to the snapshot when it converts (tests/e2e/tags.e2e.test.ts is the
example). resetTestDb(testDb) between tests keeps working: each helper call
opens a fresh context, so no identity map remembers a row the reset deleted.
What is here
One file per domain, each returning the row as the app reads it (EntityDTO
of the entity, the SELECT * shape with the *_id columns filled in):
| File | Helpers |
|---|---|
users.ts |
makeUser, makeAdmin, makeUserWithMfa, readUser, setImmichCredentials, setSynologyCredentials |
trips.ts |
makeTrip (with one day per date), makeDay, readTripDays, addTripMember, makeShareToken |
places.ts |
makePlace, makeCategory, makeTag, tagPlace |
itinerary.ts |
makeDayAssignment, makeDayNote, makeDayAccommodation |
reservations.ts |
makeReservation, makeReservationEndpoint, addReservationTraveler |
budget.ts |
makeBudgetItem, addBudgetItemMember, addBudgetItemPayer |
packing.ts |
makePackingItem, makePackingBag, addPackingBagMembers, addPackingItemRecipients |
todos.ts |
makeTodoItem |
files.ts |
makeTripFile, linkFile |
journeys.ts |
makeJourney (owner added as contributor), makeJourneyEntry, addJourneyContributor, linkTripToJourney |
tours.ts |
makeTour, addTourWaypoints (with a city walk's station columns), markStationVisit |
collections.ts |
makeCollection, addCollectionMember, makeCollectionPlace, tagCollectionPlace, labelCollectionPlace |
notifications.ts |
makeNotification, setNotificationChannels, disableNotificationPref |
settings.ts |
setAppSetting, readAppSetting, setUserSetting, readUserSetting, setAddonEnabled, setCollabFeature |
plugins.ts |
makePlugin, makePluginSettingsField, setPluginUserConfig |
tokens.ts |
makeMcpToken, makeOauthClient, makeInviteToken |
atlas.ts |
makeBucketListItem, markCountryVisited, markRegionVisited |
vacay.ts |
makeVacayPlan, addVacayPlanMember, makeVacayEntry |
photos.ts |
addTripPhoto, addAlbumLink |
collab.ts |
makeCollabNote, makeCollabMessage |
Each make* has defaults that make a valid row on its own and takes an
overrides object of the entity's columns for everything else. Pivot tables
without an entity of their own (place_tags, packing_bag_members, …) are
written through the repository method the app uses for them.
rows.ts holds the generic building blocks for any table, with or without a
dedicated factory:
| Helper | Replaces |
|---|---|
createRow(orm, Entity, data) |
INSERT + SELECT * WHERE id = lastInsertRowid |
insertRow(orm, Entity, data) / insertRows |
a bare INSERT (returns the primary key) |
insertRowIgnoringConflict(orm, Entity, data) |
INSERT OR IGNORE |
upsertRow(orm, Entity, data, mergeFields?) |
INSERT OR REPLACE, ON CONFLICT DO UPDATE |
findRow(orm, Entity, where) |
SELECT * ... LIMIT 1 (null, not undefined, when there is none) |
findRows(orm, Entity, where, orderBy?) |
SELECT * ... ORDER BY |
countRows(orm, Entity, where) |
SELECT COUNT(*) |
updateRows(orm, Entity, where, data) |
UPDATE ... SET (returns the affected count) |
deleteRows(orm, Entity, where) |
DELETE FROM (all rows for {}) |
Conversion guide
Write relations by their relation name
Most foreign keys appear twice on an entity: the relation (trip, user,
createdByRef), which is written, and its trip_id twin, which is read only.
Write { trip: tripId }, read row.trip_id. The helpers throw on a write to a
read-only twin and name the relation to use, because MikroORM would otherwise
drop the value without a word.
Insert a row
// before
const res = testDb.prepare('INSERT INTO tags (user_id, name, color) VALUES (?, ?, ?)').run(userId, 'Beach', '#ff0000');
const id = Number(res.lastInsertRowid);
// after
const { id } = await makeTag(orm, userId, { name: 'Beach', color: '#ff0000' });
// or, for a table without a factory
const row = await createRow(orm, Tags, { user: userId, name: 'Beach', color: '#ff0000' });
The legacy createUser(testDb) / createTrip(testDb, …) from
tests/helpers/factories.ts map one to one onto await makeUser(orm) /
await makeTrip(orm, …); the returned rows carry the same columns.
Read a row
// before
const row = testDb.prepare('SELECT * FROM tags WHERE id = ?').get(id) as { name: string } | undefined;
expect(row).toBeUndefined();
// after
const row = await findRow(orm, Tags, { id });
expect(row).toBeNull();
Count rows
// before
const { n } = testDb.prepare('SELECT COUNT(*) AS n FROM tour_waypoints WHERE place_id = ?').get(placeId) as { n: number };
// after
const n = await countRows(orm, TourWaypoints, { place: placeId });
Update a row
// before
testDb.prepare('UPDATE users SET must_change_password = 1 WHERE id = ?').run(user.id);
// after
await updateRows(orm, Users, { id: user.id }, { must_change_password: 1 });
Delete rows between tests
// before
beforeEach(() => db.exec('DELETE FROM tags'));
// after
beforeEach(async () => {
await deleteRows(orm, Tags);
});
Worked examples
These three were converted in full and show the setups side by side:
tests/integration/categories.test.ts: abuildApp()suite on the app's own ORM (nestApp.get(MikroORM)).tests/e2e/tags.e2e.test.ts: a partial-module e2e that also moved from hand-rolled tables to the migrated snapshot, with users pinned to the ids its session cookies are signed for.tests/e2e/calendar-feed-visibility.e2e.test.ts: a partial-module e2e seeding a user, a dated trip and staged and live bookings.
What stays raw
Some tests are about raw SQL itself, and converting them would test nothing:
- the dialect functions (
tests/unit/db/dialect/sql-functions.test.ts), which pin the SQL each function renders and what the engine returns for it; - schema introspection:
PRAGMA table_info,foreign_key_list,index_list,EXPLAIN QUERY PLAN,sqlite_master(entity/schema parity, foreign-key indexes, the table walk inresetTestDb); - databases no ORM is bound to: the legacy upgrade fixtures and the legacy
baseline, the pre-migrate snapshot copy, a restored file before the boot, the
bare database
scripts/migrate-encryption.tsworks on; - the driver itself (NUL and NaN binding, the shared connection behind
createTestOrm).
Mark them in one of two ways:
-
a whole file whose subject is raw SQL goes into the
exemptlist ofscripts/test-sql-baseline.json, with the reason as its value; -
a single statement carries a comment with
test-sql-allow:and a reason, on its own line or on the line directly above:// test-sql-allow: the table list comes from sqlite_master, which no entity or repository maps. const tables = db.prepare("SELECT name FROM sqlite_master WHERE type='table'").all();A marker without a reason does not count.
Seeding a migration test with the rows the migration is about to transform is also raw by nature (the entities describe the schema after the migration); use the allow marker there.
Ratchet
scripts/test-sql-ratchet.mjs (npm run lint:test-sql, a CI gate) counts every
.prepare( under tests/ per file, comments included, against the counts in
scripts/test-sql-baseline.json:
- a file holding more than its entry fails, and a file without an entry may hold none, so new fixtures go through the factories;
- an entry above what its file holds now fails too, so a conversion lands with
npm run lint:test-sql -- --update, which lowers and drops entries and never raises or adds one; - an exemption for a file that is gone fails until it is removed.
When a file needs a raw statement that is not raw by nature, add a factory or a reader here instead.