113 lines
3.2 KiB
TypeScript
113 lines
3.2 KiB
TypeScript
import { getPayload } from 'payload'
|
|
import configPromise from '../src/payload.config'
|
|
import fs from 'fs'
|
|
import path from 'path'
|
|
|
|
// Helper to remove system fields
|
|
const cleanData = (data: any) => {
|
|
if (!data) return data;
|
|
const { id, createdAt, updatedAt, _status, ...rest } = data
|
|
return rest
|
|
}
|
|
|
|
// Deep replace page IDs in globals and nullify missing media relations
|
|
const mapRelations = (obj: any, idMap: Map<number, number>): any => {
|
|
if (Array.isArray(obj)) {
|
|
return obj.map(item => mapRelations(item, idMap))
|
|
}
|
|
if (obj !== null && typeof obj === 'object') {
|
|
const newObj: any = {}
|
|
for (const key of Object.keys(obj)) {
|
|
if (['image', 'imageSecondary', 'photo', 'logo'].includes(key)) {
|
|
newObj[key] = null
|
|
} else if (key === 'page' && typeof obj[key] === 'number') {
|
|
newObj[key] = idMap.get(obj[key]) || obj[key]
|
|
} else if (key === 'homepage' && typeof obj[key] === 'number') {
|
|
newObj[key] = idMap.get(obj[key]) || obj[key]
|
|
} else {
|
|
newObj[key] = mapRelations(obj[key], idMap)
|
|
}
|
|
}
|
|
return newObj
|
|
}
|
|
return obj
|
|
}
|
|
|
|
console.log("Starting script"); async function run() {
|
|
const payload = await getPayload({ config: configPromise })
|
|
|
|
const snapshotPath = path.join(process.cwd(), 'scripts', 'content-snapshot.json')
|
|
if (!fs.existsSync(snapshotPath)) {
|
|
console.error('No content-snapshot.json found!')
|
|
process.exit(1)
|
|
}
|
|
|
|
const snapshot = JSON.parse(fs.readFileSync(snapshotPath, 'utf-8'))
|
|
const idMap = new Map<number, number>()
|
|
|
|
// 1. Check if DB is already seeded (has pages)
|
|
const existingPages = await payload.find({
|
|
collection: 'pages',
|
|
limit: 1,
|
|
})
|
|
|
|
if (existingPages.totalDocs > 0) {
|
|
console.log('Pages already seeded. If you want to reset, use the db:reset script.')
|
|
process.exit(0)
|
|
}
|
|
|
|
console.log('Seeding pages from snapshot...')
|
|
for (const pageItem of snapshot.pages) {
|
|
const oldId = pageItem.id
|
|
|
|
// Create in PL first
|
|
const createdPage = await payload.create({
|
|
collection: 'pages',
|
|
locale: 'pl',
|
|
data: mapRelations(cleanData(pageItem.pl), idMap),
|
|
})
|
|
|
|
idMap.set(oldId, createdPage.id as number)
|
|
|
|
// Update with DE
|
|
if (pageItem.de) {
|
|
await payload.update({
|
|
collection: 'pages',
|
|
id: createdPage.id,
|
|
locale: 'de',
|
|
data: mapRelations(cleanData(pageItem.de), idMap),
|
|
})
|
|
}
|
|
|
|
console.log(`- Restored page: ${pageItem.pl?.title || 'Unknown'} (ID: ${oldId} -> ${createdPage.id})`)
|
|
}
|
|
|
|
console.log('Seeding globals from snapshot...')
|
|
for (const slug of Object.keys(snapshot.globals)) {
|
|
const globalData = snapshot.globals[slug]
|
|
|
|
if (globalData.pl) {
|
|
const plData = mapRelations(cleanData(globalData.pl), idMap)
|
|
await payload.updateGlobal({
|
|
slug: slug as any,
|
|
locale: 'pl',
|
|
data: plData,
|
|
})
|
|
}
|
|
|
|
if (globalData.de) {
|
|
const deData = mapRelations(cleanData(globalData.de), idMap)
|
|
await payload.updateGlobal({
|
|
slug: slug as any,
|
|
locale: 'de',
|
|
data: deData,
|
|
})
|
|
}
|
|
console.log(`- Restored global: ${slug}`)
|
|
}
|
|
|
|
console.log('Seed completed successfully.')
|
|
}
|
|
|
|
try { await run(); } catch (err) { console.error(err); process.exit(1); }
|