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
22 changes: 22 additions & 0 deletions .changeset/33812-seed-depends-on.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
'@objectstack/spec': minor
'@objectstack/metadata-protocol': minor
'@objectstack/runtime': minor
'@objectstack/cli': minor
---

feat(spec, metadata-protocol): a seed dataset can declare `dependsOn`, the objects it loads after, for an order no lookup field gives

Clause-②: yes (widening)
Metadata-protocol: added
Skills-docs: follow-up #34277 — `skills/objectstack-data/references/seeds.md` (its relationship section and its best-practice table tell the author to order seeds parent before child in the exported array, which orders nothing; `dependsOn` is not taught)

The seed loader orders a load's datasets by the `lookup`, `master_detail` and `user` fields between their objects; the order of the stack's `data` array only breaks ties. A dataset whose `beforeInsert` hook reads another seeded object had no way to say so, and its hook ran against whatever that object held at the moment, which an unrelated dataset could change. A rating scored from seeded bands, ordered by its one lookup to its rule, was written a wave before the bands and scored every row as if none existed.

- **`Seed.dependsOn: string[]` names objects.** Each name adds an edge to the same dependency graph the lookup fields feed, so the dataset's object loads after the named object's rows: `defineSeed(CustomerRating, { dependsOn: ['crm_rating_band'], records })`. The edges of several datasets of one object are pooled onto that object, and the returned `dependencyGraph.nodes[].dependsOn` lists them beside the lookup edges.
- **A load that declares none keeps its order exactly.** Nothing changes for an app that does not write the key.
- **A declaration the load cannot honour refuses the whole load, before any row is written.** The load answers `success: false` with one `errors[]` entry per refused declaration (`field: 'dependsOn'`, `sourceObject` the declaring object, `targetObject` the named one), and every caller reports it the way it reports any failed load. Three cases: a name that is the dataset's own object; a name no other dataset of the same load seeds (a misspelling, or an object whose datasets `env` or `locale` scoped out of this load, which the message says); and a declared edge that closes a cycle with the other edges, lookups included, with the cycle spelled out object by object. A cycle made of lookup fields alone still loads in deferred passes, as before.
- **A refused boot seed reads as a failed seed.** The single-tenant boot seed (`AppPlugin`) counts each error of a load refused before any dataset ran as a rejected row in the outcome it records, so `os dev` / `os serve` print the source with a warning on the banner's `Seeds:` line and the `objectstack:seed-settled` message answers `ok: false`; before, a refused load wrote nothing and read clean on both.
- **`defineSeed` refuses a dataset that names its own object** when it runs. The other two cases need the whole load, so only the loader can judge them.

A seed published on its own (a draft publish) is its own load, so a published seed whose `dependsOn` names an object seeded by another item is refused until the two are published together.
65 changes: 59 additions & 6 deletions content/docs/data-modeling/seed-data.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ const contactsSeed = defineSeed(Contact, {
],
});

// Export in dependency order — parents before children
// The loader puts accounts first because `contact.account` looks them up
export const SeedData = [accountsSeed, contactsSeed];
```

Expand Down Expand Up @@ -539,6 +539,56 @@ duplicates on every replay boot ([#3434](https://github.com/objectstack-ai/objec

---

## Load Order — `dependsOn`

A load orders its datasets by the relationships between their objects: a `lookup`,
`master_detail` or `user` field makes its object load after the object it points
at, however the datasets are listed. The order of your `data` array only breaks
ties between objects that are ready at the same moment.

Some dependencies are not fields. A `beforeInsert` hook that **reads** another
seeded object while a row is written (a rating scored from seeded bands, a price
computed from a seeded rate table) needs those rows to be there first, and no
lookup says so. Declare it with `dependsOn`, naming the objects:

```typescript
export const ratingBandsSeed = defineSeed(RatingBand, {
externalId: 'code',
records: [/* … */],
});

