Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .changeset/34798-skills-seed-modes-locale-order.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
'@objectstack/skills': patch
---

docs(skills): the seeds reference drops the retired seed modes `insert` / `replace`, names `locale`, `fallback` and `dependsOn`, and stops teaching array order as load order

Clause-②: no
Metadata-protocol: none

`objectstack-data`'s `references/seeds.md` no longer lists `insert` or `replace` as import modes: both are refused, and a dataset with no single natural key keeps the default mode with a composite `externalId` (a junction table's `['team', 'project']`), or `'id'` when the records carry their own ids. `objectstack-platform`'s Seed Data passage lists only `ignore`, `upsert` and `update`.

The seeds field table now names `locale` (the BCP-47 tags a dataset loads for), `fallback` (`true` on one locale-scoped set per object, which also loads for a resolved locale no set of that object lists) and `dependsOn`.

The relationship section says lookups order the load and the exported array's order only breaks ties; a lifecycle hook that reads a seeded object no lookup links to declares `dependsOn` with object names, and a name that is the dataset's own object, is not seeded by the same load, or closes a cycle refuses the whole load with nothing written. Two eval cases cover the composite-key conversion and `fallback`.

`seeds.md` pays first by deleting restatement in the same file (three best-practice rows, the `upsert` re-assert clause, "no other mode updates a matched row", "Natural keys remain the portable default"); its token ceiling moves +13, the measured remainder. `objectstack-platform/SKILL.md` re-locks 15 tokens lower.
22 changes: 20 additions & 2 deletions scripts/check-skills-token-ratchet.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -840,7 +840,11 @@ export const CEILINGS = new Map([
// so the LANDED priced text is 24649 bytes -> 6163 tokens: the ceiling moves
// 6145 -> 6163 (+18), the measured remainder only. Pinned AT the measurement.
// ceil(($(wc -c < skills/objectstack-platform/SKILL.md) - 20) / 4) == ceil(24649 / 4)
['skills/objectstack-platform/SKILL.md', 6163],
// 6163 -> 6148 (#34798): RE-LOCK at the landed count; the Seed Data passage
// no longer lists the retired modes `insert` and `replace`. 24649 -> 24591
// priced bytes; the row follows the measurement down, zero headroom.
// ceil(($(wc -c < skills/objectstack-platform/SKILL.md) - 20) / 4) == ceil(24591 / 4)
['skills/objectstack-platform/SKILL.md', 6148],
// `skills/objectstack-pm-dispatch/SKILL.md` (9708) and its
// `rules/dev-template.md` (1838) left this table on 2026-09-10 with the files:
// the maintainer deleted the published PM skill whole (verbatim:
Expand Down Expand Up @@ -1775,7 +1779,21 @@ export const CEILINGS = new Map([
// (ruling record 6513306068).
// 1747 -> 1746 (#34771): RE-LOCK at the landed count; the `defineSeed()` row of
// `### Seed best practices` says it is the TypeScript form. 6985 -> 6984 bytes.
['skills/objectstack-data/references/seeds.md', 1746],
// 1746 -> 1759 (#34798, #35070, #34277): the retired seed modes `insert` /
// `replace` leave the mode table and best practices, replaced by their
// conversion (a composite `externalId`, or `'id'`); the field table names
// `locale`, `fallback` and `dependsOn`; array order is no longer taught as
// load order — lookups order the load, and `dependsOn` covers a hook that
// reads a seeded object no lookup links to. Raise path the standing ruling
// 「A:先削重复,余额按实测提高」 (ruling record 6513306068, letter B). PAID DOWN
// FIRST — restatement cut in the same file: three best-practice rows that
// restated the `externalId`, mode and `env` passages, the `upsert`
// re-assert clause the mode table states, "no other mode updates a matched
// row", and "Natural keys remain the portable default". Arithmetic in bytes:
// 6984 -> 6570 after the cuts (-414); the teaching nets +466 -> 7036 ->
// ceil(7036 / 4) = 1759; headroom was 0, so the ceiling moves +13, the
// measured remainder only. Pinned AT the measurement.
['skills/objectstack-data/references/seeds.md', 1759],
// 1351 -> 1343: the body-pin values left the count (#31115, ruling B; see
// BODY_PIN_REPIN); the file's count fell by the same 8, headroom unchanged.
['skills/objectstack-platform/references/bootstrap.md', 1343],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,33 @@
{
"id": 4,
"prompt": "Seed the `country` table for every environment; add demo `opportunity` rows (dev/test only) closing 45 days after install, owned by the installer, and refreshed from the seed on every replay except `stage`: a nightly job advances it, and a replay must not reset it.",
"expected_output": "`defineSeed(Country, { externalId: 'code', mode: 'upsert', records })` for reference data; `defineSeed(Opportunity, { mode: 'upsert', env: ['dev', 'test'], ... })` for the refreshed demo rows — natural-key lookups, CEL values (`close_date: cel`daysFromNow(45)``, `owner_id: cel`os.user.id``), parents first; `columns: { stage: { applyOn: 'create' } }` so the `upsert` replay leaves the job-owned column (the `ignore` default would refresh nothing).",
"expected_output": "`defineSeed(Country, { externalId: 'code', mode: 'upsert', records })` for reference data; `defineSeed(Opportunity, { mode: 'upsert', env: ['dev', 'test'], ... })` for the refreshed demo rows — natural-key lookups, CEL values (`close_date: cel`daysFromNow(45)``, `owner_id: cel`os.user.id``), lookups (not array order) ordering the load; `columns: { stage: { applyOn: 'create' } }` so the `upsert` replay leaves the job-owned column (the `ignore` default would refresh nothing).",
"files": [],
"assertions": {
"must_contain": ["defineSeed", "externalId", "upsert", "env:", "daysFromNow", "applyOn: 'create'"],
"must_not_contain": ["new Date(", "defineDataset", "externalId: 'id'"]
}
},
{
"id": 30,
"prompt": "Seed demo rows into our `project_member` junction table (a `project` lookup and a `user` lookup; no other field names a row), dev/test only. The rows are loaded once: a restart must not duplicate them.",
"expected_output": "`defineSeed(ProjectMember, { externalId: ['project', 'user'], env: ['dev', 'test'], records })` with `mode` left at the default `ignore`: each replay matches a row on the composite of its two resolved lookups and inserts only the missing ones. Neither the `insert` nor the `replace` mode: both are refused, because every replay re-inserted each record; a composite `externalId` (or `'id'` when the records carry their own ids) is the conversion the refusal names.",
"files": [],
"assertions": {
"must_contain": ["defineSeed", "externalId"],
"must_not_contain": ["'insert'", "'replace'"]
}
},
{
"id": 31,
"prompt": "Our `price_book` seed ships a Chinese set for `zh-CN` and an English set. An organization whose locale resolves to `de-DE` installs the package and should get the English price book, not an empty table. How do we declare the two sets?",
"expected_output": "Two locale-scoped datasets: `defineSeed(PriceBook, { locale: ['zh-CN'], records })` and `defineSeed(PriceBook, { locale: ['en', 'en-US'], fallback: true, records })`. Without `fallback`, a `de-DE` load gets none of the object's locale-scoped rows, since no set lists that locale. `fallback: true` makes the English set also load for a resolved locale that no locale-scoped set of `price_book` lists. It needs `locale` (a set without one already loads for every locale), and one set per object carries it.",
"files": [],
"assertions": {
"must_contain": ["locale", "fallback"],
"must_not_contain": []
}
},
{
"id": 5,
"prompt": "Finance may edit a contract's legal terms (`governing_law`, `liability_cap`) while it is a draft, not once approved, signing or active; legal still edits them.",
Expand Down
43 changes: 23 additions & 20 deletions skills/objectstack-data/references/seeds.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,21 @@ export const categorySeed = defineSeed(Category, {
],
});

export const SeedData = [statusSeed, categorySeed]; // parents first
export const SeedData = [statusSeed, categorySeed];
```

### `Seed` fields

| Field | Default | Purpose |
|:------|:--------|:--------|
| `object` | derived | Auto-set from `objectDef.name` — never write manually |
| `externalId` | `'name'` | Stable business key every keyed mode matches on |
| `externalId` | `'name'` | Stable business key (or field list) every mode matches on |
| `mode` | `'ignore'` | Import strategy (see below) |
| `columns` | — | Per-column write policy, keyed by field: `{ FIELD: { applyOn: 'create' } }` (below) |
| `env` | `['prod','dev','test']` | Environments where the seed loads |
| `locale` | — (every locale) | BCP-47 tags it loads for; a locale no set of the object names gets none |
| `fallback` | — | `true` on ONE `locale` set per object: also loads for a locale no set names |
| `dependsOn` | — | Objects it loads after when no lookup links them (below) |
| `records` | — | `Partial<Record<keyof object.fields, unknown>>[]` |

Full Zod shape: `node_modules/@objectstack/spec/src/data/seed.zod.ts`.
Expand All @@ -59,21 +62,21 @@ Full Zod shape: `node_modules/@objectstack/spec/src/data/seed.zod.ts`.
|:-----|:---------|:--------|
| `ignore` (default) | Create-only: insert a missing row, leave a matched row as is. | Data the customer owns |
| `upsert` | Insert if missing; every replay writes declared columns back. | Reference data kept current |
| `insert` | Insert every record, no match: duplicates on each replay. | Rows loaded once |
| `update` | Update only existing rows; never create. | Patching existing config |
| `replace` ⚠️ | Insert with no match; deletes nothing, the caller clears the table. | Cache / lookup tables only |

`insert` / `replace` are refused (every replay re-inserted each record). With
no single natural key, keep the default mode and set a composite `externalId`
(`['team', 'project']`), or `'id'` when the records carry their own ids.

A seed replays on each boot, install and re-publish. AppPlugin's boot and organization
replays (not a publish or package install) do not re-insert a keyed row a
person deleted; `seed-replayer` with `{ reseed: true }` restores it.

### Columns the app owns after the first load — `columns.FIELD.applyOn`

An `upsert` replay re-asserts every column on the row it matched — wrong for
a column a job or an admin advances after the first load. Declare it
For a column a job or an admin advances after the first load, declare
`applyOn: 'create'`: written when the seed creates the row, left as the row
holds it on every replay (`update` never writes it; no other mode updates a
matched row).
holds it on every replay (`update` never writes it).
Each key must be a field of the object or an injected system column —
`defineSeed` refuses one that is not. Same values and meaning as an import
mapping entry's `applyOn` (objectstack-api).
Expand All @@ -89,8 +92,8 @@ defineSeed(Invoice, {

### `externalId` selection

Pick a stable natural key. **Never use `id`** — UUIDs differ across
environments.
Pick a stable natural key. **Never use `id`** for generated UUIDs — they
differ across environments.

| Scenario | Key |
|:---------|:----|
Expand All @@ -102,14 +105,14 @@ environments.
### Relationship references

For `lookup` fields, supply the **natural key** of the target record (not
its UUID). The seed runner resolves at load time. Order seeds so parents
appear before children in the exported array:
its UUID). The loader resolves it at load time, and lookups order the load:
a child loads after its parent however the array lists them (array order
only breaks ties).

> If a lookup value matches no natural key, the loader falls back to
> resolving it as the target's `id` — so a reference to an existing record
> by internal id resolves instead of dangling to null. Natural keys remain
> the portable default; rely on the id fallback only for records you didn't
> seed (e.g. a system user).
> by internal id resolves instead of dangling to null. Rely on it only for
> records you didn't seed (e.g. a system user).

```typescript
const contacts = defineSeed(Contact, {
Expand All @@ -122,6 +125,11 @@ const contacts = defineSeed(Contact, {
});
```

A `beforeInsert` hook reading a seeded object no lookup links to needs
`dependsOn: ['crm_rating_band']` (object names). Naming the dataset's own
object, one no dataset of the load seeds (misspelt, or `env`/`locale`-scoped
out), or a cycle refuses the whole load; nothing is written.

### Dynamic values (CEL)

Any field value may be a CEL expression evaluated at install time against
Expand Down Expand Up @@ -156,11 +164,6 @@ produce byte-identical `dist/objectstack.json`.
| Practice | Why |
|:---------|:----|
| TypeScript: `defineSeed()`, never `SeedSchema.parse()` | Lose compile-time field checks otherwise |
| Prefer natural keys (`code` / `email` / `slug`) | Portable across environments |
| Leave `mode` unset; `upsert` only for reference data | Replays keep customer edits |
| Scope demo data with `env: ['dev','test']` | Keep noise out of prod |
| Order seeds parent → child in the exported array | References resolve at load time |
| `replace` only on cache tables the caller clears | It never deletes: duplicates otherwise |

**Fictional is fine; malformed is not.** A seed row is written through the same
field validation as a user's insert, and a value the field type refuses never
Expand Down
4 changes: 2 additions & 2 deletions skills/objectstack-platform/SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -374,8 +374,8 @@ The stack's `data:` collection is authored with `defineSeed()`, which
**objectstack-data** owns — go there for `externalId` matching, `env:` scoping,
and which keys are derived.

`mode` (`ignore` default · `upsert` · `insert` · `update` · `replace` ⚠️ never
deletes, duplicates) decides what a replay does to existing rows:
`mode` (`ignore` default · `upsert` · `update`) decides what a replay does
to existing rows:
[Import modes](../objectstack-data/references/seeds.md#import-modes).

### Demo accounts — `demoStaff` vs `devLogins`
Expand Down
Loading