Skip to content

Commit 77ec41e

Browse files
committed
docs(seed-data): columns applyOn section; changeset for seed column applyOn
Claude-Session: https://claude.ai/code/session_018HE4VY52U3L5Bfzz51dsFv Co-authored-by: Claude <noreply@anthropic.com>
1 parent 1cfe06d commit 77ec41e

2 files changed

Lines changed: 81 additions & 1 deletion

File tree

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
---
2+
'@objectstack/spec': minor
3+
'@objectstack/metadata-protocol': minor
4+
'@objectstack/skills': minor
5+
---
6+
7+
feat(spec,metadata-protocol): a seed dataset column can be written only when its row is created (`columns.<field>.applyOn: 'create'`)
8+
9+
Clause-②: yes
10+
Metadata-protocol: added
11+
12+
A seed is replayed on every dev-server boot and every package re-publish, and an `upsert` replay re-asserts every column a record declares on the row it matched. That reverts the columns an app's own jobs write after the first seed (a status a payment job advances, a flag a renewal job sets), so a re-run undid the jobs' work with nothing in the log.
13+
14+
A seed dataset now takes a per-column write policy, keyed by field name:
15+
16+
```ts
17+
defineSeed(Instalment, {
18+
externalId: 'code',
19+
columns: {
20+
status: { applyOn: 'create' },
21+
is_expiring: { applyOn: 'create' },
22+
},
23+
records: [{ code: 'INS-001', amount: 1200, status: 'pending', is_expiring: false }],
24+
});
25+
```
26+
27+
- **`applyOn: 'create'`** writes the column when the seed creates the row and leaves it out of every write to a row the seed matched by its `externalId`: an `upsert` replay over an existing row, and every row of an `update` dataset. The matched row keeps the value it holds, while the `always` columns are refreshed. A row whose only differences are create-only columns is not written at all. A create-only reference column on a matched row is not resolved and not deferred to pass 2, so nothing later in the load writes it back. A column of the `externalId` is the key the row was matched by and stays in its update. `insert`, `ignore` and `replace` never update a matched row, so nothing changes on those modes.
28+
- **`applyOn: 'always'`** (the default, and every column without a `columns` entry) is the behaviour before this change.
29+
- The values, the default and the meaning are those of the import door's `FieldMappingEntry.applyOn`. Both doors now read one enum, exported as `ColumnApplyOnSchema` from `@objectstack/spec/data`. `SeedColumnSchema` (with `SeedColumn` / `SeedColumnParsed`) is the per-column shape.
30+
- A `columns` key must name a column the object has: a declared field, or a system column the platform injects on it such as `owner_id`. `defineSeed` refuses one that does not when it runs (and at compile time for an object literal), naming the key with a near miss. The seed loader warns once per dataset when a seed that did not come through `defineSeed` names one.
31+
- `SeedSchema` refuses an unknown key on a column (`unrecognized_keys`), and a dataset-level `applyOn` is pointed at `columns`.
32+
33+
`@objectstack/skills` only regenerates its schema reference indexes, which now list `SeedColumnSchema` and `ColumnApplyOnSchema`.

‎content/docs/data-modeling/seed-data.mdx‎

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,51 @@ so a typo like `'complete'` for `'completed'` still surfaces before boot.
147147

148148
---
149149

150+
## Columns Your App Owns After the First Seed — `applyOn: 'create'`
151+
152+
An `upsert` replay re-asserts every column a record declares on the row it
153+
matched. That is right for master data and wrong for a column the app itself
154+
owns once the row exists: a status a scheduled job advances, a flag a job sets
155+
after it sends a notice, a reference a workflow re-points. Replayed, the seed
156+
would put those back, undoing the job's work on every boot.
157+
158+
Declare such a column `applyOn: 'create'` under `columns`:
159+
160+
```typescript
161+
defineSeed(Instalment, {
162+
externalId: 'code',
163+
mode: 'upsert',
164+
columns: {
165+
status: { applyOn: 'create' }, // the payment job owns it after the first seed
166+
is_expiring: { applyOn: 'create' }, // the renewal job sets it
167+
},
168+
records: [
169+
{ code: 'INS-001', amount: 1200, status: 'pending', is_expiring: false },
170+
],
171+
});
172+
```
173+
174+
- When the seed **creates** the row, every column is written, `status` and
175+
`is_expiring` included.
176+
- When a replay **matches** an existing row, `status` and `is_expiring` are left
177+
out of the write: the row keeps the values the jobs gave it, while `amount`
178+
(`applyOn: 'always'`, the default) is refreshed. A row whose only differences
179+
are create-only columns is not written at all.
180+
- An `update` dataset never writes a create-only column. `insert`, `ignore` and
181+
`replace` never update a matched row, so the policy changes nothing there.
182+
- A create-only **reference** on a matched row is not resolved at all, so a
183+
replay neither re-points it nor reports it.
184+
- A column of the `externalId` is what the row was matched by, so it stays in
185+
the update whatever it declares.
186+
187+
`applyOn` takes the same values, with the same default and meaning, as an import
188+
mapping entry's [`applyOn`](/docs/data-modeling/import-mappings). A `columns` key
189+
must name a field of the object (or a system column the platform injects on it,
190+
such as `owner_id`): `defineSeed` refuses one that does not, and the loader warns
191+
when a stored seed names one.
192+
193+
---
194+
150195
## Environment Scoping
151196

152197
The `env` array controls which deployment environments receive the records. The
@@ -587,6 +632,7 @@ function defineSeed<
587632
config: {
588633
externalId?: string | string[]; // single field, or a composite list (join tables); default: 'name'
589634
mode?: 'insert' | 'update' | 'upsert' | 'replace' | 'ignore'; // default: 'upsert'
635+
columns?: { [field: string]: { applyOn?: 'create' | 'always' } }; // per-column write policy; default: 'always'
590636
env?: Array<'prod' | 'dev' | 'test'>; // default: ['prod','dev','test']
591637
locale?: string[]; // BCP-47 tags; omitted = every locale
592638
records: Array<SeedRecord<TObj['fields']>>; // declared fields + injectable system columns
@@ -596,7 +642,8 @@ function defineSeed<
596642

597643
`SeedRecord` keys are the object's declared fields (a `lookup` / `master_detail`
598644
field takes its target's natural-key string) plus the injectable system column
599-
names. Every record key is checked again when the call runs.
645+
names. Every record key, and every `columns` key, is checked again when the call
646+
runs.
600647

601648
The returned `Seed` object is a plain serialisable value — pass it to your
602649
stack's seed runner or store it in an export array.

0 commit comments

Comments
 (0)