// The rating's beforeInsert hook scores each row from crm_rating_band rows,
// so this dataset must load after them.
export const customerRatingsSeed = defineSeed(CustomerRating, {
dependsOn: ['crm_rating_band'],
records: [/* … */],
});
```

Each name adds an edge to the same dependency graph the lookup fields feed. A
dataset that declares no `dependsOn` loads exactly where its lookups put it.

The loader refuses the **whole load**, writing no row, and reports why, when a
`dependsOn`:

- names the dataset's own object (`defineSeed` also refuses this when it runs);
- names an object no other dataset of the same load seeds: a misspelling, or a
dataset that `env` or `locale` scoped out of this load. A seed published on its
own is its own load, so publish it together with the seeds it depends on. Two
more loads carry only part of an app's seeds and refuse the same way: the dev
hot-reload seeder loads only the datasets of objects first seen in that reload,
so restart the dev server after adding a seed that depends on an object seeded
earlier, and a marketplace install loads one package's datasets, so name only
objects your own package seeds;
- closes a cycle with the other edges, lookups included: `crm_rating_rule`
declaring `dependsOn: ['crm_customer_rating']` while the rating looks up its
rule. No order can keep that edge.

A cycle made only of lookup fields is still loaded in several passes (each row is
written first and its missing reference filled in afterwards), as before.

---

## File Fields — `seedFile(path)`

A `file`, `image`, `avatar`, `video` or `audio` field stores the id of a stored
Expand Down Expand Up @@ -705,11 +755,11 @@ import { contactsSeed } from './contacts.seed';
import { leadsSeed } from './leads.seed';
import { productsSeed } from './products.seed';

/** All seed datasets — order determines load sequence */
/** All seed datasets — the loader orders them by their lookups and dependsOn */
export const SeedData = [
accountsSeed, // no dependencies
productsSeed, // no dependencies
contactsSeed, // depends on accounts
contactsSeed, // looks up accounts, so it loads after them
leadsSeed, // no dependencies
];
```
Expand Down Expand Up @@ -753,10 +803,12 @@ when the use case requires it — `upsert` for reference data the package keeps
(with `applyOn: 'create'` on the columns the customer or a job owns), or `replace`
for ephemeral cache tables.

### Seed in dependency order
### Declare a dependency no field expresses

Always export parent datasets before child datasets. If `contact` has a lookup to
`account`, `accountsSeed` must appear before `contactsSeed` in the exported array.
Lookup fields order the load for you: if `contact` has a lookup to `account`,
accounts load first wherever `accountsSeed` sits in the array. Listing parents
first keeps the file readable, but it orders nothing. When a hook reads another
seeded object, declare it with `dependsOn` (see "Load Order").

### Keep records realistic

