mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-05 10:24:05 +00:00
refactor(db): read new ids with RETURNING instead of insertId
Kysely fills InsertResult.insertId from SQLite's last_insert_rowid. On
Postgres it is undefined, so the sixteen repository inserts that returned
Number(result.insertId) would hand NaN ids to their services without an
error. They now add .returning('id') and read the id from the row, which
SQLite (3.35 and later, bundled by better-sqlite3) and Postgres both answer
with the same value.
DemoRepository ran its example-trip seed as nine SQL strings through
connection.execute, including INSERT OR IGNORE and a bare CURRENT_TIMESTAMP.
Those are Kysely inserts on the generated table types now: ON CONFLICT
(trip_id, user_id) DO NOTHING for the membership row, the dialect helper for
the timestamp, RETURNING id where the caller needs it. With no SQL text left
it drops off the ESLint exemption list and its pin.
ESLint now refuses any read of insertId in src/db/repositories and
src/db/seeders, so the pattern cannot come back.
This commit is contained in:
@@ -91,6 +91,22 @@ describe('DemoRepository', () => {
|
||||
expect(note).toEqual({ day_id: dayId, trip_id: tripId, text: 'Note text', time: '13:00', icon: 'Info', sort_order: 0.5 });
|
||||
});
|
||||
|
||||
it('DEMOREPO-007: the inserts read the new id back with RETURNING, and a packing item gets its updated_at stamp', async () => {
|
||||
const { user } = createAdmin(testDb);
|
||||
const tripId = await demo.insertTrip(user.id, 'Ids', 'd', '2026-05-01', '2026-05-02', 'EUR');
|
||||
expect(typeof tripId).toBe('number');
|
||||
const latest = testDb.prepare('SELECT MAX(id) AS id FROM trips').get() as { id: number };
|
||||
expect(tripId).toBe(latest.id);
|
||||
|
||||
const dayId = await demo.insertDay(tripId, 1, '2026-05-01');
|
||||
const secondDayId = await demo.insertDay(tripId, 2, '2026-05-02');
|
||||
expect(secondDayId).toBeGreaterThan(dayId);
|
||||
|
||||
await demo.insertPackingItem(tripId, 'Charger', 0, 'Tech', 1);
|
||||
const packing = testDb.prepare('SELECT updated_at FROM packing_items WHERE trip_id = ?').get(tripId) as { updated_at: string | null };
|
||||
expect(packing.updated_at).toMatch(/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/);
|
||||
});
|
||||
|
||||
it('DEMOREPO-CRED-001 (DMR1/DMR5 round trip): getAdminCredentials reads exactly what restoreAdminCredentials wrote', async () => {
|
||||
const { user } = createAdmin(testDb, { email: 'admin@nomad.app' });
|
||||
testDb.prepare('UPDATE users SET maps_api_key = ?, openweather_api_key = ?, unsplash_api_key = ?, avatar = ? WHERE id = ?')
|
||||
|
||||
@@ -141,7 +141,6 @@ declare const repo: { run(sql: string, params?: unknown[]): Promise<unknown>; ge
|
||||
// the ignores entry) has to follow.
|
||||
const EXEMPT_SQL_STRINGS: Record<string, number> = {
|
||||
'src/db/repositories/MaintenanceRepository.ts': 4,
|
||||
'src/db/repositories/DemoRepository.ts': 9,
|
||||
'src/db/seeders/AddonSeeder.ts': 1,
|
||||
'src/db/seeders/AdminSeeder.ts': 2,
|
||||
'src/db/seeders/CategorySeeder.ts': 2,
|
||||
@@ -202,3 +201,29 @@ void safeFetch(url);`,
|
||||
expect(hits(options, "void connection.execute('SELECT 1');")).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('insertId selectors', () => {
|
||||
const INSERT = `
|
||||
declare const result: { insertId: bigint | undefined };
|
||||
declare const query: { executeTakeFirstOrThrow(): Promise<{ insertId: bigint | undefined }> };
|
||||
`;
|
||||
const insertIdHits = (options: unknown[], code: string) =>
|
||||
messages(options, `${INSERT}\n${code}`).filter((m) => m.startsWith('InsertResult.insertId'));
|
||||
|
||||
it('flags a read of insertId in every repository, the SQL-exempt one included, and in a seeder', async () => {
|
||||
for (const file of [
|
||||
'src/db/repositories/Probe.repository.ts',
|
||||
'src/db/repositories/MaintenanceRepository.ts',
|
||||
'src/db/seeders/ProbeSeeder.ts',
|
||||
]) {
|
||||
const options = await selectorsFor(file);
|
||||
expect(insertIdHits(options, 'void Number(result.insertId);'), file).toHaveLength(1);
|
||||
expect(insertIdHits(options, 'void query.executeTakeFirstOrThrow().then(({ insertId }) => insertId);'), file).toHaveLength(1);
|
||||
}
|
||||
});
|
||||
|
||||
it('leaves a returning() read alone', async () => {
|
||||
const options = await selectorsFor('src/db/repositories/Probe.repository.ts');
|
||||
expect(insertIdHits(options, 'declare const row: { id: number };\nvoid row.id;')).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user