From 0053670f024e5e22eefda3438420d136fa4d8d43 Mon Sep 17 00:00:00 2001 From: Sam Becker Date: Sat, 2 Mar 2024 13:19:33 -0600 Subject: [PATCH] Debug all sql queries --- src/services/vercel-postgres.ts | 66 +++++++++++++++++++++++---------- 1 file changed, 46 insertions(+), 20 deletions(-) diff --git a/src/services/vercel-postgres.ts b/src/services/vercel-postgres.ts index 62b535fa..9e27e656 100644 --- a/src/services/vercel-postgres.ts +++ b/src/services/vercel-postgres.ts @@ -286,10 +286,15 @@ export type GetPhotosOptions = { includeHidden?: boolean } -const safelyQueryPhotos = async (callback: () => Promise): Promise => { +const safelyQueryPhotos = async ( + callback: () => Promise, + debugMessage: string +): Promise => { let result: T; - console.log('Executing sql query (neon postgres)'); + if (debugMessage) { + console.log(`Executing sql query: ${debugMessage}`); + } try { result = await callback(); @@ -391,7 +396,7 @@ export const getPhotos = async (options: GetPhotosOptions = {}) => { return safelyQueryPhotos(async () => { const client = await db.connect(); return client.query(sql.join(' '), values); - }) + }, sql.join(' ')) .then(({ rows }) => rows.map(parsePhotoFromDb)); }; @@ -421,14 +426,15 @@ export const getPhotosNearId = async ( `, [id, limit] ); - }) + }, 'getPhotosNearId') .then(({ rows }) => rows.map(parsePhotoFromDb)); }; export const getPhotoIds = async ({ limit }: { limit?: number }) => { return safelyQueryPhotos(() => limit ? sql`SELECT id FROM photos LIMIT ${limit}` - : sql`SELECT id FROM photos`) + : sql`SELECT id FROM photos`, + 'getPhotoIds') .then(({ rows }) => rows.map(({ id }) => id as string)); }; @@ -436,40 +442,60 @@ export const getPhoto = async (id: string): Promise => { // Check for photo id forwarding // and convert short ids to uuids const photoId = translatePhotoId(id); - return safelyQueryPhotos(() => sqlGetPhoto(photoId)) + return safelyQueryPhotos(() => sqlGetPhoto(photoId), 'getPhoto') .then(({ rows }) => rows.map(parsePhotoFromDb)) .then(photos => photos.length > 0 ? photos[0] : undefined); }; export const getPhotosDateRange = () => - safelyQueryPhotos(sqlGetPhotosDateRange); + safelyQueryPhotos(sqlGetPhotosDateRange, 'getPhotosDateRange'); export const getPhotosCount = () => - safelyQueryPhotos(sqlGetPhotosCount); + safelyQueryPhotos(sqlGetPhotosCount, 'getPhotosCount'); export const getPhotosCountIncludingHidden = () => - safelyQueryPhotos(sqlGetPhotosCountIncludingHidden); + safelyQueryPhotos( + sqlGetPhotosCountIncludingHidden, + 'getPhotosCountIncludingHidden', + ); // TAGS export const getUniqueTags = () => - safelyQueryPhotos(sqlGetUniqueTags); + safelyQueryPhotos(sqlGetUniqueTags, 'getUniqueTags'); export const getUniqueTagsHidden = () => - safelyQueryPhotos(sqlGetUniqueTagsHidden); + safelyQueryPhotos(sqlGetUniqueTagsHidden, 'getUniqueTagsHidden'); export const getPhotosTagDateRange = (tag: string) => - safelyQueryPhotos(() => sqlGetPhotosTagDateRange(tag)); + safelyQueryPhotos( + () => sqlGetPhotosTagDateRange(tag), + 'getPhotosTagDateRange', + ); export const getPhotosTagCount = (tag: string) => - safelyQueryPhotos(() => sqlGetPhotosTagCount(tag)); + safelyQueryPhotos( + () => sqlGetPhotosTagCount(tag), + 'getPhotosTagCount', + ); // CAMERAS export const getUniqueCameras = () => - safelyQueryPhotos(sqlGetUniqueCameras); + safelyQueryPhotos(sqlGetUniqueCameras, 'getUniqueCameras'); export const getPhotosCameraDateRange = (camera: Camera) => - safelyQueryPhotos(() => sqlGetPhotosCameraDateRange(camera)); + safelyQueryPhotos( + () => sqlGetPhotosCameraDateRange(camera), + 'getPhotosCameraDateRange', + ); export const getPhotosCameraCount = (camera: Camera) => - safelyQueryPhotos(() => sqlGetPhotosCameraCount(camera)); + safelyQueryPhotos( + () => sqlGetPhotosCameraCount(camera), + 'getPhotosCameraCount', + ); // FILM SIMULATIONS export const getUniqueFilmSimulations = () => - safelyQueryPhotos(sqlGetUniqueFilmSimulations); + safelyQueryPhotos(sqlGetUniqueFilmSimulations, 'getUniqueFilmSimulations'); export const getPhotosFilmSimulationDateRange = - (simulation: FilmSimulation) => safelyQueryPhotos(() => - sqlGetPhotosFilmSimulationDateRange(simulation)); + (simulation: FilmSimulation) => safelyQueryPhotos( + () => sqlGetPhotosFilmSimulationDateRange(simulation), + 'getPhotosFilmSimulationDateRange', + ); export const getPhotosFilmSimulationCount = (simulation: FilmSimulation) => - safelyQueryPhotos(() => sqlGetPhotosFilmSimulationCount(simulation)); + safelyQueryPhotos( + () => sqlGetPhotosFilmSimulationCount(simulation), + 'getPhotosFilmSimulationCount', + );