import { PrismaClient } from "@prisma/client"; import { defaultCategories, defaultRootFolder } from "../lib/defaults"; const prisma = new PrismaClient(); async function main() { console.log("🌱 Seeding database..."); // ═══════════════════════════════════════════════════════════════════════════ // CrΓ©er le dossier racine // ═══════════════════════════════════════════════════════════════════════════ const rootFolder = await prisma.folder.upsert({ where: { id: defaultRootFolder.id }, update: {}, create: defaultRootFolder, }); console.log("πŸ“ Root folder:", rootFolder.name); // ═══════════════════════════════════════════════════════════════════════════ // CrΓ©er les catΓ©gories (hiΓ©rarchiques) // ═══════════════════════════════════════════════════════════════════════════ const slugToId = new Map(); // D'abord les parents const parents = defaultCategories.filter((c) => c.parentSlug === null); console.log(`\n🏷️ CrΓ©ation de ${parents.length} catΓ©gories parentes...`); for (const category of parents) { const existing = await prisma.category.findFirst({ where: { name: category.name, parentId: null }, }); if (!existing) { const created = await prisma.category.create({ data: { name: category.name, color: category.color, icon: category.icon, keywords: JSON.stringify(category.keywords), parentId: null, }, }); slugToId.set(category.slug, created.id); console.log(` βœ… ${category.name}`); } else { slugToId.set(category.slug, existing.id); } } // Puis les enfants const children = defaultCategories.filter((c) => c.parentSlug !== null); console.log(`\nπŸ“‚ CrΓ©ation de ${children.length} sous-catΓ©gories...`); for (const category of children) { const parentId = slugToId.get(category.parentSlug!); if (!parentId) { console.log(` ⚠️ Parent non trouvΓ© pour: ${category.name}`); continue; } const existing = await prisma.category.findFirst({ where: { name: category.name, parentId }, }); if (!existing) { const created = await prisma.category.create({ data: { name: category.name, color: category.color, icon: category.icon, keywords: JSON.stringify(category.keywords), parentId, }, }); slugToId.set(category.slug, created.id); console.log(` βœ… ${category.name}`); } else { slugToId.set(category.slug, existing.id); } } // Stats const totalCategories = await prisma.category.count(); console.log(`\n✨ Seeding terminΓ©! ${totalCategories} catΓ©gories en base.`); } main() .catch((e) => { console.error(e); process.exit(1); }) .finally(async () => { await prisma.$disconnect(); });