Debug all sql queries

This commit is contained in:
Sam Becker 2024-03-02 13:19:33 -06:00
parent 7f463d70ce
commit 0053670f02

View File

@ -286,10 +286,15 @@ export type GetPhotosOptions = {
includeHidden?: boolean includeHidden?: boolean
} }
const safelyQueryPhotos = async <T>(callback: () => Promise<T>): Promise<T> => { const safelyQueryPhotos = async <T>(
callback: () => Promise<T>,
debugMessage: string
): Promise<T> => {
let result: T; let result: T;
console.log('Executing sql query (neon postgres)'); if (debugMessage) {
console.log(`Executing sql query: ${debugMessage}`);
}
try { try {
result = await callback(); result = await callback();
@ -391,7 +396,7 @@ export const getPhotos = async (options: GetPhotosOptions = {}) => {
return safelyQueryPhotos(async () => { return safelyQueryPhotos(async () => {
const client = await db.connect(); const client = await db.connect();
return client.query(sql.join(' '), values); return client.query(sql.join(' '), values);
}) }, sql.join(' '))
.then(({ rows }) => rows.map(parsePhotoFromDb)); .then(({ rows }) => rows.map(parsePhotoFromDb));
}; };
@ -421,14 +426,15 @@ export const getPhotosNearId = async (
`, `,
[id, limit] [id, limit]
); );
}) }, 'getPhotosNearId')
.then(({ rows }) => rows.map(parsePhotoFromDb)); .then(({ rows }) => rows.map(parsePhotoFromDb));
}; };
export const getPhotoIds = async ({ limit }: { limit?: number }) => { export const getPhotoIds = async ({ limit }: { limit?: number }) => {
return safelyQueryPhotos(() => limit return safelyQueryPhotos(() => limit
? sql`SELECT id FROM photos LIMIT ${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)); .then(({ rows }) => rows.map(({ id }) => id as string));
}; };
@ -436,40 +442,60 @@ export const getPhoto = async (id: string): Promise<Photo | undefined> => {
// Check for photo id forwarding // Check for photo id forwarding
// and convert short ids to uuids // and convert short ids to uuids
const photoId = translatePhotoId(id); const photoId = translatePhotoId(id);
return safelyQueryPhotos(() => sqlGetPhoto(photoId)) return safelyQueryPhotos(() => sqlGetPhoto(photoId), 'getPhoto')
.then(({ rows }) => rows.map(parsePhotoFromDb)) .then(({ rows }) => rows.map(parsePhotoFromDb))
.then(photos => photos.length > 0 ? photos[0] : undefined); .then(photos => photos.length > 0 ? photos[0] : undefined);
}; };
export const getPhotosDateRange = () => export const getPhotosDateRange = () =>
safelyQueryPhotos(sqlGetPhotosDateRange); safelyQueryPhotos(sqlGetPhotosDateRange, 'getPhotosDateRange');
export const getPhotosCount = () => export const getPhotosCount = () =>
safelyQueryPhotos(sqlGetPhotosCount); safelyQueryPhotos(sqlGetPhotosCount, 'getPhotosCount');
export const getPhotosCountIncludingHidden = () => export const getPhotosCountIncludingHidden = () =>
safelyQueryPhotos(sqlGetPhotosCountIncludingHidden); safelyQueryPhotos(
sqlGetPhotosCountIncludingHidden,
'getPhotosCountIncludingHidden',
);
// TAGS // TAGS
export const getUniqueTags = () => export const getUniqueTags = () =>
safelyQueryPhotos(sqlGetUniqueTags); safelyQueryPhotos(sqlGetUniqueTags, 'getUniqueTags');
export const getUniqueTagsHidden = () => export const getUniqueTagsHidden = () =>
safelyQueryPhotos(sqlGetUniqueTagsHidden); safelyQueryPhotos(sqlGetUniqueTagsHidden, 'getUniqueTagsHidden');
export const getPhotosTagDateRange = (tag: string) => export const getPhotosTagDateRange = (tag: string) =>
safelyQueryPhotos(() => sqlGetPhotosTagDateRange(tag)); safelyQueryPhotos(
() => sqlGetPhotosTagDateRange(tag),
'getPhotosTagDateRange',
);
export const getPhotosTagCount = (tag: string) => export const getPhotosTagCount = (tag: string) =>
safelyQueryPhotos(() => sqlGetPhotosTagCount(tag)); safelyQueryPhotos(
() => sqlGetPhotosTagCount(tag),
'getPhotosTagCount',
);
// CAMERAS // CAMERAS
export const getUniqueCameras = () => export const getUniqueCameras = () =>
safelyQueryPhotos(sqlGetUniqueCameras); safelyQueryPhotos(sqlGetUniqueCameras, 'getUniqueCameras');
export const getPhotosCameraDateRange = (camera: Camera) => export const getPhotosCameraDateRange = (camera: Camera) =>
safelyQueryPhotos(() => sqlGetPhotosCameraDateRange(camera)); safelyQueryPhotos(
() => sqlGetPhotosCameraDateRange(camera),
'getPhotosCameraDateRange',
);
export const getPhotosCameraCount = (camera: Camera) => export const getPhotosCameraCount = (camera: Camera) =>
safelyQueryPhotos(() => sqlGetPhotosCameraCount(camera)); safelyQueryPhotos(
() => sqlGetPhotosCameraCount(camera),
'getPhotosCameraCount',
);
// FILM SIMULATIONS // FILM SIMULATIONS
export const getUniqueFilmSimulations = () => export const getUniqueFilmSimulations = () =>
safelyQueryPhotos(sqlGetUniqueFilmSimulations); safelyQueryPhotos(sqlGetUniqueFilmSimulations, 'getUniqueFilmSimulations');
export const getPhotosFilmSimulationDateRange = export const getPhotosFilmSimulationDateRange =
(simulation: FilmSimulation) => safelyQueryPhotos(() => (simulation: FilmSimulation) => safelyQueryPhotos(
sqlGetPhotosFilmSimulationDateRange(simulation)); () => sqlGetPhotosFilmSimulationDateRange(simulation),
'getPhotosFilmSimulationDateRange',
);
export const getPhotosFilmSimulationCount = (simulation: FilmSimulation) => export const getPhotosFilmSimulationCount = (simulation: FilmSimulation) =>
safelyQueryPhotos(() => sqlGetPhotosFilmSimulationCount(simulation)); safelyQueryPhotos(
() => sqlGetPhotosFilmSimulationCount(simulation),
'getPhotosFilmSimulationCount',
);