fillGlobal: a field already holding the seeded value is skipped, not rewritten

Seeding exactly the default (allowThemeToggle: true) used to count as unset,
rewrite it and report create on every run. Now: skip (exists), no write. A choice
at its default is still replaced by a different seeded value; editor changes stay.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
Radosław Smoliński
2026-10-29 16:39:55 +01:00
co-authored by Claude Opus 5.5
parent 6f56ef4205
commit 37131e9930
6 changed files with 17 additions and 5 deletions
+2 -1
View File
@@ -93,7 +93,8 @@ fifteen ready page sections and ten page templates. A new site in one command: `
400. With R2 nothing changes; a production build on localhost still never opens private IPs. 400. With R2 nothing changes; a production build on localhost still never opens private IPs.
- **`fillGlobal`:** a choice field (select, radio, checkbox, number, date) still at its `defaultValue` counts as - **`fillGlobal`:** a choice field (select, radio, checkbox, number, date) still at its `defaultValue` counts as
empty, so e.g. Theme → `defaultTheme: 'system'` can be seeded over the stored default `light`. Text is never empty, so e.g. Theme → `defaultTheme: 'system'` can be seeded over the stored default `light`. Text is never
overwritten, even when it equals a default. overwritten, even when it equals a default. A field already holding
exactly the seeded value is not rewritten (`skip (exists)`), so a seed of defaults stays idempotent.
- **Docs:** `LanguageSwitcher` with `renderLabel` needs a `'use client'` wrapper (i18n.md, ipal-site). - **Docs:** `LanguageSwitcher` with `renderLabel` needs a `'use client'` wrapper (i18n.md, ipal-site).
### Changed ### Changed
@@ -560,7 +560,7 @@ Server only (`pnpm payload run`); never import it from client code, the Payload
| `createSeeder(payload, { dryRun?, update?, force?, only?, guard?, editCheck?, context?, pagesCollection?, formsCollection?, label?, log? })` | `@intecion/ipal-kit/seed` | The seed rules in one place → `{ upsertBySlug, upsertBy, fillGlobal, summary, dryRun, update, force, local, only }`. `only` (or `SEED_ONLY`) = the records the run may touch (`pages:kontakt`, `forms:<title>`, `global:<slug>`, `pages:*`), the rest → `skip (not listed)`; a run without it against production / a non-local database throws unless `IPAL_SEED_ALLOW_FULL=1` / `guard.allowFull: true` (after launch: named records only; dry runs are never refused; `guard: false` turns every guard off). Create-only by default, natural keys, never deletes, globals: empty fields only, every locale with `fallbackLocale: false` onto the same row ids, skips records edited since seeding, writes with `context` `{ ipalSeed, disableRevalidate, ipalSkipIndexNow }`. Throws when `update` runs on production / a non-local database without `IPAL_SEED_ALLOW_PROD_UPDATE=1`. Both seed templates use it. | | `createSeeder(payload, { dryRun?, update?, force?, only?, guard?, editCheck?, context?, pagesCollection?, formsCollection?, label?, log? })` | `@intecion/ipal-kit/seed` | The seed rules in one place → `{ upsertBySlug, upsertBy, fillGlobal, summary, dryRun, update, force, local, only }`. `only` (or `SEED_ONLY`) = the records the run may touch (`pages:kontakt`, `forms:<title>`, `global:<slug>`, `pages:*`), the rest → `skip (not listed)`; a run without it against production / a non-local database throws unless `IPAL_SEED_ALLOW_FULL=1` / `guard.allowFull: true` (after launch: named records only; dry runs are never refused; `guard: false` turns every guard off). Create-only by default, natural keys, never deletes, globals: empty fields only, every locale with `fallbackLocale: false` onto the same row ids, skips records edited since seeding, writes with `context` `{ ipalSeed, disableRevalidate, ipalSkipIndexNow }`. Throws when `update` runs on production / a non-local database without `IPAL_SEED_ALLOW_PROD_UPDATE=1`. Both seed templates use it. |
| `upsertBySlug(collection, data \| data[], { slugField? })` | (method of `createSeeder`) | Matches by the default-locale slug; an array creates missing records first, so `{ $page: 'slug' }` links work in any order; `{ $form: 'title' }` = the form's id (`formsCollection`, default `forms`). | | `upsertBySlug(collection, data \| data[], { slugField? })` | (method of `createSeeder`) | Matches by the default-locale slug; an array creates missing records first, so `{ $page: 'slug' }` links work in any order; `{ $form: 'title' }` = the form's id (`formsCollection`, default `forms`). |
| `upsertBy(collection, key, data)` | (method) | Matches by other keys, e.g. a form by `{ title }`. | | `upsertBy(collection, key, data)` | (method) | Matches by other keys, e.g. a form by `{ title }`. |
| `fillGlobal(slug, data)` | (method) | Fills only empty fields of a global — a choice (select, radio, checkbox, number, date) still at its `defaultValue` counts as empty, text never does (all seeded fields with `update` when untouched). | | `fillGlobal(slug, data)` | (method) | Fills only empty fields of a global — a choice (select, radio, checkbox, number, date) still at its `defaultValue` counts as empty, text never does; a field already holding the seeded value is skipped (all seeded fields with `update` when untouched). |
| `summary()` | (method) | Prints and returns `{ created, skipped, updated, skippedEdited, errors }`; exit 1 when `errors`. | | `summary()` | (method) | Prints and returns `{ created, skipped, updated, skippedEdited, errors }`; exit 1 when `errors`. |
| `isLocalDatabase(url, { localHosts? })` | `@intecion/ipal-kit/seed` | `true` for SQLite files, Unix sockets, localhost / 127.0.0.1 / ::1 (+ `SEED_LOCAL_HOSTS`). | | `isLocalDatabase(url, { localHosts? })` | `@intecion/ipal-kit/seed` | `true` for SQLite files, Unix sockets, localhost / 127.0.0.1 / ::1 (+ `SEED_LOCAL_HOSTS`). |
| `writeLocales(payload, target, data, { context?, draft? })` | `@intecion/ipal-kit/seed` | Writes seed values (`{ pl, en }` per field) to `{ collection, id }` or `{ global }`: default locale first, then every other locale with `fallbackLocale: false` onto the same row ids. | | `writeLocales(payload, target, data, { context?, draft? })` | `@intecion/ipal-kit/seed` | Writes seed values (`{ pl, en }` per field) to `{ collection, id }` or `{ global }`: default locale first, then every other locale with `fallbackLocale: false` onto the same row ids. |
+1 -1
View File
@@ -203,7 +203,7 @@ is an error for that record, never an empty relation. Ready pages made of librar
|---|---| |---|---|
| `upsertBySlug(collection, data \| data[], { slugField })` | matches by the default-locale slug (`slugField`, default `slug`). With an array, missing records are created first (without fields that hold `$page` / `$form` links), then every record gets its full content, so pages can link to each other in any order | | `upsertBySlug(collection, data \| data[], { slugField })` | matches by the default-locale slug (`slugField`, default `slug`). With an array, missing records are created first (without fields that hold `$page` / `$form` links), then every record gets its full content, so pages can link to each other in any order |
| `upsertBy(collection, key, data)` | matches by other natural keys, e.g. `{ title: 'Contact form' }` (all keys must match, default locale) | | `upsertBy(collection, key, data)` | matches by other natural keys, e.g. `{ title: 'Contact form' }` (all keys must match, default locale) |
| `fillGlobal(slug, data)` | fills only the global's empty fields (the default locale decides). A choice — select, radio, checkbox, number, date — still at the field's `defaultValue` counts as empty (Payload stores defaults on the first save, so `defaultTheme: 'system'` can be seeded over `light`); text is never treated so, even when it equals a default. With `update` and an untouched global: every seeded field | | `fillGlobal(slug, data)` | fills only the global's empty fields (the default locale decides). A choice — select, radio, checkbox, number, date — still at the field's `defaultValue` counts as empty (Payload stores defaults on the first save, so `defaultTheme: 'system'` can be seeded over `light`); text is never treated so, even when it equals a default. A field that already holds exactly the seeded value is not written (`skip (exists)`), so re-running a seed of defaults changes nothing. With `update` and an untouched global: every seeded field |
| `summary()` | prints the summary and returns `{ created, skipped, updated, skippedEdited, errors }` | | `summary()` | prints the summary and returns `{ created, skipped, updated, skippedEdited, errors }` |
Each method prints one line per record (see [Running a seed](#running-a-seed)) and returns Each method prints one line per record (see [Running a seed](#running-a-seed)) and returns
@@ -560,7 +560,7 @@ Server only (`pnpm payload run`); never import it from client code, the Payload
| `createSeeder(payload, { dryRun?, update?, force?, only?, guard?, editCheck?, context?, pagesCollection?, formsCollection?, label?, log? })` | `@intecion/ipal-kit/seed` | The seed rules in one place → `{ upsertBySlug, upsertBy, fillGlobal, summary, dryRun, update, force, local, only }`. `only` (or `SEED_ONLY`) = the records the run may touch (`pages:kontakt`, `forms:<title>`, `global:<slug>`, `pages:*`), the rest → `skip (not listed)`; a run without it against production / a non-local database throws unless `IPAL_SEED_ALLOW_FULL=1` / `guard.allowFull: true` (after launch: named records only; dry runs are never refused; `guard: false` turns every guard off). Create-only by default, natural keys, never deletes, globals: empty fields only, every locale with `fallbackLocale: false` onto the same row ids, skips records edited since seeding, writes with `context` `{ ipalSeed, disableRevalidate, ipalSkipIndexNow }`. Throws when `update` runs on production / a non-local database without `IPAL_SEED_ALLOW_PROD_UPDATE=1`. Both seed templates use it. | | `createSeeder(payload, { dryRun?, update?, force?, only?, guard?, editCheck?, context?, pagesCollection?, formsCollection?, label?, log? })` | `@intecion/ipal-kit/seed` | The seed rules in one place → `{ upsertBySlug, upsertBy, fillGlobal, summary, dryRun, update, force, local, only }`. `only` (or `SEED_ONLY`) = the records the run may touch (`pages:kontakt`, `forms:<title>`, `global:<slug>`, `pages:*`), the rest → `skip (not listed)`; a run without it against production / a non-local database throws unless `IPAL_SEED_ALLOW_FULL=1` / `guard.allowFull: true` (after launch: named records only; dry runs are never refused; `guard: false` turns every guard off). Create-only by default, natural keys, never deletes, globals: empty fields only, every locale with `fallbackLocale: false` onto the same row ids, skips records edited since seeding, writes with `context` `{ ipalSeed, disableRevalidate, ipalSkipIndexNow }`. Throws when `update` runs on production / a non-local database without `IPAL_SEED_ALLOW_PROD_UPDATE=1`. Both seed templates use it. |
| `upsertBySlug(collection, data \| data[], { slugField? })` | (method of `createSeeder`) | Matches by the default-locale slug; an array creates missing records first, so `{ $page: 'slug' }` links work in any order; `{ $form: 'title' }` = the form's id (`formsCollection`, default `forms`). | | `upsertBySlug(collection, data \| data[], { slugField? })` | (method of `createSeeder`) | Matches by the default-locale slug; an array creates missing records first, so `{ $page: 'slug' }` links work in any order; `{ $form: 'title' }` = the form's id (`formsCollection`, default `forms`). |
| `upsertBy(collection, key, data)` | (method) | Matches by other keys, e.g. a form by `{ title }`. | | `upsertBy(collection, key, data)` | (method) | Matches by other keys, e.g. a form by `{ title }`. |
| `fillGlobal(slug, data)` | (method) | Fills only empty fields of a global — a choice (select, radio, checkbox, number, date) still at its `defaultValue` counts as empty, text never does (all seeded fields with `update` when untouched). | | `fillGlobal(slug, data)` | (method) | Fills only empty fields of a global — a choice (select, radio, checkbox, number, date) still at its `defaultValue` counts as empty, text never does; a field already holding the seeded value is skipped (all seeded fields with `update` when untouched). |
| `summary()` | (method) | Prints and returns `{ created, skipped, updated, skippedEdited, errors }`; exit 1 when `errors`. | | `summary()` | (method) | Prints and returns `{ created, skipped, updated, skippedEdited, errors }`; exit 1 when `errors`. |
| `isLocalDatabase(url, { localHosts? })` | `@intecion/ipal-kit/seed` | `true` for SQLite files, Unix sockets, localhost / 127.0.0.1 / ::1 (+ `SEED_LOCAL_HOSTS`). | | `isLocalDatabase(url, { localHosts? })` | `@intecion/ipal-kit/seed` | `true` for SQLite files, Unix sockets, localhost / 127.0.0.1 / ::1 (+ `SEED_LOCAL_HOSTS`). |
| `writeLocales(payload, target, data, { context?, draft? })` | `@intecion/ipal-kit/seed` | Writes seed values (`{ pl, en }` per field) to `{ collection, id }` or `{ global }`: default locale first, then every other locale with `fallbackLocale: false` onto the same row ids. | | `writeLocales(payload, target, data, { context?, draft? })` | `@intecion/ipal-kit/seed` | Writes seed values (`{ pl, en }` per field) to `{ collection, id }` or `{ global }`: default locale first, then every other locale with `fallbackLocale: false` onto the same row ids. |
+3 -1
View File
@@ -431,7 +431,9 @@ export function createSeeder(payload: BasePayload, options: SeederOptions = {}):
const fieldOf = (name: string) => (global?.flattenedFields as { defaultValue?: unknown; name?: string; type?: string }[] | undefined)?.find((f) => f.name === name) const fieldOf = (name: string) => (global?.flattenedFields as { defaultValue?: unknown; name?: string; type?: string }[] | undefined)?.find((f) => f.name === name)
const cur = await readSeedTarget(payload, target, defaultLocale) const cur = await readSeedTarget(payload, target, defaultLocale)
// Empty, or a choice still at its default value (e.g. Theme → light): never text an editor wrote. // Empty, or a choice still at its default value (e.g. Theme → light): never text an editor wrote.
const empty = Object.keys(data).filter((k) => isUnsetValue(cur[k], fieldOf(k))) // A field already holding exactly the seeded value is left alone (no write, reported as skip).
const same = (k: string) => JSON.stringify(cur[k]) === JSON.stringify(localize(data[k], defaultLocale, cur[k], codes))
const empty = Object.keys(data).filter((k) => isUnsetValue(cur[k], fieldOf(k)) && !same(k))
let keys = empty let keys = empty
let edited: null | string = null let edited: null | string = null
if (update) { if (update) {
+9
View File
@@ -256,6 +256,15 @@ describe('createSeeder', () => {
expect(first.note).toBe('empty fields filled: mode, toggle') expect(first.note).toBe('empty fields filled: mode, toggle')
const look = (await payload.findGlobal({ slug: 'look' as never })) as { greeting: string; mode: string; toggle: boolean } const look = (await payload.findGlobal({ slug: 'look' as never })) as { greeting: string; mode: string; toggle: boolean }
expect(look).toMatchObject({ greeting: 'Hello', mode: 'system', toggle: false }) expect(look).toMatchObject({ greeting: 'Hello', mode: 'system', toggle: false })
// Seeding exactly what is stored (here the default) writes nothing and reports a skip, every run.
await payload.updateGlobal({ slug: 'look' as never, data: { mode: 'light', toggle: true } as never })
const before = ((await payload.findGlobal({ slug: 'look' as never })) as { updatedAt: string }).updatedAt
for (let run = 0; run < 2; run++) {
expect(await seeder().fillGlobal('look', { mode: 'light', toggle: true })).toMatchObject({ status: 'skip (exists)' })
}
expect(((await payload.findGlobal({ slug: 'look' as never })) as { updatedAt: string }).updatedAt).toBe(before)
// A different seeded value still replaces a choice at its default.
expect(await seeder().fillGlobal('look', { mode: 'system', toggle: true })).toMatchObject({ note: 'empty fields filled: mode', status: 'create' })
// An editor's choice that differs from the default stays. // An editor's choice that differs from the default stays.
await payload.updateGlobal({ slug: 'look' as never, data: { mode: 'dark' } as never }) await payload.updateGlobal({ slug: 'look' as never, data: { mode: 'dark' } as never })
await seeder().fillGlobal('look', { mode: 'system' }) await seeder().fillGlobal('look', { mode: 'system' })