- scripts/transfer-content.ts: export (shared config + MongoDB adapter, locale 'all', drafts, upload files) and import (PostgreSQL, one transaction, refuses a non-empty target); pure helpers in src/lib/content-transfer.ts - LegalPages: the section anchor is its own field; as the array row id it was a table-wide primary key in PostgreSQL and the two pages collided - Products.status: own enumName; the default name equalled the drafts _status enum, so only draft/published were accepted - baseline migration regenerated Verified on a staging copy (disposable MongoDB + PostgreSQL on llmdev): counts match, all 14 public pages (hu/en) render identical text, partner logo loads, second import writes nothing. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
101 lines
4.2 KiB
TypeScript
101 lines
4.2 KiB
TypeScript
/**
|
|
* MITHOME-136: pure helpers of the one-off MongoDB → PostgreSQL content
|
|
* transfer (scripts/transfer-content.ts).
|
|
*
|
|
* The export reads every document with `locale: 'all'`, so each localized
|
|
* field arrives as `{ hu, en }`. Payload writes one locale at a time, so the
|
|
* import splits a document into per-locale data using the field config:
|
|
* localized fields take the locale's value, groups/unnamed rows recurse,
|
|
* array rows keep their ids (the English update must hit the same rows), and
|
|
* upload/relationship ids are mapped from the old to the new id.
|
|
*/
|
|
import type { Field } from 'payload'
|
|
|
|
export type Locale = 'hu' | 'en'
|
|
type Data = Record<string, unknown>
|
|
export type IdMap = Map<string, string>
|
|
|
|
const SYSTEM_KEYS = new Set(['id', '_id', '__v', 'globalType', 'updatedAt', '_status'])
|
|
|
|
const isObject = (value: unknown): value is Data =>
|
|
typeof value === 'object' && value !== null && !Array.isArray(value)
|
|
|
|
function mapId(value: unknown, ids: IdMap): unknown {
|
|
if (value === null || value === undefined) return value
|
|
const id = isObject(value) ? value.id : value
|
|
return typeof id === 'string' ? (ids.get(id) ?? id) : id
|
|
}
|
|
|
|
function splitFields(fields: readonly Field[], source: Data, locale: Locale, ids: IdMap, out: Data): Data {
|
|
for (const field of fields) {
|
|
if (!('name' in field) || !field.name) {
|
|
// row / collapsible: unnamed wrappers keep their fields on the same level
|
|
if ('fields' in field) splitFields(field.fields, source, locale, ids, out)
|
|
continue
|
|
}
|
|
if (!(field.name in source)) continue
|
|
const raw = source[field.name]
|
|
const value = 'localized' in field && field.localized && isObject(raw) ? raw[locale] : raw
|
|
|
|
if (field.type === 'group' && isObject(value)) {
|
|
out[field.name] = splitFields(field.fields, value, locale, ids, {})
|
|
} else if (field.type === 'array' && Array.isArray(value)) {
|
|
out[field.name] = value.map((row) =>
|
|
isObject(row) ? { ...(row.id ? { id: row.id } : {}), ...splitFields(field.fields, row, locale, ids, {}) } : row,
|
|
)
|
|
} else if (field.type === 'upload' || field.type === 'relationship') {
|
|
out[field.name] = Array.isArray(value) ? value.map((v) => mapId(v, ids)) : mapId(value, ids)
|
|
} else {
|
|
out[field.name] = value
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
/**
|
|
* Shape changes between the MongoDB schema and the current one, applied to an
|
|
* exported document before it is split by locale.
|
|
* - legal-pages: the section anchor was the array row `id`; it is now the
|
|
* `anchor` field, and the row id must be unique across all pages (it is a
|
|
* table-wide primary key in PostgreSQL), so it becomes `<slug>-<anchor>`.
|
|
*/
|
|
export function upgradeDoc(collection: string, doc: Data): Data {
|
|
if (collection !== 'legal-pages' || !Array.isArray(doc.sections)) return doc
|
|
return {
|
|
...doc,
|
|
sections: doc.sections.map((section) =>
|
|
isObject(section) && typeof section.id === 'string' && !('anchor' in section)
|
|
? { ...section, id: `${doc.slug}-${section.id}`, anchor: section.id }
|
|
: section,
|
|
),
|
|
}
|
|
}
|
|
|
|
/** The data of one locale, ready for `create`/`update` with that `locale`. */
|
|
export function localeData(fields: readonly Field[], doc: Data, locale: Locale, ids: IdMap = new Map()): Data {
|
|
const out = splitFields(fields, doc, locale, ids, {})
|
|
for (const key of SYSTEM_KEYS) delete out[key]
|
|
// Keep the original creation time (contact submission retention).
|
|
if (typeof doc.createdAt === 'string') out.createdAt = doc.createdAt
|
|
return out
|
|
}
|
|
|
|
/** Count documents per collection/global in an export bundle. */
|
|
export function bundleCounts(bundle: TransferBundle): Record<string, number> {
|
|
const counts: Record<string, number> = {}
|
|
for (const [slug, docs] of Object.entries(bundle.collections)) counts[slug] = docs.length
|
|
for (const slug of Object.keys(bundle.globals)) counts[`global:${slug}`] = 1
|
|
return counts
|
|
}
|
|
|
|
export type BundleDoc = { published: Data; draft?: Data }
|
|
|
|
export type TransferBundle = {
|
|
exportedAt: string
|
|
source: string
|
|
globals: Record<string, BundleDoc>
|
|
collections: Record<string, BundleDoc[]>
|
|
/** Upload files by filename, base64 — the bundle is self-contained. */
|
|
files: Record<string, { mimeType: string; base64: string }>
|
|
}
|