Expand All @@ -783,6 +835,7 @@ function defineSeed<
columns?: { [field: string]: { applyOn?: 'create' | 'always' } }; // per-column write policy; default: 'always'
env?: Array<'prod' | 'dev' | 'test'>; // default: ['prod','dev','test']
locale?: string[]; // BCP-47 tags; omitted = every locale
dependsOn?: string[]; // objects this dataset loads after (see "Load Order")
records: Array<SeedRecord<TObj['fields']>>; // declared fields + injectable system columns
}
): Seed
Expand Down
2 changes: 2 additions & 0 deletions content/docs/references/api/package-api.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ Package manifest installed through POST /api/v1/packages (AUTHORING stage; scope
| **columns** | `Record<string, { applyOn?: Enum<'create' \| 'always'> }>` | optional | Per-column write policy, keyed by field name: `{ status: { applyOn: 'create' } }` writes `status` only when the seed creates the row. Omitted, every column is written on create and update. |
| **env** | `Enum<'prod' \| 'dev' \| 'test'>[]` | optional (default: `["prod","dev","test"]`) | Applicable environments |
| **locale** | `string[]` | optional | Applicable locales (BCP-47 tags); omitted applies to every locale. The boot seed filters on the app's i18n.defaultLocale; the marketplace install, sample-data reseed, restart heal and per-organization replay filter on the locale resolved for the load (the localization.locale setting when set, then the locale of the installing, reseeding or founding user, then the app's i18n.defaultLocale where the load names one); package apply and draft publish do not filter by locale — they load every dataset and warn |
| **dependsOn** | `string[]` | optional | Objects (by name) this dataset loads after, for an order no lookup field gives — e.g. a beforeInsert hook that reads another seeded object. Each name adds an edge to the load's dependency graph. The load is refused, writing nothing, when a name is the dataset's own object, is not seeded by another dataset of the same load, or closes a cycle with the other edges. Omitted, the order comes from lookup fields alone. |
| **records** | `Record<string, any>[]` | ✅ | Data records |
| **files** | `Record<string, { contentType: string; size: integer; sha256: string; data: string }>` | optional | Bytes of the package files the records name with `seedFile(path)`, keyed by path. Written by `os build`, which reads each path relative to the package root; the loader stores each one as a file the written record owns. |
| **_lock** | `Enum<'none' \| 'no-overlay' \| 'no-delete' \| 'full'>` | optional | Item-level lock — controls overlay & delete (ADR-0010). |
Expand Down Expand Up @@ -355,6 +356,7 @@ Package manifest installed through POST /api/v1/packages (AUTHORING stage; scope
| **columns** | `Record<string, { applyOn?: Enum<'create' \| 'always'> }>` | optional | Per-column write policy, keyed by field name: `{ status: { applyOn: 'create' } }` writes `status` only when the seed creates the row. Omitted, every column is written on create and update. |
| **env** | `Enum<'prod' \| 'dev' \| 'test'>[]` | optional (default: `["prod","dev","test"]`) | Applicable environments |
| **locale** | `string[]` | optional | Applicable locales (BCP-47 tags); omitted applies to every locale. The boot seed filters on the app's i18n.defaultLocale; the marketplace install, sample-data reseed, restart heal and per-organization replay filter on the locale resolved for the load (the localization.locale setting when set, then the locale of the installing, reseeding or founding user, then the app's i18n.defaultLocale where the load names one); package apply and draft publish do not filter by locale — they load every dataset and warn |
| **dependsOn** | `string[]` | optional | Objects (by name) this dataset loads after, for an order no lookup field gives — e.g. a beforeInsert hook that reads another seeded object. Each name adds an edge to the load's dependency graph. The load is refused, writing nothing, when a name is the dataset's own object, is not seeded by another dataset of the same load, or closes a cycle with the other edges. Omitted, the order comes from lookup fields alone. |
| **records** | `Record<string, any>[]` | ✅ | Data records |
| **files** | `Record<string, { contentType: string; size: integer; sha256: string; data: string }>` | optional | Bytes of the package files the records name with `seedFile(path)`, keyed by path. Written by `os build`, which reads each path relative to the package root; the loader stores each one as a file the written record owns. |
| **_lock** | `Enum<'none' \| 'no-overlay' \| 'no-delete' \| 'full'>` | optional | Item-level lock — controls overlay & delete (ADR-0010). |
Expand Down
1 change: 1 addition & 0 deletions content/docs/references/data/seed-loader.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ Seed loader request with datasets and configuration
| **columns** | `Record<string, { applyOn?: Enum<'create' \| 'always'> }>` | optional | Per-column write policy, keyed by field name: `{ status: { applyOn: 'create' } }` writes `status` only when the seed creates the row. Omitted, every column is written on create and update. |
| **env** | `Enum<'prod' \| 'dev' \| 'test'>[]` | optional (default: `["prod","dev","test"]`) | Applicable environments |
| **locale** | `string[]` | optional | Applicable locales (BCP-47 tags); omitted applies to every locale. The boot seed filters on the app's i18n.defaultLocale; the marketplace install, sample-data reseed, restart heal and per-organization replay filter on the locale resolved for the load (the localization.locale setting when set, then the locale of the installing, reseeding or founding user, then the app's i18n.defaultLocale where the load names one); package apply and draft publish do not filter by locale — they load every dataset and warn |
| **dependsOn** | `string[]` | optional | Objects (by name) this dataset loads after, for an order no lookup field gives — e.g. a beforeInsert hook that reads another seeded object. Each name adds an edge to the load's dependency graph. The load is refused, writing nothing, when a name is the dataset's own object, is not seeded by another dataset of the same load, or closes a cycle with the other edges. Omitted, the order comes from lookup fields alone. |
| **records** | `Record<string, any>[]` | ✅ | Data records |
| **files** | `Record<string, { contentType: string; size: integer; sha256: string; data: string }>` | optional | Bytes of the package files the records name with `seedFile(path)`, keyed by path. Written by `os build`, which reads each path relative to the package root; the loader stores each one as a file the written record owns. |
| **_lock** | `Enum<'none' \| 'no-overlay' \| 'no-delete' \| 'full'>` | optional | Item-level lock — controls overlay & delete (ADR-0010). |
Expand Down
1 change: 1 addition & 0 deletions content/docs/references/data/seed.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const result = SeedSchema.parse(data);
| **columns** | `Record<string, { applyOn?: Enum<'create' \| 'always'> }>` | optional | Per-column write policy, keyed by field name: `{ status: { applyOn: 'create' } }` writes `status` only when the seed creates the row. Omitted, every column is written on create and update. |
| **env** | `Enum<'prod' \| 'dev' \| 'test'>[]` | optional (default: `["prod","dev","test"]`) | Applicable environments |
| **locale** | `string[]` | optional | Applicable locales (BCP-47 tags); omitted applies to every locale. The boot seed filters on the app's i18n.defaultLocale; the marketplace install, sample-data reseed, restart heal and per-organization replay filter on the locale resolved for the load (the localization.locale setting when set, then the locale of the installing, reseeding or founding user, then the app's i18n.defaultLocale where the load names one); package apply and draft publish do not filter by locale — they load every dataset and warn |
| **dependsOn** | `string[]` | optional | Objects (by name) this dataset loads after, for an order no lookup field gives — e.g. a beforeInsert hook that reads another seeded object. Each name adds an edge to the load's dependency graph. The load is refused, writing nothing, when a name is the dataset's own object, is not seeded by another dataset of the same load, or closes a cycle with the other edges. Omitted, the order comes from lookup fields alone. |
| **records** | `Record<string, any>[]` | ✅ | Data records |
| **files** | `Record<string, { contentType: string; size: integer; sha256: string; data: string }>` | optional | Bytes of the package files the records name with `seedFile(path)`, keyed by path. Written by `os build`, which reads each path relative to the package root; the loader stores each one as a file the written record owns. |
| **_lock** | `Enum<'none' \| 'no-overlay' \| 'no-delete' \| 'full'>` | optional | Item-level lock — controls overlay & delete (ADR-0010). |
Expand Down
1 change: 1 addition & 0 deletions content/docs/references/kernel/manifest.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ Install mode: managed (registered as code, upgradeable, sealed) | unmanaged (cop
| **columns** | `Record<string, { applyOn?: Enum<'create' \| 'always'> }>` | optional | Per-column write policy, keyed by field name: `{ status: { applyOn: 'create' } }` writes `status` only when the seed creates the row. Omitted, every column is written on create and update. |
| **env** | `Enum<'prod' \| 'dev' \| 'test'>[]` | optional (default: `["prod","dev","test"]`) | Applicable environments |
| **locale** | `string[]` | optional | Applicable locales (BCP-47 tags); omitted applies to every locale. The boot seed filters on the app's i18n.defaultLocale; the marketplace install, sample-data reseed, restart heal and per-organization replay filter on the locale resolved for the load (the localization.locale setting when set, then the locale of the installing, reseeding or founding user, then the app's i18n.defaultLocale where the load names one); package apply and draft publish do not filter by locale — they load every dataset and warn |
| **dependsOn** | `string[]` | optional | Objects (by name) this dataset loads after, for an order no lookup field gives — e.g. a beforeInsert hook that reads another seeded object. Each name adds an edge to the load's dependency graph. The load is refused, writing nothing, when a name is the dataset's own object, is not seeded by another dataset of the same load, or closes a cycle with the other edges. Omitted, the order comes from lookup fields alone. |
| **records** | `Record<string, any>[]` | ✅ | Data records |
| **files** | `Record<string, { contentType: string; size: integer; sha256: string; data: string }>` | optional | Bytes of the package files the records name with `seedFile(path)`, keyed by path. Written by `os build`, which reads each path relative to the package root; the loader stores each one as a file the written record owns. |
| **_lock** | `Enum<'none' \| 'no-overlay' \| 'no-delete' \| 'full'>` | optional | Item-level lock — controls overlay & delete (ADR-0010). |
Expand Down
Loading
Loading