129 lines
5.5 KiB
JavaScript
129 lines
5.5 KiB
JavaScript
// seed.cjs - Prisma 7 Seed Script
|
|
// Note: Bun loads .env automatically - no need for dotenv
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
const { PrismaClient } = require('@prisma/client');
|
|
const { Pool } = require('pg');
|
|
const { PrismaPg } = require('@prisma/adapter-pg');
|
|
const bcrypt = require('bcryptjs');
|
|
const seedAdmin = require('./seedAdmin.cjs');
|
|
|
|
const pool = new Pool({ connectionString: process.env.DATABASE_URL });
|
|
const adapter = new PrismaPg(pool);
|
|
const prisma = new PrismaClient({ adapter });
|
|
|
|
const brandNames = [
|
|
'ตะไคร้', 'ลาเวนเดอร์', 'วานิลลา', 'ซากุระ', 'มะลิ', 'ยูคาลิปตัส', 'เฟรชลาเวนเดอร์',
|
|
'เปเปอร์มิ้น', 'โอเชี่ยน', 'เซเว่นซี', 'ดอกแก้ว', 'ดอกโมก', 'ลีลาวดี', 'ส้ม', 'มะนาว',
|
|
'แคนตาลูป', 'แอปเปิ้ลแดง', 'แอปเปิ้ลเขียว', 'ดอกลิลลี่', 'โรสเมรี่', 'อัญชัน',
|
|
'กุหลาบแดง', 'หมากฝรั่ง', 'ผลไม้รวม', 'มะกรูด', 'แซนด้าวูส', 'ดอกปีบ', 'ดอกบัว',
|
|
'โอโซน', 'ไทยสปา', 'กล้วย', 'กาแฟ', 'ดอกชมนาด', 'กลิ่นน้ำนม', 'ดอกการะเวก',
|
|
'ดอกจำปา', 'ดอกพิกุล', 'ดอกพุด', 'ดอกราตรี', 'ดอกลำดวน', 'ทับทิม', 'มะละกอสุก',
|
|
'ดอกราชาวดี', 'กลิ่นลูกพีช', 'กลิ่นองุ่น'
|
|
];
|
|
|
|
const categoryConfigs = [
|
|
{
|
|
name: 'Oil Ozone',
|
|
sizes: ['30ml', '60ml', '85ml', '120ml'],
|
|
priceMap: { '30ml': 69, '60ml': 100, '85ml': 120, '120ml': 150 }
|
|
},
|
|
{
|
|
name: 'Aroma Oil',
|
|
sizes: ['30ml', '60ml', '85ml', '120ml'],
|
|
priceMap: { '30ml': 90, '60ml': 150, '85ml': 165, '120ml': 225 } // 120ml ปรับเป็น 225 (฿1.875/ml) ให้ถูกกว่า 85ml ต่อ ml
|
|
},
|
|
{
|
|
name: 'Reed Diffuser',
|
|
sizes: ['60ml', '85ml', '120ml'],
|
|
priceMap: { '60ml': 120, '85ml': 160, '120ml': 190 }
|
|
},
|
|
{
|
|
name: 'Room Spray',
|
|
sizes: ['85ml'],
|
|
priceMap: { '85ml': 150 }
|
|
},
|
|
{
|
|
name: 'Car Diffuser',
|
|
sizes: ['25ml'],
|
|
priceMap: { '25ml': 60 }
|
|
}
|
|
];
|
|
|
|
async function main() {
|
|
process.stdout.write('🚀 Starting System Seeding (Admin, Menus, Brands, Categories)...\n');
|
|
|
|
// 0. Seed Admin User
|
|
await seedAdmin(prisma);
|
|
|
|
// 0.5 Seed Core Menus
|
|
const coreMenus = [
|
|
{ name: "main", items: [{ id: 1, label: "Home", url: "/", order: 1 }, { id: 2, label: "Shop", url: "/shop", order: 2 }, { id: 3, label: "Scent Guide", url: "/scent-guide", order: 3 }, { id: 4, label: "About Us", url: "/about", order: 4 }] },
|
|
{ name: "footer-explore", items: [{ id: 5, label: "Home", url: "/", order: 1 }, { id: 6, label: "Shop", url: "/shop", order: 2 }, { id: 7, label: "Scent Guide", url: "/scent-guide", order: 3 }, { id: 8, label: "About Us", url: "/about", order: 4 }] },
|
|
{ name: "footer-support", items: [{ id: 9, label: "Track Order", url: "/track-order", order: 1 }, { id: 10, label: "FAQ", url: "/faq", order: 2 }, { id: 11, label: "Contact Us", url: "/contact", order: 3 }, { id: 12, label: "Delivery & Returns", url: "/delivery", order: 4 }] }
|
|
];
|
|
|
|
for (const menu of coreMenus) {
|
|
const m = await prisma.menu.upsert({
|
|
where: { name: menu.name },
|
|
update: {},
|
|
create: { name: menu.name }
|
|
});
|
|
for (const item of menu.items) {
|
|
await prisma.menuItem.upsert({
|
|
where: { id: item.id },
|
|
update: { label: item.label, url: item.url, order: item.order, menuId: m.id },
|
|
create: { id: item.id, label: item.label, url: item.url, order: item.order, menuId: m.id }
|
|
});
|
|
}
|
|
}
|
|
process.stdout.write('✅ Seeded Core Menus.\n');
|
|
|
|
// 1. Seed Brands & Categories
|
|
const brands = [];
|
|
for (const name of brandNames) {
|
|
const s = await prisma.productBrand.upsert({
|
|
where: { name },
|
|
update: {},
|
|
create: { name }
|
|
});
|
|
brands.push(s);
|
|
}
|
|
process.stdout.write(`✅ Seeded ${brands.length} Brands.\n`);
|
|
|
|
const categories = [];
|
|
for (const config of categoryConfigs) {
|
|
const c = await prisma.category.upsert({
|
|
where: { name: config.name },
|
|
update: {},
|
|
create: { name: config.name }
|
|
});
|
|
categories.push(c);
|
|
}
|
|
process.stdout.write(`✅ Seeded ${categories.length} Categories.\n`);
|
|
|
|
// Skipped product & variant seeding per configuration
|
|
|
|
process.stdout.write(`\n✨ Seeding Complete!\n`);
|
|
process.stdout.write(`📊 Summary:\n`);
|
|
process.stdout.write(` - Admin: 1 (admin / homaroma123)\n`);
|
|
process.stdout.write(` - Menus: ${coreMenus.length}\n`);
|
|
process.stdout.write(` - Brands: ${brands.length}\n`);
|
|
process.stdout.write(` - Categories: ${categories.length}\n`);
|
|
process.stdout.write(` - Products: 0 (Skipped per configuration)\n`);
|
|
|
|
// Fix PostgreSQL sequence desync for tables where we hardcoded IDs
|
|
process.stdout.write('🔄 Resetting sequences for hardcoded ID tables...\n');
|
|
await prisma.$executeRawUnsafe(`SELECT setval('"MenuItem_id_seq"', COALESCE((SELECT MAX(id) FROM "MenuItem") + 1, 1), false);`);
|
|
process.stdout.write('✅ Sequences reset successfully.\n');
|
|
}
|
|
|
|
main()
|
|
.catch((e) => {
|
|
process.stderr.write(`❌ Error: ${e.message}\n`);
|
|
process.exit(1);
|
|
})
|
|
.finally(async () => {
|
|
await prisma.$disconnect();
|
|
});
|