Files
ketopath/packages/db/prisma/seed.ts
T
lucianoandClaude Opus 4.7 c37dc5d717 feat(seed): popola Recipe.variants e RecipeIngredient.substitutes
- DEFAULT_SUBSTITUTES_BY_INGREDIENT in seed-recipes.ts: ~50 mappature
  riusabili (pesci interscambiabili, latticini lactose-free, manzo→tacchino,
  pollo→tacchino, frutta secca, condimenti, legumi inter-cambiabili)
- 12 ricette signature ora dichiarano variants reali (1-2 ciascuna):
  • Frittata di spinaci → senza lattosio / più saziante
  • Yogurt greco con noci → vegana / con frutti di bosco
  • Uova strapazzate con pancetta → senza maiale / con verdure
  • Insalata di pollo, avocado e pomodorini → vegetariana / con feta e olive
  • Salmone al forno con asparagi → versione fredda / sgombro
  • Bistecca con zucchine → tacchino / con melanzane
  • Insalatona con tonno e olive → Niçoise / sgombro
  • Branzino al cartoccio → mediterranea / orata
  • Polpette di pollo e ricotta → in sugo / tacchino-spinaci
  • Tagliata con rucola → con pomodorini / bresaola
  • Caprese → con avocado / con prosciutto crudo
  • Pancake proteici, Smoothie verde, Tagliolini di zucchine,
    Insalata di lenticchie, Quinoa con pollo (varianti analoghe)
- seed.ts ora propaga r.variants al campo Recipe.variants e applica
  ing.substitutes (override) o DEFAULT_SUBSTITUTES_BY_INGREDIENT[name]
  alle righe RecipeIngredient

Smoke verificato live: /recipes/<frittata> mostra "VARIANTI SUGGERITE"
con "Versione senza lattosio (-40 kcal)" e "Più saziante (+90 kcal)";
gli ingredienti hanno la riga "Sostituzioni: bietole, Grana Padano…".

Catalog: 46 ricette, 164 link RecipeIngredient, ~50 mappature substitutes.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-29 23:31:33 +02:00

119 lines
3.9 KiB
TypeScript

import { PrismaClient } from '@prisma/client';
import { SEED_INGREDIENTS } from './seed-ingredients.js';
import { DEFAULT_SUBSTITUTES_BY_INGREDIENT, SEED_RECIPES } from './seed-recipes.js';
const prisma = new PrismaClient();
async function main(): Promise<void> {
// 1. Ingredienti — upsert sul nome (unique). Aggiorniamo phase2Week e gli
// altri campi anche per quelli già presenti, così il seed resta autoritativo.
let ingredientsInserted = 0;
for (const i of SEED_INGREDIENTS) {
const data = {
category: i.category,
defaultUnit: i.defaultUnit,
kcalPer100g: i.kcalPer100g,
proteinPer100g: i.proteinPer100g,
fatPer100g: i.fatPer100g,
netCarbPer100g: i.netCarbPer100g,
exclusionGroups: i.exclusionGroups,
priceAvgEur: i.priceAvgEur ?? null,
phase2Week: i.phase2Week ?? null,
};
const existing = await prisma.ingredient.findUnique({ where: { name: i.name } });
if (existing) {
await prisma.ingredient.update({ where: { id: existing.id }, data });
} else {
await prisma.ingredient.create({ data: { ...data, name: i.name } });
ingredientsInserted++;
}
}
const allIngredients = await prisma.ingredient.findMany({ select: { id: true, name: true } });
const ingredientByName = new Map(allIngredients.map((i) => [i.name, i.id]));
// 2. Ricette — `Recipe.name` non è unique nel modello, ma il seed garantisce
// unicità per costruzione. Idempotente: se esiste, aggiorniamo i campi
// anagrafici e ricreiamo i RecipeIngredient (così editare il seed propaga).
let recipesInserted = 0;
let linksCreated = 0;
for (const r of SEED_RECIPES) {
let recipe = await prisma.recipe.findFirst({ where: { name: r.name } });
const variantsJson = r.variants ?? [];
if (!recipe) {
recipe = await prisma.recipe.create({
data: {
name: r.name,
category: r.category,
description: r.description,
prepMinutes: r.prepMinutes,
difficulty: r.difficulty,
kcal: r.kcal,
proteinG: r.proteinG,
fatG: r.fatG,
netCarbG: r.netCarbG,
phases: r.phases,
notesChef: r.notesChef ?? null,
variants: variantsJson,
},
});
recipesInserted++;
} else {
recipe = await prisma.recipe.update({
where: { id: recipe.id },
data: {
category: r.category,
description: r.description,
prepMinutes: r.prepMinutes,
difficulty: r.difficulty,
kcal: r.kcal,
proteinG: r.proteinG,
fatG: r.fatG,
netCarbG: r.netCarbG,
phases: r.phases,
notesChef: r.notesChef ?? null,
variants: variantsJson,
},
});
}
await prisma.recipeIngredient.deleteMany({ where: { recipeId: recipe.id } });
for (const ing of r.ingredients) {
const ingredientId = ingredientByName.get(ing.name);
if (!ingredientId) {
// eslint-disable-next-line no-console
console.warn(`[seed] ricetta "${r.name}": ingrediente "${ing.name}" non trovato`);
continue;
}
const substitutes = ing.substitutes ?? DEFAULT_SUBSTITUTES_BY_INGREDIENT[ing.name] ?? [];
await prisma.recipeIngredient.create({
data: {
recipeId: recipe.id,
ingredientId,
quantity: ing.quantity,
unit: ing.unit,
substitutes,
},
});
linksCreated++;
}
}
// eslint-disable-next-line no-console
console.info(
`[seed] ingredients +${ingredientsInserted} (catalog ${SEED_INGREDIENTS.length}), ` +
`recipes +${recipesInserted} (catalog ${SEED_RECIPES.length}), recipe-ingredient links: ${linksCreated}`,
);
}
main()
.catch((err: unknown) => {
// eslint-disable-next-line no-console
console.error(err);
process.exitCode = 1;
})
.finally(() => {
void prisma.$disconnect();
});