diff --git a/backend/src/schema/books/resolvers/Query/bookFormats.ts b/backend/src/schema/books/resolvers/Query/bookFormats.ts index 0ca312a..17f74d6 100644 --- a/backend/src/schema/books/resolvers/Query/bookFormats.ts +++ b/backend/src/schema/books/resolvers/Query/bookFormats.ts @@ -3,9 +3,11 @@ import type { QueryResolvers } from "~/schema/types.generated"; import { db } from "~/db"; import { books } from "~/db/schema"; -export const bookFormats: NonNullable< - QueryResolvers["bookFormats"] -> = async () => { +// The list only changes with the dataset, so it is computed once per server +// process and shared between all users. Restart the server after reseeding. +let cached: Promise | undefined; + +async function loadBookFormats() { // Order by each format's book count, and then alphabetically const rows = await db .select({ bookFormat: books.bookFormat }) @@ -15,4 +17,13 @@ export const bookFormats: NonNullable< .orderBy(desc(count()), asc(books.bookFormat)); return rows.flatMap((row) => (row.bookFormat ? [row.bookFormat] : [])); +} + +export const bookFormats: NonNullable = () => { + cached ??= loadBookFormats().catch((error: unknown) => { + // Let the next request try again instead of caching the failure + cached = undefined; + throw error; + }); + return cached; }; diff --git a/backend/src/schema/books/resolvers/Query/genres.ts b/backend/src/schema/books/resolvers/Query/genres.ts index c536d95..cfda934 100644 --- a/backend/src/schema/books/resolvers/Query/genres.ts +++ b/backend/src/schema/books/resolvers/Query/genres.ts @@ -3,7 +3,11 @@ import type { QueryResolvers } from "~/schema/types.generated"; import { db } from "~/db"; import { bookGenres, bookToGenres } from "~/db/schema"; -export const genres: NonNullable = async () => { +// The list only changes with the dataset, so it is computed once per server +// process and shared between all users. Restart the server after reseeding. +let cached: Promise | undefined; + +async function loadGenres() { // Order by each genre's book count, and then alphabetically const rows = await db .select({ genre: bookGenres.genre }) @@ -13,4 +17,13 @@ export const genres: NonNullable = async () => { .orderBy(desc(count(bookToGenres.bookId)), asc(bookGenres.genre)); return rows.map((row) => row.genre); +} + +export const genres: NonNullable = () => { + cached ??= loadGenres().catch((error: unknown) => { + // Let the next request try again instead of caching the failure + cached = undefined; + throw error; + }); + return cached; }; diff --git a/backend/src/schema/books/resolvers/Query/languages.ts b/backend/src/schema/books/resolvers/Query/languages.ts index af22448..f5bb002 100644 --- a/backend/src/schema/books/resolvers/Query/languages.ts +++ b/backend/src/schema/books/resolvers/Query/languages.ts @@ -3,7 +3,11 @@ import type { QueryResolvers } from "~/schema/types.generated"; import { db } from "~/db"; import { books } from "~/db/schema"; -export const languages: NonNullable = async () => { +// The list only changes with the dataset, so it is computed once per server +// process and shared between all users. Restart the server after reseeding. +let cached: Promise | undefined; + +async function loadLanguages() { // Order by each language's book count, and then alphabetically const rows = await db .select({ language: books.language }) @@ -13,4 +17,13 @@ export const languages: NonNullable = async () => { .orderBy(desc(count()), asc(books.language)); return rows.flatMap((row) => (row.language ? [row.language] : [])); +} + +export const languages: NonNullable = () => { + cached ??= loadLanguages().catch((error: unknown) => { + // Let the next request try again instead of caching the failure + cached = undefined; + throw error; + }); + return cached; };