Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions backend/src/schema/books/resolvers/Query/bookFormats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string[]> | undefined;

async function loadBookFormats() {
// Order by each format's book count, and then alphabetically
const rows = await db
.select({ bookFormat: books.bookFormat })
Expand All @@ -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<QueryResolvers["bookFormats"]> = () => {
cached ??= loadBookFormats().catch((error: unknown) => {
// Let the next request try again instead of caching the failure
cached = undefined;
throw error;
});
return cached;
};
15 changes: 14 additions & 1 deletion backend/src/schema/books/resolvers/Query/genres.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<QueryResolvers["genres"]> = 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<string[]> | undefined;

async function loadGenres() {
// Order by each genre's book count, and then alphabetically
const rows = await db
.select({ genre: bookGenres.genre })
Expand All @@ -13,4 +17,13 @@ export const genres: NonNullable<QueryResolvers["genres"]> = async () => {
.orderBy(desc(count(bookToGenres.bookId)), asc(bookGenres.genre));

return rows.map((row) => row.genre);
}

export const genres: NonNullable<QueryResolvers["genres"]> = () => {
cached ??= loadGenres().catch((error: unknown) => {
// Let the next request try again instead of caching the failure
cached = undefined;
throw error;
});
return cached;
};
15 changes: 14 additions & 1 deletion backend/src/schema/books/resolvers/Query/languages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ import type { QueryResolvers } from "~/schema/types.generated";
import { db } from "~/db";
import { books } from "~/db/schema";

export const languages: NonNullable<QueryResolvers["languages"]> = 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<string[]> | undefined;

async function loadLanguages() {
// Order by each language's book count, and then alphabetically
const rows = await db
.select({ language: books.language })
Expand All @@ -13,4 +17,13 @@ export const languages: NonNullable<QueryResolvers["languages"]> = async () => {
.orderBy(desc(count()), asc(books.language));

return rows.flatMap((row) => (row.language ? [row.language] : []));
}

export const languages: NonNullable<QueryResolvers["languages"]> = () => {
cached ??= loadLanguages().catch((error: unknown) => {
// Let the next request try again instead of caching the failure
cached = undefined;
throw error;
});
return cached;
};