chefbible/prisma/seed.ts
2025-08-14 12:51:24 -04:00

104 lines
3.1 KiB
TypeScript

import { PrismaClient } from '@prisma-app/client';
import { faker } from '@faker-js/faker';
const prisma = new PrismaClient();
async function main() {
console.log('🧹 Clearing existing records...');
await prisma.menuRecipe.deleteMany();
await prisma.recipeIngredient.deleteMany();
await prisma.menu.deleteMany();
await prisma.recipe.deleteMany();
await prisma.ingredient.deleteMany();
await prisma.allergen.deleteMany();
console.log('🌱 Seeding database with food-themed data...');
// Allergens
const allergenNames = ['Gluten', 'Dairy', 'Tree Nuts', 'Eggs', 'Fish', 'Soy'];
const allergens = await Promise.all(
allergenNames.map(name =>
prisma.allergen.create({
data: { name, description: faker.food.description() },
})
)
);
// Ingredients (mix of spices, vegetables, meats, fruits)
const ingredients = await Promise.all(
Array.from({ length: 20 }).map(() => {
const typePick = faker.helpers.arrayElement([
() => faker.food.ingredient(),
() => faker.food.spice(),
() => faker.food.vegetable(),
() => faker.food.meat(),
() => faker.food.fruit(),
]);
const randomAllergens = faker.helpers.arrayElements(allergens, faker.number.int({ min: 0, max: 2 }));
return prisma.ingredient.create({
data: {
name: typePick(),
allergens: { connect: randomAllergens.map(a => ({ id: a.id })) },
},
});
})
);
// Recipes
const recipes = await Promise.all(
Array.from({ length: 10 }).map(async () => {
const recipeIngredients = faker.helpers.arrayElements(ingredients, faker.number.int({ min: 3, max: 6 }));
return prisma.recipe.create({
data: {
name: faker.food.dish(),
description: faker.food.description(),
instructions: faker.lorem.paragraph(),
photoUrl: faker.image.urlLoremFlickr({ category: 'food' }),
ingredients: {
create: recipeIngredients.map(ing => ({
ingredientId: ing.id,
quantity: faker.number.int({ min: 10, max: 500 }),
unit: faker.helpers.arrayElement(['g', 'ml', 'pcs']),
})),
},
},
});
})
);
// Menus
const menus = await Promise.all(
['Spring', 'Summer', 'Autumn', 'Winter'].map(season => {
const seasonRecipes = faker.helpers.arrayElements(recipes, 3);
return prisma.menu.create({
data: {
name: `${season} Menu: ${faker.food.ethnicCategory()}`,
season,
recipes: {
create: seasonRecipes.map((rec, index) => ({
recipeId: rec.id,
position: index + 1,
})),
},
},
});
})
);
console.log('✅ Seeding complete!');
console.log('--- Summary ---');
console.log(`Allergens created: ${allergens.length}`);
console.log(`Ingredients created: ${ingredients.length}`);
console.log(`Recipes created: ${recipes.length}`);
console.log(`Menus created: ${menus.length}`);
}
main()
.catch(e => {
console.error(e);
process.exit(1);
})
.finally(async () => {
await prisma.$disconnect();
});