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
12 changes: 12 additions & 0 deletions .changeset/34201-skills-lookup-filter-record-reference.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
'@objectstack/skills': patch
---

docs(skills): `objectstack-data` teaches the `lookupFilters` `{ $record }` reference and `orEmpty`

Clause-②: no
Metadata-protocol: none

objectstack-data's `rules/field-types.md` now teaches a lookup filter that reads the record being edited. A `lookupFilters` value may be `{ $record: 'field' }`, or one lookup hop away, `{ $record: 'lookup_field.field' }` (at most one dot). It is the whole `value`, never a member of an `in` / `notIn` list, and pairs only with `eq`, `ne`, `gt`, `lt`, `gte`, `lte`. Only a record form resolves it; every other picker host leaves the entry out, and while the referenced value is empty the entry is dropped. `orEmpty: true` also matches a candidate whose column is null, with the same six operators. The picker re-queries when a referenced field changes, so that field is not also listed in `dependsOn`, which would add an equality filter on it. The section's example is now a quote's price-book lookup.

Five restatements in the same file are cut to pay for part of it (the `secret` row's recommendation, which the `password` row states; the `autonumber` row's token summary and second format example, which the Autonumber section carries; and two repeats of "pad width is a minimum"). The data-modeling docs pages `relationships.mdx` and `fields.mdx` teach the same four points.
30 changes: 29 additions & 1 deletion content/docs/data-modeling/fields.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,14 @@ primary_contact: Field.lookup('contact', {
],
}),

// Filtered by the record being edited: books valid on the quote's date, or with no start date
price_book: Field.lookup('price_book', {
label: 'Price Book',
lookupFilters: [
{ field: 'valid_from', operator: 'lte', value: { $record: 'quote_date' }, orEmpty: true },
],
}),

