24 lines
835 B
TypeScript
24 lines
835 B
TypeScript
import "server-only";
|
|
import { pool } from "@/lib/db";
|
|
import type { IngredientEntry } from "./ingredient-search";
|
|
|
|
// Die kanonischen Zutaten sind global und ändern sich nur beim Einspielen
|
|
// (Neustart). Einmal laden, im Speicher halten.
|
|
let cache: Promise<IngredientEntry[]> | null = null;
|
|
|
|
export function allIngredients(): Promise<IngredientEntry[]> {
|
|
cache ??= pool
|
|
.query("select id, key, default_unit as unit, category, names, synonyms, forms from ingredients order by key")
|
|
.then((r) => r.rows as IngredientEntry[])
|
|
.catch((error) => {
|
|
cache = null;
|
|
throw error;
|
|
});
|
|
return cache;
|
|
}
|
|
|
|
export async function ingredientName(id: string, locale: string): Promise<string> {
|
|
const entry = (await allIngredients()).find((i) => i.id === id);
|
|
return entry?.names[locale] ?? entry?.names.en ?? "";
|
|
}
|