/** * Prisma Seed Script * Populates initial program and award data from JSON config */ import { PrismaClient } from '@prisma/client'; import * as fs from 'fs'; import * as path from 'path'; import { fileURLToPath } from 'url'; const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); const prisma = new PrismaClient(); interface ProgramData { id: string; name: string; teamName: string; performer: string; order: number; remark: string; } interface AwardData { id: string; name: string; icon: string; order: number; remark: string; } interface ConfigFile { programs: ProgramData[]; awards: AwardData[]; } async function main() { console.log('Starting seed...'); // Load config from JSON file const configPath = path.join(__dirname, '../config/programs.json'); if (!fs.existsSync(configPath)) { console.error('Config file not found:', configPath); process.exit(1); } const config: ConfigFile = JSON.parse(fs.readFileSync(configPath, 'utf-8')); // Seed programs console.log(`Seeding ${config.programs.length} programs...`); for (const p of config.programs) { await prisma.program.upsert({ where: { id: p.id }, create: { id: p.id, name: p.name, teamName: p.teamName || '', performer: p.performer || '', order: p.order, remark: p.remark || null, isActive: true, }, update: { name: p.name, teamName: p.teamName || '', performer: p.performer || '', order: p.order, remark: p.remark || null, isActive: true, }, }); console.log(` - ${p.name}`); } // Seed awards console.log(`Seeding ${config.awards.length} awards...`); for (const a of config.awards) { await prisma.award.upsert({ where: { id: a.id }, create: { id: a.id, name: a.name, icon: a.icon, order: a.order, remark: a.remark || null, isActive: true, }, update: { name: a.name, icon: a.icon, order: a.order, remark: a.remark || null, isActive: true, }, }); console.log(` - ${a.name}`); } console.log('Seed completed!'); } main() .catch((e) => { console.error('Seed failed:', e); process.exit(1); }) .finally(async () => { await prisma.$disconnect(); });