From 305bf219156c826de759262d29c7874872995d62 Mon Sep 17 00:00:00 2001 From: Sam Becker Date: Mon, 20 May 2024 11:13:44 -0500 Subject: [PATCH] Use new meta query for cameras, simulations --- .../film/[simulation]/[photoId]/layout.tsx | 5 +-- .../[make]/[model]/[photoId]/layout.tsx | 4 +- src/camera/data.ts | 4 +- src/photo/cache.ts | 14 ------ src/photo/db.ts | 44 +------------------ src/simulation/data.ts | 4 +- 6 files changed, 9 insertions(+), 66 deletions(-) diff --git a/src/app/film/[simulation]/[photoId]/layout.tsx b/src/app/film/[simulation]/[photoId]/layout.tsx index cba372b1..4773e8da 100644 --- a/src/app/film/[simulation]/[photoId]/layout.tsx +++ b/src/app/film/[simulation]/[photoId]/layout.tsx @@ -14,7 +14,7 @@ import PhotoDetailPage from '@/photo/PhotoDetailPage'; import { ReactNode, cache } from 'react'; import { FilmSimulation } from '@/simulation'; import { - getPhotosFilmSimulationMetaCached, + getPhotosMetaCached, getPhotosNearIdCached, } from '@/photo/cache'; @@ -70,8 +70,7 @@ export default async function PhotoFilmSimulationPage({ if (!photo) { redirect(PATH_ROOT); } - const { count, dateRange } = - await getPhotosFilmSimulationMetaCached(simulation); + const { count, dateRange } = await getPhotosMetaCached({ simulation }); return <> {children} diff --git a/src/app/shot-on/[make]/[model]/[photoId]/layout.tsx b/src/app/shot-on/[make]/[model]/[photoId]/layout.tsx index d1a5c04c..42efcca0 100644 --- a/src/app/shot-on/[make]/[model]/[photoId]/layout.tsx +++ b/src/app/shot-on/[make]/[model]/[photoId]/layout.tsx @@ -12,7 +12,7 @@ import { } from '@/site/paths'; import PhotoDetailPage from '@/photo/PhotoDetailPage'; import { - getPhotosCameraMetaCached, + getPhotosMetaCached, getPhotosNearIdCached, } from '@/photo/cache'; import { @@ -79,7 +79,7 @@ export default async function PhotoCameraPage({ const camera = cameraFromPhoto(photo, { make, model }); - const { count, dateRange } = await getPhotosCameraMetaCached(camera); + const { count, dateRange } = await getPhotosMetaCached({ camera }); return <> {children} diff --git a/src/camera/data.ts b/src/camera/data.ts index b90a19f5..afd407c6 100644 --- a/src/camera/data.ts +++ b/src/camera/data.ts @@ -1,7 +1,7 @@ import { cameraFromPhoto, getCameraFromParams } from '.'; import { getPhotosCached, - getPhotosCameraMetaCached, + getPhotosMetaCached, } from '@/photo/cache'; export const getPhotosCameraDataCached = async ( @@ -12,7 +12,7 @@ export const getPhotosCameraDataCached = async ( const camera = getCameraFromParams({ make, model }); return Promise.all([ getPhotosCached({ camera, limit }), - getPhotosCameraMetaCached(camera), + getPhotosMetaCached({ camera }), ]) .then(([photos, meta]) => [ photos, diff --git a/src/photo/cache.ts b/src/photo/cache.ts index 793009a8..a42c9ae0 100644 --- a/src/photo/cache.ts +++ b/src/photo/cache.ts @@ -12,10 +12,8 @@ import { getPhotosCountIncludingHidden, getUniqueCameras, getUniqueTags, - getPhotosCameraMeta, getUniqueTagsHidden, getUniqueFilmSimulations, - getPhotosFilmSimulationMeta, getPhotosDateRange, getPhotosNearId, getPhotosMostRecentUpdate, @@ -189,18 +187,6 @@ export const getPhotosMostRecentUpdateCached = [KEY_PHOTOS, KEY_COUNT, KEY_DATE_RANGE], ); -export const getPhotosCameraMetaCached = - unstable_cache( - getPhotosCameraMeta, - [KEY_PHOTOS, KEY_CAMERAS, KEY_DATE_RANGE], - ); - -export const getPhotosFilmSimulationMetaCached = - unstable_cache( - getPhotosFilmSimulationMeta, - [KEY_PHOTOS, KEY_FILM_SIMULATIONS, KEY_DATE_RANGE], - ); - export const getPhotoCached = (...args: Parameters) => unstable_cache( getPhoto, diff --git a/src/photo/db.ts b/src/photo/db.ts index ef213418..9fb496ea 100644 --- a/src/photo/db.ts +++ b/src/photo/db.ts @@ -199,34 +199,6 @@ const sqlGetPhotosDateRange = async () => sql` ? rows[0] as PhotoDateRange : undefined); -const sqlGetPhotosCameraMeta = async (camera: Camera) => sql` - SELECT COUNT(*), MIN(taken_at_naive) as start, MAX(taken_at_naive) as end - FROM photos - WHERE - LOWER(REPLACE(make, ' ', '-'))=${parameterize(camera.make, true)} AND - LOWER(REPLACE(model, ' ', '-'))=${parameterize(camera.model, true)} AND - hidden IS NOT TRUE -`.then(({ rows }) => ({ - count: parseInt(rows[0].count, 10), - ...rows[0]?.start && rows[0]?.end - ? { dateRange: rows[0] as PhotoDateRange } - : undefined, - })); - -const sqlGetPhotosFilmSimulationMeta = async ( - simulation: FilmSimulation, -) => sql` - SELECT COUNT(*), MIN(taken_at_naive) as start, MAX(taken_at_naive) as end - FROM photos - WHERE film_simulation=${simulation} AND - hidden IS NOT TRUE -`.then(({ rows }) => ({ - count: parseInt(rows[0].count, 10), - ...rows[0]?.start && rows[0]?.end - ? { dateRange: rows[0] as PhotoDateRange } - : undefined, - })); - const sqlGetUniqueTags = async () => sql` SELECT DISTINCT unnest(tags) as tag, COUNT(*) FROM photos @@ -553,26 +525,12 @@ export const getPhotosMostRecentUpdate = () => 'getPhotosMostRecentUpdate', ); -// TAGS +// UNIQUE META export const getUniqueTags = () => safelyQueryPhotos(sqlGetUniqueTags, 'getUniqueTags'); export const getUniqueTagsHidden = () => safelyQueryPhotos(sqlGetUniqueTagsHidden, 'getUniqueTagsHidden'); - -// CAMERAS export const getUniqueCameras = () => safelyQueryPhotos(sqlGetUniqueCameras, 'getUniqueCameras'); -export const getPhotosCameraMeta = (camera: Camera) => - safelyQueryPhotos( - () => sqlGetPhotosCameraMeta(camera), - 'getPhotosCameraMeta', - ); - -// FILM SIMULATIONS export const getUniqueFilmSimulations = () => safelyQueryPhotos(sqlGetUniqueFilmSimulations, 'getUniqueFilmSimulations'); -export const getPhotosFilmSimulationMeta = - (simulation: FilmSimulation) => safelyQueryPhotos( - () => sqlGetPhotosFilmSimulationMeta(simulation), - 'getPhotosFilmSimulationMeta', - ); diff --git a/src/simulation/data.ts b/src/simulation/data.ts index d28b026e..a289fdef 100644 --- a/src/simulation/data.ts +++ b/src/simulation/data.ts @@ -1,6 +1,6 @@ import { getPhotosCached, - getPhotosFilmSimulationMetaCached, + getPhotosMetaCached, } from '@/photo/cache'; import { FilmSimulation } from '.'; @@ -13,5 +13,5 @@ export const getPhotosFilmSimulationDataCached = ({ }) => Promise.all([ getPhotosCached({ simulation, limit }), - getPhotosFilmSimulationMetaCached(simulation), + getPhotosMetaCached({ simulation }), ]);