Files
fintrack/prisma/seed.ts

96 lines
3.4 KiB
TypeScript
Raw Permalink Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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<string, string>();
// 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();
});