// A pick fills sibling fields: the picked product's list_price lands in unit_price
product: Field.lookup('product', {
label: 'Product',
Expand All @@ -238,13 +246,33 @@ order: Field.masterDetail('order', {
| Property | Type | Description |
| :--- | :--- | :--- |
| `reference` (1st arg) | `string` | Target object name |
| `lookupFilters` | `{ field, operator, value }[]` | Base filters restricting which records are selectable (structured, picker-honored) |
| `lookupFilters` | `{ field, operator, value, orEmpty? }[]` | Base filters restricting which records are selectable (structured, picker-honored). `value` is a literal or a `{ $record }` reference — see below |
| `dependsOn` | `(string \| { field, param? })[]` | Sibling fields that scope the candidates by equality; the picker is gated until they are set |
| `deleteBehavior` | `enum` | `'set_null'`, `'cascade'`, `'restrict'` |
| `inlineEdit` | `boolean \| 'grid' \| 'form'` | Render child records inline on the parent create/edit form (`true` = auto-pick, `'grid'`, or `'form'`) |
| `inlineColumns` | `array` | Optional explicit columns for the inline grid |
| `inlineAmountField` | `string` | Optional numeric child field for the inline running total |
| `lookupAutofill` | `{ target, source, overwrite? }[]` | Fields of this object a pick fills from the picked record (single-value `lookup` / `master_detail` only) — see below |

#### Lookup filters that read the record being edited

A `lookupFilters` `value` can be `{ $record: 'field' }`, or one lookup hop away,
`{ $record: 'lookup_field.field' }`: a value of the record being edited, which the picker
of a record form sends in place of the reference.

- **The reference** is the whole `value` and pairs only with `eq`, `ne`, `gt`, `lt`, `gte`
or `lte`. Outside a record form (inline edit, an action's parameter dialog, a bulk action)
the entry is left out.
- **An empty value** drops the entry: while the referenced value is empty or cannot be read,
the other entries alone filter.
- **`orEmpty: true`** also matches a candidate whose column is null; it pairs with the same
six operators.
- **Not `dependsOn`.** The picker re-queries when a referenced field changes. Listing that
field in `dependsOn` as well would add an equality filter on it to the candidate query.

Details and a fuller example:
[Filtered Lookups](/docs/data-modeling/relationships#filter-by-the-record-being-edited).

#### Lookup-pick autofill: `lookupAutofill`

A single-value `lookup` / `master_detail` can declare `lookupAutofill: [{ target,
Expand Down
41 changes: 41 additions & 0 deletions content/docs/data-modeling/relationships.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,47 @@ contact: Field.lookup('contact', {
structured `lookupFilters` (`{ field, operator, value }`) and `dependsOn` shown above instead.
</Callout>

#### Filter by the record being edited

A `lookupFilters` value can read the record the form is editing instead of a literal:
`{ $record: 'field' }`, or one lookup hop away, `{ $record: 'lookup_field.field' }` (at most
one dot). The picker puts that value in its place before it queries the candidates.

```typescript
// On quote: only price books valid on the quote's date,
// in the customer's market or in none (a generic book)
price_book: Field.lookup('price_book', {
label: 'Price Book',
lookupFilters: [
{ field: 'is_active', operator: 'eq', value: true },
{ field: 'valid_from', operator: 'lte', value: { $record: 'quote_date' }, orEmpty: true },
{ field: 'valid_to', operator: 'gte', value: { $record: 'quote_date' }, orEmpty: true },
{ field: 'market', operator: 'eq', value: { $record: 'account.market' }, orEmpty: true },
],
})
```

- **The reference.** `{ $record }` is the whole `value`, never a member of an `in` / `notIn`
list, and pairs only with `eq`, `ne`, `gt`, `lt`, `gte` or `lte`. `os validate` refuses a
path that names no field of the object the lookup is on, or of the object its hop points at.
- **Where it resolves.** In a record form, whatever its layout. Every other picker host leaves
every `{ $record }` entry out: inline edit on a record page, a record page's header or
related list, an action's parameter dialog, and a bulk action over selected rows.
- **An empty value.** While the referenced value is empty (null, unset, an empty string, or a
hop whose lookup is unset) or cannot be read, the entry is left out of the candidate query:
a quote whose account has no market is offered every book the other entries admit.
- **`orEmpty: true`** makes the entry also match a candidate whose column is null ("valid from
the quote date, or with no start date"). Like `{ $record }`, it pairs only with `eq`, `ne`,
`gt`, `lt`, `gte` or `lte`.
- **`dependsOn` or `$record`?** `dependsOn: ['account']` scopes the candidates by equality on
the parent field and gates the picker until that field is set. A `$record` entry compares
any candidate column, with any of the six operators, and is left out while its value is
empty. The picker re-queries whenever a referenced field changes, so do not also list that
field in `dependsOn`: `dependsOn: ['quote_date']` would add `quote_date = …` to the query
over the referenced object.

The picker narrows the choice only: a rule the save must enforce belongs on the server too.

### Self-Referencing Lookups

Create hierarchies:
Expand Down
17 changes: 16 additions & 1 deletion scripts/check-skills-token-ratchet.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -909,7 +909,22 @@ export const CEILINGS = new Map([
// zero headroom, ceiling +141, pinned AT the measurement. Standing ruling (2026-10-27,
// tap A, verbatim and untranslated): 「A:先削重复,余额按实测提高」; recognised path per
// ruling record 6513306068; seat widening record 6784270263.
['skills/objectstack-data/rules/field-types.md', 3349],
// 3349 -> 3553 (#34201, 2026-11-16): § Lookup teaches the `lookupFilters`
// `{ $record: 'path' }` reference (a field of the record being edited, or one lookup
// hop), its six operators, the record-form-only resolution, the dropped entry on an
// empty value, `orEmpty: true`, and why a `$record` field is not also listed in
// `dependsOn`; the section's one example becomes a `price_book` lookup. +994 bytes.
// PAID DOWN FIRST by five same-file restatement cuts, 179 bytes (41 + 51 + 24 + 43 +
// 20): the `secret` row's "recommended type for credentials" (home: the `password`
// row, "Prefer `secret` for credentials"); the `autonumber` row's token-and-scope
// parenthetical, cut to a pointer, and its second `format` example (home: § Autonumber,
// the token table and the scope paragraph); rule 3's "Size it for readability, not as
// a ceiling" (home: the same rule's heading); the token table's "(minimum width)"
// (home: rule 3). `wc -c` = 14210 bytes -> ceil(14210 / 4) = 3553 tokens, zero
// headroom, ceiling +204, pinned AT the measurement. Standing ruling (2026-10-27,
// tap A, verbatim and untranslated): 「A:先削重复,余额按实测提高」; recognised path per
// ruling record 6513306068.
['skills/objectstack-data/rules/field-types.md', 3553],
['skills/objectstack-data/rules/indexing.md', 3241],
// 1590 -> 1909 (#32364, 2026-11-11): § Sealing (`seal`) teaches `lifecycle.seal`
// (+1574 B). PAID DOWN FIRST by three same-file restatements (-296 B): the ❌
Expand Down
27 changes: 20 additions & 7 deletions skills/objectstack-data/rules/field-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const fields = {
| `url` | Web URLs — built-in format validation | `required` |
| `phone` | Phone numbers | `format` |
| `password` | ⚠️ Masked-on-read input. On a generic object the value is stored **PLAINTEXT at rest** (never hashed — one-way hashing applies only inside the auth subsystem's own identity tables). Prefer `secret` for credentials; a generic `password` field triggers a build warning | `minLength`, `maxLength` |
| `secret` | Reversible **encrypted-at-rest** credential (DB password, API key, token) — encrypted on write via `ICryptoProvider`, masked on read, fail-closed (ADR-0100). **The recommended type for credentials** | — |
| `secret` | Reversible **encrypted-at-rest** credential (DB password, API key, token) — encrypted on write via `ICryptoProvider`, masked on read, fail-closed (ADR-0100) | — |
| `markdown` | Markdown-formatted content | `maxLength` |
| `html` | Raw HTML content | `maxLength` |
| `richtext` | WYSIWYG rich text editor | `maxLength` |
Expand Down Expand Up @@ -129,7 +129,7 @@ Stored as JSON on the parent row — no separate table / FK:
|:-----|:------------|:-------|
| `formula` | Computed on read: neither aggregatable nor a dataset or cube dimension (group key); both need a stored field of its return type with the same `expression` (objectstack-ui → Dashboards → Dataset-Bound Widgets) that reads only its own row (relationships.md → Computed column) | `expression` (CEL, `record.` prefixes), `returnType` (`'number' \| 'text' \| 'boolean' \| 'date' \| 'currency'`); `'currency'` carries its own `currencyConfig` |
| `summary` | Roll-up aggregation from child records | `summaryOperations` ({ object, field? (omit for `count`), function, relationshipField?, filter? }) |
| `autonumber` | Auto-incrementing display format ({0000} counter + optional date / {field} tokens, resets per scope) | `format` (shorthand) / `autonumberFormat` (canonical) — e.g., `"CASE-{0000}"`, `"AD{YYYYMMDD}{0000}"` |
| `autonumber` | Auto-incrementing display format (→ Autonumber) | `format` (shorthand) / `autonumberFormat` (canonical) — e.g., `"CASE-{0000}"` |

## Enhanced Types

Expand Down Expand Up @@ -211,15 +211,28 @@ delete the lines and purge the recycle bin, or move them to another invoice.

```typescript
{
type: 'lookup',
reference: 'account',
type: 'lookup', // price_book, on quote
reference: 'price_book',
required: true,
lookupFilters: [
{ field: 'status', operator: 'eq', value: 'active' },
{ field: 'is_active', operator: 'eq', value: true },
// the record being edited: a field, or one lookup hop (at most one dot)
{ field: 'valid_from', operator: 'lte', value: { $record: 'quote_date' }, orEmpty: true },
{ field: 'market', operator: 'eq', value: { $record: 'account.market' }, orEmpty: true },
],
}
```

**`{ $record: 'path' }`** is the whole `value` (never an `in` / `notIn` member) and,
like `orEmpty`, pairs only with `eq`, `ne`, `gt`, `lt`, `gte`, `lte`. Only a record
form resolves it; every other picker host (inline edit, an action dialog, a bulk
action) leaves the entry out. **Empty value:** while the referenced value is empty
or unreadable, the entry is dropped. **`orEmpty: true`** also matches a candidate
whose column is null. **Not `dependsOn`:** the picker re-queries when a `$record`
field changes; listing it in `dependsOn` adds an equality filter (`quote_date = …`)
on the referenced object. The picker only narrows the choice: a rule the save must
enforce belongs on the server too.

### Formula

```typescript
Expand All @@ -238,7 +251,7 @@ The `format` is literal text interleaved with `{...}` tokens:

| Token | Renders | Example |
|:------|:--------|:--------|
| `{0000}` | The counter, zero-padded to that many digits (**minimum** width). At most ONE slot. | `CASE-{0000}` → `CASE-0042` |
| `{0000}` | The counter, zero-padded to that many digits. At most ONE slot. | `CASE-{0000}` → `CASE-0042` |
| `{YYYY}` `{YY}` `{MM}` `{DD}` `{YYYYMMDD}` | Generation date in the request's business timezone | `AD{YYYYMMDD}{0000}` → `AD202606170001` |
| `{field_name}` | The value of another field **on the same record** | `{plan_no}{000}` → `PLAN-001001` |

Expand All @@ -248,5 +261,5 @@ The `format` is literal text interleaved with `{...}` tokens:

1. **Every `{field}` you interpolate must be `required: true`** and set before the record is created. An empty interpolated field makes the record number generation *throw* (the compile lint flags a non-existent field as an error, an optional one as a warning).
2. **Put a delimiter between adjacent variable tokens** — `{section}-{zone}{000}`, not `{section}{zone}{000}`. Without one, `('AB','C')` and `('A','BC')` both render prefix `ABC` and share a counter (to keep numbers unique).
3. **Pad width is a MINIMUM, not a cap.** `{000}` → `001`…`999`, then `1000` (it grows, never wraps). Size it for readability, not as a ceiling.
3. **Pad width is a MINIMUM, not a cap.** `{000}` → `001`…`999`, then `1000` (it grows, never wraps).
4. **Only known tokens are interpolated.** Date tokens are **case-sensitive** — `{yyyy}` parses as a `{field}` reference and renders **empty** (rule 1 applies). Only a spelling no field could have — `{ YYYY }`, `{YYYY-MM}` — is emitted **literally**.
Loading