79 lines
1.9 KiB
TypeScript
79 lines
1.9 KiB
TypeScript
import { PrismaClient } from '@prisma/client'
|
|
import fs from 'fs'
|
|
import path from 'path'
|
|
|
|
const prisma = new PrismaClient()
|
|
|
|
type LocalizedString = { tr: string; en: string }
|
|
|
|
type JsonMenuItem = {
|
|
id: string
|
|
emoji?: string
|
|
image?: string
|
|
name: LocalizedString
|
|
description?: LocalizedString
|
|
price?: number
|
|
highlight?: boolean
|
|
}
|
|
|
|
type JsonCategory = {
|
|
id: string
|
|
emoji: string
|
|
label: LocalizedString
|
|
items: JsonMenuItem[]
|
|
}
|
|
|
|
async function migrate() {
|
|
console.log('Starting migration...')
|
|
|
|
const filePath = path.join(process.cwd(), 'data', 'menu.json')
|
|
const fileContent = fs.readFileSync(filePath, 'utf-8')
|
|
const categories: JsonCategory[] = JSON.parse(fileContent)
|
|
|
|
// Clear existing data (if any)
|
|
await prisma.menuItem.deleteMany({})
|
|
await prisma.category.deleteMany({})
|
|
console.log('Cleared existing data')
|
|
|
|
for (let cIndex = 0; cIndex < categories.length; cIndex++) {
|
|
const cat = categories[cIndex]
|
|
|
|
await prisma.category.create({
|
|
data: {
|
|
id: cat.id,
|
|
emoji: cat.emoji,
|
|
labelTr: cat.label.tr,
|
|
labelEn: cat.label.en,
|
|
order: cIndex,
|
|
items: {
|
|
create: cat.items.map((item, iIndex) => ({
|
|
id: item.id,
|
|
emoji: item.emoji || null,
|
|
image: item.image || null,
|
|
nameTr: item.name.tr,
|
|
nameEn: item.name.en,
|
|
descriptionTr: item.description?.tr || null,
|
|
descriptionEn: item.description?.en || null,
|
|
price: item.price !== undefined ? item.price : null,
|
|
highlight: item.highlight || false,
|
|
order: iIndex,
|
|
}))
|
|
}
|
|
}
|
|
})
|
|
|
|
console.log(`Migrated category: ${cat.label.tr}`)
|
|
}
|
|
|
|
console.log('Migration complete!')
|
|
}
|
|
|
|
migrate()
|
|
.catch((e) => {
|
|
console.error(e)
|
|
process.exit(1)
|
|
})
|
|
.finally(async () => {
|
|
await prisma.$disconnect()
|
|
})
|