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
8 changes: 8 additions & 0 deletions .changeset/34986-skills-seed-rows-obey-validation-rules.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
'@objectstack/skills': patch
---

docs(skills): `objectstack-data`'s seeds reference says seed rows obey the object's validation rules (all but `state_machine`) as they load: a rule reading a parent roll-up sees the total earlier rows left, a seeded roll-up value is stored as written, and `os verify`, not `os validate`, fails on a dropped row. One eval case covers it; the file pays in deleted restatement and its token ceiling does not move.

Clause-②: no
Metadata-protocol: none
5 changes: 3 additions & 2 deletions content/docs/data-modeling/seed-data.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -841,7 +841,7 @@ export a single aggregate array.
```
src/
data/
index.ts ← exports SeedData array in dependency order
index.ts ← exports the SeedData array (the loader orders it)
accounts.seed.ts
contacts.seed.ts
leads.seed.ts
Expand Down Expand Up @@ -920,7 +920,8 @@ company names, email addresses, and values — not `foo`, `bar`, or `test123`.
### One file per object

Split large seed payloads into `{object}.seed.ts` files. A single `index.ts` that
re-exports and orders them keeps the entry point clean.
re-exports them keeps the entry point clean; lookups and `dependsOn` order the
load, and array position only breaks ties.

---

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,22 @@
"must_contain": ["CrossDatasourceTransactionWriteError", "default datasource"],
"must_not_contain": []
}
},
{
"id": 32,
"prompt": "`payment` has a validation rule that refuses a payment above the invoice's open balance (`record.amount > record.invoice.amount - record.invoice.amount_paid`); `invoice.amount_paid` is a `sum` roll-up of its payments. Seed demo data: invoice `INV-1` for 4800, fully paid in two payments, and `INV-2` for 5000 with 2000 paid.",
"expected_output": "Two `defineSeed` sets. The invoice rows carry `number` and `amount` and no roll-up value: a seed write stores a supplied `amount_paid` as written, the rule then reads a balance already spent, and both payments on the fully paid `INV-1` are refused (the 2000 on `INV-2` still fits its 3000 open balance). The payments name their invoice by natural key (`invoice: 'INV-1'`) and each fits what the rows before it left: two that sum to 4800 on `INV-1`, one of 2000 on `INV-2`; the roll-up then computes the paid totals. Does not say seed rows skip validation rules (only `state_machine` is skipped) and does not weaken the rule to make the seed load; `os verify` fails on a dropped seed row.",
"files": [],
"assertions": {
"must_contain": [
"defineSeed",
"invoice: 'INV-1'"
],
"must_not_contain": [
"amount_paid: 4800",
"amount_paid: 2000"
]
}
}
]
}
36 changes: 20 additions & 16 deletions skills/objectstack-data/references/seeds.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
## Seed Data & Fixtures (`defineSeed()`)

Object definition and seed data live together — a `*.object.ts` usually
pairs with a `*.seed.ts` (fixtures, reference rows, bootstrap data).
pairs with a `*.seed.ts`.

> The factory is `defineSeed` — **not** `defineDataset`, which is the
> unrelated ADR-0021 analytics semantic layer (`@objectstack/spec/ui`).
Expand Down Expand Up @@ -85,42 +85,40 @@ mapping entry's `applyOn` (objectstack-api).
defineSeed(Invoice, {
externalId: 'number',
mode: 'upsert',
columns: { status: { applyOn: 'create' } }, // the payment job owns it after the first seed
columns: { status: { applyOn: 'create' } },
records: [{ number: 'INV-001', status: 'draft', amount: 1200 }],
});
```

### `externalId` selection

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

| Scenario | Key |
|:---------|:----|
| Named entities (country, currency) | `'code'` / `'slug'` |
| Users / contacts | `'email'` |
| Externally sourced | `'external_id'` |
| Generic | `'name'` (default) |

### Relationship references

For `lookup` fields, supply the **natural key** of the target record (not
its UUID). The loader resolves it at load time, and lookups order the load:
For `lookup` fields, supply the target record's **natural key**. The
loader resolves it, 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. Rely on it only for
> records you didn't seed (e.g. a system user).
> resolving it as the target's `id`. Rely on it only for records you didn't
> seed (e.g. a system user).

```typescript
const contacts = defineSeed(Contact, {
externalId: 'email',
records: [{
email: 'john@acme.example.com',
first_name: 'John',
account: 'Acme Corporation', // natural key of an Account record
account: 'Acme Corporation',
}],
});
```
Expand All @@ -130,15 +128,21 @@ A `beforeInsert` hook reading a seeded object no lookup links to needs
object, one no dataset of the load seeds (misspelt, or `env`/`locale`-scoped
out), or a cycle refuses the whole load; nothing is written.

**Seed rows obey the object's validation rules** (all but `state_machine`); a
refused row never lands. `os validate` does not run them on seed rows;
`os verify` fails on each dropped row. A rule reading a parent roll-up
(`record.invoice.amount_paid`) sees the total earlier rows left, so children
must fit in record order. ⛔ Never seed the roll-up: a seeded `amount_paid` is
stored as written, and a cap on payments refuses those it counts.

### Dynamic values (CEL)

Any field value may be a CEL expression evaluated at install time against
a single per-load pinned `now`. This is the **only** correct way to author
time-based or identity-derived seed values — `new Date()` ships the package
author's clock to every customer and breaks build determinism.
author's clock to every customer.

```typescript
import { defineSeed } from '@objectstack/spec/data';
import { cel } from '@objectstack/spec';

defineSeed(Opportunity, {
Expand All @@ -165,10 +169,10 @@ produce byte-identical `dist/objectstack.json`.
|:---------|:----|
| TypeScript: `defineSeed()`, never `SeedSchema.parse()` | Lose compile-time field checks 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
lands — the table loads short or empty while sibling rows point at records that
do not exist. `os validate` / `os build` refuse such a value first, at the
**Fictional is fine; malformed is not.** A seed row passes the same field
validation as a user's insert; a refused value leaves the table short or empty
while sibling rows point at records that do not exist.
`os validate` / `os build` refuse such a value first, at the
record's field — run them before trusting the load. So never mask or
placeholder a sample value: no `138****4207`,
`***`, `XXX`, `N/A`, `—`, `TBD`. Invent a plausible well-formed one — `+86 138
Expand Down
Loading