41 lines
1.2 KiB
TypeScript
41 lines
1.2 KiB
TypeScript
import type {
|
|
Equipment,
|
|
Ingredient,
|
|
PrepAction,
|
|
PurchaseItem,
|
|
Recipe,
|
|
Unit,
|
|
SourceMapping,
|
|
} from "./types.ts";
|
|
import { databaseProjection, openDatabase } from "./database.ts";
|
|
|
|
export function loadCatalogs() {
|
|
const database = openDatabase();
|
|
if (!database) throw new Error("Database unavailable. Restore from backup with: npm run db:restore -- path/to/backup.json");
|
|
try {
|
|
const projection = databaseProjection(database) as {
|
|
ingredients: Ingredient[];
|
|
recipes: Recipe[];
|
|
units: Unit[];
|
|
equipment: Equipment[];
|
|
prepActions: PrepAction[];
|
|
purchaseItems: PurchaseItem[];
|
|
sourceMappings: SourceMapping[];
|
|
};
|
|
const map = <T extends { id: string }>(values: T[]) =>
|
|
new Map(values.map((value) => [value.id, value]));
|
|
|
|
return {
|
|
ingredients: map(projection.ingredients),
|
|
recipes: map(projection.recipes),
|
|
units: map(projection.units),
|
|
equipment: map(projection.equipment),
|
|
prepActions: map(projection.prepActions),
|
|
purchaseItems: map(projection.purchaseItems),
|
|
sourceMappings: map(projection.sourceMappings),
|
|
};
|
|
} finally {
|
|
database.close();
|
|
}
|
|
}
|