Refine static params queries

This commit is contained in:
Sam Becker 2024-01-20 11:29:01 -06:00
parent 4887f93bfc
commit 84848f6a45
2 changed files with 10 additions and 4 deletions

View File

@ -13,14 +13,13 @@ import {
} from '@/site/paths';
import PhotoDetailPage from '@/photo/PhotoDetailPage';
import { getPhotoCached, getPhotosNearIdCached } from '@/cache';
import { getPhotos } from '@/services/vercel-postgres';
import { getPhotoIds } from '@/services/vercel-postgres';
export async function generateStaticParams() {
const photos = await getPhotos({ limit: GENERATE_STATIC_PARAMS_LIMIT });
return photos.map(({ id }) => ({ photoId: id }));
const photos = await getPhotoIds({ limit: GENERATE_STATIC_PARAMS_LIMIT });
return photos.map(id => ({ photoId: id }));
}
interface PhotoProps {
params: { photoId: string }
}

View File

@ -410,6 +410,13 @@ export const getPhotosNearId = async (
.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`)
.then(({ rows }) => rows.map(({ id }) => id as string));
};
export const getPhoto = async (id: string): Promise<Photo | undefined> => {
// Check for photo id forwarding
// and convert short ids to uuids