fix: route db helper functions through the null-safe proxy

getPlaceWithTags, canAccessTrip, and isOwner were calling _db! directly,
bypassing the Proxy that guards against null-dereference during a backup
restore. When the restore handler briefly sets _db = null, any concurrent
request hitting these helpers would crash with an unhandled TypeError
instead of receiving a clean 503-style error.

Replace all four _db! accesses with the exported db proxy so the guard
("Database connection is not available") fires consistently.
This commit is contained in:
jubnl
2026-04-01 04:22:57 +02:00
parent ccb5f9df1f
commit 701a8ab03a

View File

@@ -84,7 +84,7 @@ interface PlaceWithTags extends Place {
} }
function getPlaceWithTags(placeId: number | string): PlaceWithTags | null { function getPlaceWithTags(placeId: number | string): PlaceWithTags | null {
const place = _db!.prepare(` const place = db.prepare(`
SELECT p.*, c.name as category_name, c.color as category_color, c.icon as category_icon SELECT p.*, c.name as category_name, c.color as category_color, c.icon as category_icon
FROM places p FROM places p
LEFT JOIN categories c ON p.category_id = c.id LEFT JOIN categories c ON p.category_id = c.id
@@ -93,7 +93,7 @@ function getPlaceWithTags(placeId: number | string): PlaceWithTags | null {
if (!place) return null; if (!place) return null;
const tags = _db!.prepare(` const tags = db.prepare(`
SELECT t.* FROM tags t SELECT t.* FROM tags t
JOIN place_tags pt ON t.id = pt.tag_id JOIN place_tags pt ON t.id = pt.tag_id
WHERE pt.place_id = ? WHERE pt.place_id = ?
@@ -117,7 +117,7 @@ interface TripAccess {
} }
function canAccessTrip(tripId: number | string, userId: number): TripAccess | undefined { function canAccessTrip(tripId: number | string, userId: number): TripAccess | undefined {
return _db!.prepare(` return db.prepare(`
SELECT t.id, t.user_id FROM trips t SELECT t.id, t.user_id FROM trips t
LEFT JOIN trip_members m ON m.trip_id = t.id AND m.user_id = ? LEFT JOIN trip_members m ON m.trip_id = t.id AND m.user_id = ?
WHERE t.id = ? AND (t.user_id = ? OR m.user_id IS NOT NULL) WHERE t.id = ? AND (t.user_id = ? OR m.user_id IS NOT NULL)
@@ -125,7 +125,7 @@ function canAccessTrip(tripId: number | string, userId: number): TripAccess | un
} }
function isOwner(tripId: number | string, userId: number): boolean { function isOwner(tripId: number | string, userId: number): boolean {
return !!_db!.prepare('SELECT id FROM trips WHERE id = ? AND user_id = ?').get(tripId, userId); return !!db.prepare('SELECT id FROM trips WHERE id = ? AND user_id = ?').get(tripId, userId);
} }
export { db, closeDb, reinitialize, getPlaceWithTags, canAccessTrip, isOwner }; export { db, closeDb, reinitialize, getPlaceWithTags, canAccessTrip, isOwner };