77 lines
2.1 KiB
TypeScript
77 lines
2.1 KiB
TypeScript
import { PrismaClient } from '@prisma/client';
|
|
import { PrismaPg } from '@prisma/adapter-pg';
|
|
import { Pool } from 'pg';
|
|
import fs from 'fs';
|
|
import path from 'path';
|
|
import 'dotenv/config';
|
|
import bcrypt from 'bcryptjs';
|
|
|
|
const connectionString = process.env.DATABASE_URL;
|
|
const pool = new Pool({ connectionString });
|
|
const adapter = new PrismaPg(pool);
|
|
const prisma = new PrismaClient({ adapter });
|
|
|
|
async function main() {
|
|
const menuPath = path.join(__dirname, '../menu/menu.json');
|
|
const menuData = JSON.parse(fs.readFileSync(menuPath, 'utf-8'));
|
|
|
|
// 1. Create Restaurant
|
|
const restaurant = await prisma.restaurant.create({
|
|
data: {
|
|
name: menuData.restaurant_name,
|
|
footerNote: menuData.footer_note,
|
|
},
|
|
});
|
|
|
|
console.log(`Created restaurant: ${restaurant.name}`);
|
|
|
|
// 1.5 Create Admin User
|
|
const adminUsername = 'admin';
|
|
const hashedPassword = await bcrypt.hash('admin', 10);
|
|
|
|
const existingUser = await (prisma as any).user.findUnique({ where: { username: adminUsername } });
|
|
if (!existingUser) {
|
|
await (prisma as any).user.create({
|
|
data: {
|
|
username: adminUsername,
|
|
password: hashedPassword,
|
|
},
|
|
});
|
|
console.log(`Created admin user: ${adminUsername}`);
|
|
}
|
|
for (const cat of menuData.categories) {
|
|
const category = await prisma.category.create({
|
|
data: {
|
|
title: cat.title,
|
|
externalId: cat.id,
|
|
restaurantId: restaurant.id,
|
|
},
|
|
});
|
|
|
|
console.log(`Created category: ${category.title}`);
|
|
|
|
for (const item of cat.items) {
|
|
await prisma.item.create({
|
|
data: {
|
|
name: item.name,
|
|
ingredients: item.ingredients || null,
|
|
tasteProfile: item.taste_profile || null,
|
|
grapeVariety: item.grape_variety || null,
|
|
price: item.price, // Prisma handles JSON fields automatically
|
|
categoryId: category.id,
|
|
},
|
|
});
|
|
}
|
|
console.log(` Added ${cat.items.length} items to ${cat.title}`);
|
|
}
|
|
}
|
|
|
|
main()
|
|
.catch((e) => {
|
|
console.error(e);
|
|
process.exit(1);
|
|
})
|
|
.finally(async () => {
|
|
await prisma.$disconnect();
|
|
});
|