From 166471cf0fbf4bd583c699315826a2cab8cfc73b Mon Sep 17 00:00:00 2001 From: Chris Arter Date: Fri, 8 May 2026 11:53:54 -0400 Subject: [PATCH] feat(import): record upsert helper Idempotent upsert for posts/projects/pages: looks up existing doc by slug, updates it if found, creates it otherwise. Returns an action discriminant so callers can count creates vs updates. Co-Authored-By: Claude Sonnet 4.6 --- scripts/import/upsert-record.ts | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 scripts/import/upsert-record.ts diff --git a/scripts/import/upsert-record.ts b/scripts/import/upsert-record.ts new file mode 100644 index 0000000..d728f38 --- /dev/null +++ b/scripts/import/upsert-record.ts @@ -0,0 +1,26 @@ +import type { Payload } from 'payload' + +export type Slug = 'posts' | 'projects' | 'pages' + +export async function upsertRecord( + payload: Payload, + collection: Slug, + slug: string, + data: Record, +) { + const existing = await payload.find({ + collection, + where: { slug: { equals: slug } }, + limit: 1, + }) + if (existing.docs[0]) { + await payload.update({ + collection, + id: existing.docs[0].id, + data: { ...data, slug }, + }) + return { action: 'updated' as const } + } + await payload.create({ collection, data: { ...data, slug } }) + return { action: 'created' as const } +} -- 2.51.2