@proveanything/smartlinks 2.1.40 → 2.1.41
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +754 -754
- package/dist/README.md +573 -573
- package/dist/adminCardsView.js +55 -55
- package/dist/api/collection.d.ts +8 -1
- package/dist/api/collection.js +10 -0
- package/dist/baseline.css +194 -194
- package/dist/docs/API_SUMMARY.md +863 -835
- package/dist/docs/admin-cards.md +189 -189
- package/dist/docs/agent-tools.md +156 -156
- package/dist/docs/ai-tools-and-skills.md +180 -180
- package/dist/docs/ai.md +1721 -1721
- package/dist/docs/analytics.md +39 -39
- package/dist/docs/app-data-storage.md +277 -277
- package/dist/docs/app-docs.md +73 -73
- package/dist/docs/app-manifest.md +852 -852
- package/dist/docs/app-records-pattern.md +388 -388
- package/dist/docs/appConfig.md +550 -550
- package/dist/docs/assistant-apps.md +172 -172
- package/dist/docs/assistant-context.md +119 -119
- package/dist/docs/bookings.md +442 -442
- package/dist/docs/comms.md +686 -686
- package/dist/docs/connectors.md +340 -340
- package/dist/docs/css-baseline.md +133 -133
- package/dist/docs/customer-guide.md +70 -70
- package/dist/docs/deploying-apps.md +340 -340
- package/dist/docs/entitlements.md +90 -90
- package/dist/docs/files.md +69 -69
- package/dist/docs/host-dependency-contract.md +210 -210
- package/dist/docs/integrations.md +322 -322
- package/dist/docs/interactions.md +545 -545
- package/dist/docs/item-context.md +181 -181
- package/dist/docs/knowledge.md +102 -102
- package/dist/docs/lots.md +111 -111
- package/dist/docs/loyalty.md +352 -352
- package/dist/docs/members.md +182 -182
- package/dist/docs/mobile-admin-container.md +693 -693
- package/dist/docs/operations.md +281 -281
- package/dist/docs/overview.md +482 -482
- package/dist/docs/proof-comms-triggers.md +142 -142
- package/dist/docs/proof-ownership-transfer.md +155 -155
- package/dist/docs/proof-product-data-scoping.md +462 -462
- package/dist/docs/proof-share-grants.md +305 -305
- package/dist/docs/realtime.md +138 -138
- package/dist/docs/records-admin-pattern.md +304 -304
- package/dist/docs/standard-query.md +187 -187
- package/dist/docs/tag-codes.md +97 -97
- package/dist/docs/theme-tokens.md +252 -252
- package/dist/docs/wallet-passes.md +107 -107
- package/dist/docs/websites.md +123 -123
- package/dist/openapi.yaml +80 -3
- package/dist/theme-boot.js +78 -78
- package/dist/theme.css +75 -75
- package/dist/types/appManifest.d.ts +21 -0
- package/dist/types/collection.d.ts +26 -0
- package/docs/API_SUMMARY.md +863 -835
- package/docs/admin-cards.md +189 -189
- package/docs/agent-tools.md +156 -156
- package/docs/ai-tools-and-skills.md +180 -180
- package/docs/ai.md +1721 -1721
- package/docs/analytics.md +39 -39
- package/docs/app-data-storage.md +277 -277
- package/docs/app-docs.md +73 -73
- package/docs/app-manifest.md +852 -852
- package/docs/app-records-pattern.md +388 -388
- package/docs/appConfig.md +550 -550
- package/docs/assistant-apps.md +172 -172
- package/docs/assistant-context.md +119 -119
- package/docs/bookings.md +442 -442
- package/docs/comms.md +686 -686
- package/docs/connectors.md +340 -340
- package/docs/css-baseline.md +133 -133
- package/docs/customer-guide.md +70 -70
- package/docs/deploying-apps.md +340 -340
- package/docs/entitlements.md +90 -90
- package/docs/files.md +69 -69
- package/docs/host-dependency-contract.md +210 -210
- package/docs/integrations.md +322 -322
- package/docs/interactions.md +545 -545
- package/docs/item-context.md +181 -181
- package/docs/knowledge.md +102 -102
- package/docs/lots.md +111 -111
- package/docs/loyalty.md +352 -352
- package/docs/members.md +182 -182
- package/docs/mobile-admin-container.md +693 -693
- package/docs/operations.md +281 -281
- package/docs/overview.md +482 -482
- package/docs/proof-comms-triggers.md +142 -142
- package/docs/proof-ownership-transfer.md +155 -155
- package/docs/proof-product-data-scoping.md +462 -462
- package/docs/proof-share-grants.md +305 -305
- package/docs/realtime.md +138 -138
- package/docs/standard-query.md +187 -187
- package/docs/tag-codes.md +97 -97
- package/docs/theme-lookbook.v1.json +120 -120
- package/docs/theme-tokens.md +252 -252
- package/docs/wallet-passes.md +107 -107
- package/docs/websites.md +123 -123
- package/openapi.yaml +80 -3
- package/package.json +87 -87
- package/scripts/headless-check.mjs +87 -87
- package/scripts/lib/bare-imports.mjs +57 -57
- package/scripts/lib/keyed-app-data.mjs +21 -21
- package/scripts/publish.mjs +272 -272
- package/scripts/register-release.mjs +91 -91
|
@@ -1,388 +1,388 @@
|
|
|
1
|
-
# SmartLinks App Records Pattern
|
|
2
|
-
|
|
3
|
-
> Canonical guide for microapps that store **per-product**, **per-facet**, **per-variant**, **per-batch**, or **rule-targeted** data.
|
|
4
|
-
>
|
|
5
|
-
> Audience: microapp developers (ingredients, nutrition, allergy, FAQs, recipes, warranty, provenance, …).
|
|
6
|
-
>
|
|
7
|
-
> Status: **standard**. New apps MUST follow this contract; existing apps SHOULD migrate.
|
|
8
|
-
>
|
|
9
|
-
> SDK: `@proveanything/smartlinks` ≥ **2.0** (R5).
|
|
10
|
-
> Admin shell (React only): `@proveanything/smartlinks-utils-ui` ≥ **2.0** — required for the admin side if using the React shell; not needed in public widgets.
|
|
11
|
-
|
|
12
|
-
---
|
|
13
|
-
|
|
14
|
-
## 0. TL;DR — pick your shape, then copy the snippet
|
|
15
|
-
|
|
16
|
-
Every records-based app fits into a 2×2:
|
|
17
|
-
|
|
18
|
-
| | **Singleton** (one record per scope) | **Collection** (many records per scope) |
|
|
19
|
-
| ------------------------ | ---------------------------------------------------- | ------------------------------------------------------------- |
|
|
20
|
-
| **Best-match (one wins)**| Ingredients, nutrition, washing instructions | _(rare — usually you want all)_ |
|
|
21
|
-
| **All matches (aggregate)** | _(rare — usually you want best)_ | FAQs, recipes, SOPs, care tips, story cards |
|
|
22
|
-
|
|
23
|
-
That choice drives **three** things and nothing else:
|
|
24
|
-
|
|
25
|
-
1. **Manifest:** `cardinality: 'singleton' | 'collection'` and `allowFacetRules: boolean`.
|
|
26
|
-
2. **Admin:** use `<RecordsAdminShell>` (React) or call the admin SDK functions directly. Pass `cardinality` + include `'rule'` in `scopes` if `allowFacetRules`.
|
|
27
|
-
3. **Public widget:** call `app.records.match()` (best match / singleton) or `app.records.resolveAll()` (all matches / collection). These are plain SDK calls with no framework dependency.
|
|
28
|
-
|
|
29
|
-
If you only remember one rule: **never write your own resolution loop**. The server already walks the chain correctly — calling `match()` or `resolveAll()` is the entire public-side implementation.
|
|
30
|
-
|
|
31
|
-
---
|
|
32
|
-
|
|
33
|
-
## 1. The data model in one paragraph
|
|
34
|
-
|
|
35
|
-
A microapp owns a typed **records table** keyed by `(appId, recordType, id)`. Each `AppRecord` carries a `data` payload plus **either** a structured `scope` (anchored to a node in the chain) **or** a `facetRule` (matches products dynamically by their facets). Records also carry a `status` (`'active'` | `'draft'` | `'archived'`) and optional `startsAt` / `expiresAt` timestamps. The server resolves which record(s) apply to a given product context. There is no "global"; the top of the chain is **collection** — anything not explicitly scoped further applies to the whole collection.
|
|
36
|
-
|
|
37
|
-
```ts
|
|
38
|
-
import * as SL from '@proveanything/smartlinks';
|
|
39
|
-
|
|
40
|
-
await SL.app.records.upsert(collectionId, appId, {
|
|
41
|
-
recordType: 'ingredients',
|
|
42
|
-
scope: { productId: 'prod_abc', variantId: 'var_500ml' }, // server derives the ref
|
|
43
|
-
data: { /* domain payload */ },
|
|
44
|
-
}, /* admin */ true);
|
|
45
|
-
```
|
|
46
|
-
|
|
47
|
-
Or, for a rule-targeted record:
|
|
48
|
-
|
|
49
|
-
```ts
|
|
50
|
-
await SL.app.records.upsert(collectionId, appId, {
|
|
51
|
-
recordType: 'ingredients',
|
|
52
|
-
facetRule: {
|
|
53
|
-
all: [
|
|
54
|
-
{ facetKey: 'brand', anyOf: ['acme'] },
|
|
55
|
-
{ facetKey: 'category', anyOf: ['bread', 'pastry'] },
|
|
56
|
-
],
|
|
57
|
-
},
|
|
58
|
-
data: { /* domain payload */ },
|
|
59
|
-
}, true);
|
|
60
|
-
```
|
|
61
|
-
|
|
62
|
-
`scope` and `facetRule` are **mutually exclusive on save**.
|
|
63
|
-
|
|
64
|
-
---
|
|
65
|
-
|
|
66
|
-
## 2. Resolution order (one canonical chain)
|
|
67
|
-
|
|
68
|
-
The server walks **most-specific → least-specific** and stops at the first match (for best-match) or collects every match (for aggregate):
|
|
69
|
-
|
|
70
|
-
```
|
|
71
|
-
proof → batch → variant → product → rule(*) → facet(*) → collection
|
|
72
|
-
```
|
|
73
|
-
|
|
74
|
-
- `rule(*)` — facet-rule records are scored by **specificity** (number of clauses + number of constrained values). The most specific rule wins.
|
|
75
|
-
- `facet(*)` — legacy single-facet anchors, walked deterministically (alphabetical).
|
|
76
|
-
- `collection` — the top of the chain. **There is no "global" tier above collection.** A collection-level record is the catch-all for that collection.
|
|
77
|
-
|
|
78
|
-
The resolved value comes back tagged with `matchedAt: 'product' | 'rule' | 'facet' | …` so the UI can say things like _"Matched by rule: brand=Acme AND category=bread"_.
|
|
79
|
-
|
|
80
|
-
> ⚠️ Legacy `scope.facets[]` (colon-delimited single-facet refs) is deprecated and removed in SDK 1.12. Use `facetRule` for everything that isn't a one-off facet pin.
|
|
81
|
-
|
|
82
|
-
---
|
|
83
|
-
|
|
84
|
-
## 3. Manifest declaration
|
|
85
|
-
|
|
86
|
-
Declare each record type once in `app.admin.json`. The shell and the platform read this to render the right scope tabs and disable the wrong affordances.
|
|
87
|
-
|
|
88
|
-
```json
|
|
89
|
-
{
|
|
90
|
-
"records": {
|
|
91
|
-
"ingredients": {
|
|
92
|
-
"label": "Ingredients",
|
|
93
|
-
"cardinality": "singleton",
|
|
94
|
-
"allowFacetRules": true,
|
|
95
|
-
"scopes": ["collection", "facet", "rule", "product", "variant", "batch"],
|
|
96
|
-
"defaultScope": "product"
|
|
97
|
-
},
|
|
98
|
-
"faq": {
|
|
99
|
-
"label": "FAQs",
|
|
100
|
-
"cardinality": "collection",
|
|
101
|
-
"allowFacetRules": true,
|
|
102
|
-
"scopes": ["collection", "rule", "product"],
|
|
103
|
-
"defaultScope": "collection"
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
```
|
|
108
|
-
|
|
109
|
-
| Field | Meaning |
|
|
110
|
-
| ----------------- | ------------------------------------------------------------------------------------------------------------------------------------ |
|
|
111
|
-
| `cardinality` | `'singleton'` (one per scope, e.g. ingredients) or `'collection'` (many per scope, e.g. FAQs). Default `'singleton'`. |
|
|
112
|
-
| `allowFacetRules` | `true` to enable the `rule` scope tab + `<FacetRuleEditor>` in the shell. Default `false`. |
|
|
113
|
-
| `scopes` | Allowed scope kinds in **resolution order**. `'rule'` is a synthetic scope that holds rule-targeted records. |
|
|
114
|
-
| `defaultScope` | Where the "Create new" button lands. |
|
|
115
|
-
| `label` | Human-readable label used in headings and toasts. |
|
|
116
|
-
|
|
117
|
-
---
|
|
118
|
-
|
|
119
|
-
## 4. Admin side
|
|
120
|
-
|
|
121
|
-
> The admin shell and rule editor are part of `@proveanything/smartlinks-utils-ui`, which is a **React-only library**. It is only needed in admin dashboards — never import it in a public widget.
|
|
122
|
-
|
|
123
|
-
### `<RecordsAdminShell>` (React)
|
|
124
|
-
|
|
125
|
-
The shell owns: scope tabs, browser pane, rule editor, save/discard, dirty navigation, inheritance markers, deletion, CSV, bulk apply, deep linking. **You only supply the editor for one record's `data`.**
|
|
126
|
-
|
|
127
|
-
```tsx
|
|
128
|
-
import * as SL from '@proveanything/smartlinks';
|
|
129
|
-
import { RecordsAdminShell } from '@proveanything/smartlinks-utils-ui/records-admin';
|
|
130
|
-
|
|
131
|
-
<RecordsAdminShell<IngredientsConfig>
|
|
132
|
-
SL={SL}
|
|
133
|
-
collectionId={collectionId}
|
|
134
|
-
appId={appId}
|
|
135
|
-
recordType="ingredients"
|
|
136
|
-
label="Ingredients"
|
|
137
|
-
cardinality="singleton" // ← from manifest
|
|
138
|
-
scopes={['collection', 'facet', 'rule', 'product', 'variant', 'batch']}
|
|
139
|
-
defaultScope="product"
|
|
140
|
-
defaultData={() => emptyConfig()}
|
|
141
|
-
renderEditor={(ctx) => (
|
|
142
|
-
<IngredientsEditor
|
|
143
|
-
value={ctx.value}
|
|
144
|
-
onChange={ctx.onChange}
|
|
145
|
-
// For rule-targeted records, the shell hands you the live rule + setter:
|
|
146
|
-
facetRule={ctx.facetRule}
|
|
147
|
-
onFacetRuleChange={ctx.onFacetRuleChange}
|
|
148
|
-
/>
|
|
149
|
-
)}
|
|
150
|
-
/>
|
|
151
|
-
```
|
|
152
|
-
|
|
153
|
-
### What the shell gives you for free
|
|
154
|
-
|
|
155
|
-
- **Scope tabs** including a **`Rule`** tab when `'rule'` is in `scopes`. Selecting it opens `<FacetRuleEditor>` above your editor — no extra wiring.
|
|
156
|
-
- **`EditorContext.facetRule` / `onFacetRuleChange`** for rule-scoped records, plus `canSave: false` until at least one clause has values (avoids server 500s).
|
|
157
|
-
- **Inheritance markers** — when editing a variant, the product baseline is shown; per-field "↩ Inherited" / "● Override" is rendered by the inheritance helpers.
|
|
158
|
-
- **Collection cardinality flow** — set `cardinality="collection"` and the shell turns the right pane into a list of items (table / cards / gallery) with `+ New` and per-item nav.
|
|
159
|
-
- **Telemetry** — `record.save`, `record.delete`, `scope.change`, `csv.import`, `bulk.apply`, `item.create`, etc. via `onTelemetry`.
|
|
160
|
-
|
|
161
|
-
### Standalone rule editor
|
|
162
|
-
|
|
163
|
-
If you need a rule editor outside the shell (e.g. on a settings page):
|
|
164
|
-
|
|
165
|
-
```tsx
|
|
166
|
-
import { FacetRuleEditor } from '@proveanything/smartlinks-utils-ui/facet-rule-editor';
|
|
167
|
-
|
|
168
|
-
<FacetRuleEditor
|
|
169
|
-
value={rule}
|
|
170
|
-
onChange={setRule}
|
|
171
|
-
collectionId={collectionId} // lazy-fetches facets via SL.facets.publicList
|
|
172
|
-
preview={rulePreview} // optional — wire from useRulePreview
|
|
173
|
-
/>
|
|
174
|
-
```
|
|
175
|
-
|
|
176
|
-
---
|
|
177
|
-
|
|
178
|
-
## 5. Public side
|
|
179
|
-
|
|
180
|
-
> **Do not import `@proveanything/smartlinks-utils-ui` in a public widget.** It is a React admin library. Public widgets only need `@proveanything/smartlinks`.
|
|
181
|
-
|
|
182
|
-
The SDK is framework-agnostic. Public widgets call two endpoints depending on cardinality:
|
|
183
|
-
|
|
184
|
-
| Cardinality | Call | What it does |
|
|
185
|
-
|---|---|---|
|
|
186
|
-
| **Singleton** (one answer) | `app.records.match()` | Server walks the chain, returns the best-matching record |
|
|
187
|
-
| **Collection** (all answers) | `app.records.resolveAll()` | Server walks the chain, returns every matching record |
|
|
188
|
-
|
|
189
|
-
Neither call requires React or any other framework — wrap them in whatever async pattern your widget uses.
|
|
190
|
-
|
|
191
|
-
> **Admin vs public — the rule is simple:**
|
|
192
|
-
>
|
|
193
|
-
> | Function | Public widget | Admin dashboard |
|
|
194
|
-
> |---|---|---|
|
|
195
|
-
> | `app.records.create(…, false)` | ✅ default — omit the flag | ✅ pass `true` |
|
|
196
|
-
> | `app.records.list(…, false)` | ✅ default — omit the flag | ✅ pass `true` |
|
|
197
|
-
> | `app.records.get(…, false)` | ✅ default — omit the flag | ✅ pass `true` |
|
|
198
|
-
> | `app.records.update(…, false)` | ✅ default — omit the flag | ✅ pass `true` |
|
|
199
|
-
> | `app.records.remove(…, false)` | ✅ default — omit the flag | ✅ pass `true` |
|
|
200
|
-
> | `app.records.aggregate(…, false)` | ✅ default — omit the flag | ✅ pass `true` |
|
|
201
|
-
> | `app.records.match(…, false)` | ✅ default — omit the flag | ✅ pass `true` |
|
|
202
|
-
> | `app.records.resolveAll(…, false)` | ✅ default — omit the flag | ✅ pass `true` |
|
|
203
|
-
> | `app.records.upsert()` | ❌ admin only — no public path | ✅ |
|
|
204
|
-
> | `app.records.bulkUpsert()` | ❌ admin only — no public path | ✅ |
|
|
205
|
-
> | `app.records.bulkDelete()` | ❌ admin only — no public path | ✅ |
|
|
206
|
-
> | `app.records.restore()` | ❌ admin only — no public path | ✅ |
|
|
207
|
-
> | `app.records.previewRule()` | ❌ admin only — no public path | ✅ |
|
|
208
|
-
|
|
209
|
-
### 5a. Singleton — `app.records.match()` (best match wins)
|
|
210
|
-
|
|
211
|
-
Use when the widget shows **one** answer for the current product (ingredients, nutrition, warranty terms, washing instructions).
|
|
212
|
-
|
|
213
|
-
```ts
|
|
214
|
-
import * as SL from '@proveanything/smartlinks';
|
|
215
|
-
|
|
216
|
-
const result = await SL.app.records.match(collectionId, appId, {
|
|
217
|
-
target: { productId, variantId, batchId }, // pass whatever context you have
|
|
218
|
-
strategy: 'best',
|
|
219
|
-
recordType: 'ingredients',
|
|
220
|
-
});
|
|
221
|
-
|
|
222
|
-
// result.data[0] → the single highest-specificity MatchEntry (when strategy: 'best')
|
|
223
|
-
// result.data[0].matchedAt → 'product' | 'rule' | 'facet' | 'collection' | …
|
|
224
|
-
// result.data[0].data → your record payload
|
|
225
|
-
```
|
|
226
|
-
|
|
227
|
-
The server walks `proof → batch → variant → product → rule → facet → collection` and returns the first match. `result.data` will have at most one entry when `strategy: 'best'`.
|
|
228
|
-
|
|
229
|
-
### 5b. Collection — `app.records.resolveAll()` (every match, ordered)
|
|
230
|
-
|
|
231
|
-
Use when the widget shows **many** answers across the chain (FAQs, recipes, care tips, SOPs).
|
|
232
|
-
|
|
233
|
-
```ts
|
|
234
|
-
const result = await SL.app.records.resolveAll(collectionId, appId, {
|
|
235
|
-
context: { productId }, // note: resolveAll uses 'context', not 'target'
|
|
236
|
-
recordType: 'faq', // singular — omit to return all record types
|
|
237
|
-
});
|
|
238
|
-
|
|
239
|
-
// result.records → ResolveAllEntry[] sorted most-specific first
|
|
240
|
-
// each entry: { record: AppRecord, matchedAt, specificity, matchedRule? }
|
|
241
|
-
```
|
|
242
|
-
|
|
243
|
-
### 5c. Multi-type — `app.records.resolveAll()` without a recordType filter
|
|
244
|
-
|
|
245
|
-
When you need records of all types for a context in one call (rare; executors, SEO surfaces), omit `recordType`:
|
|
246
|
-
|
|
247
|
-
```ts
|
|
248
|
-
const result = await SL.app.records.resolveAll(collectionId, appId, {
|
|
249
|
-
context: {
|
|
250
|
-
productId,
|
|
251
|
-
facets: { brand: ['acme'] }, // include facets to match rule records
|
|
252
|
-
},
|
|
253
|
-
// no recordType → returns all declared types
|
|
254
|
-
});
|
|
255
|
-
|
|
256
|
-
// result.records → one ResolveAllEntry per matched record, all types interleaved
|
|
257
|
-
// filter client-side by entry.record.recordType if you need to separate them
|
|
258
|
-
```
|
|
259
|
-
|
|
260
|
-
### 5d. Status filtering and the draft → active lifecycle
|
|
261
|
-
|
|
262
|
-
Every record has a `status` field with three canonical values:
|
|
263
|
-
|
|
264
|
-
| Value | Meaning | Returned to public/owner callers? |
|
|
265
|
-
|---|---|---|
|
|
266
|
-
| `active` | Live and current | ✅ Yes |
|
|
267
|
-
| `draft` | Being prepared, not yet published | ❌ No |
|
|
268
|
-
| `archived` | Previously live, retained for history | ❌ No |
|
|
269
|
-
|
|
270
|
-
**Enforcement:** `match()`, `resolveAll()`, and `GET /records` (query) now only return `status: "active"` records to public and owner callers. Admin callers receive all statuses as before; use explicit `status` filters (`status=draft`, `status=archived`) to narrow results.
|
|
271
|
-
|
|
272
|
-
**`active` is the default** when no `status` is supplied on creation, so existing records and simple creation flows are unaffected.
|
|
273
|
-
|
|
274
|
-
**Draft → publish workflow:** create the record with `status: 'draft'` so it is invisible to public widgets, then update it to `status: 'active'` when ready to publish.
|
|
275
|
-
|
|
276
|
-
```ts
|
|
277
|
-
// Create a record that is not yet publicly visible
|
|
278
|
-
await SL.app.records.upsert(collectionId, appId, {
|
|
279
|
-
recordType: 'ingredients',
|
|
280
|
-
scope: { productId },
|
|
281
|
-
data: { /* draft payload */ },
|
|
282
|
-
status: 'draft',
|
|
283
|
-
}, /* admin */ true);
|
|
284
|
-
|
|
285
|
-
// Publish it
|
|
286
|
-
await SL.app.records.upsert(collectionId, appId, {
|
|
287
|
-
recordType: 'ingredients',
|
|
288
|
-
scope: { productId },
|
|
289
|
-
data: { /* final payload */ },
|
|
290
|
-
status: 'active',
|
|
291
|
-
}, true);
|
|
292
|
-
```
|
|
293
|
-
|
|
294
|
-
**Composes with `startsAt` / `expiresAt`:** a record must satisfy **both** the status check and the time window to be returned to public callers. A record that is `active` but whose `startsAt` is in the future, or whose `expiresAt` has passed, is excluded.
|
|
295
|
-
|
|
296
|
-
---
|
|
297
|
-
|
|
298
|
-
### Common mistakes (do not do these)
|
|
299
|
-
|
|
300
|
-
| ❌ Anti-pattern | ✅ Do this instead |
|
|
301
|
-
| ---------------------------------------------------------- | ----------------------------------------------------------------- |
|
|
302
|
-
| Importing anything from `@proveanything/smartlinks-utils-ui` in a public widget | That package is React-only and admin-only. Public widgets only use `@proveanything/smartlinks`. |
|
|
303
|
-
| Calling `SL.app.records.list()` and filtering client-side | `app.records.match()` (singleton) or `app.records.resolveAll()` (collection). The server walks the chain. |
|
|
304
|
-
| Calling `SL.app.records.list(…, true)` from a public widget | Omit the `admin` flag — it defaults to `false`. |
|
|
305
|
-
| Calling `SL.app.records.match(…, true)` from a public widget | Omit the `admin` flag — it defaults to `false`. |
|
|
306
|
-
| Calling `SL.app.records.resolveAll(…, true)` from a public widget | Omit the `admin` flag — it defaults to `false`. |
|
|
307
|
-
| Calling `upsert`, `bulkUpsert`, `bulkDelete`, or `previewRule` from widget code | Those are admin-only. Widget code reads data; it never writes records. |
|
|
308
|
-
| Walking the chain by hand with multiple `get` / `list` calls | One `match()` or `resolveAll()` call. The server handles the resolution order including rules. |
|
|
309
|
-
| Treating `facet:key:value` refs as the rule mechanism | Use `facetRule` (`{ all: [{ facetKey, anyOf: [...] }] }`). Multi-condition, scored by specificity. |
|
|
310
|
-
| Reading `matchedAt === 'global'` | There is no `'global'`. The top of the chain is `'collection'`. |
|
|
311
|
-
| Expecting `draft` or `archived` records to appear in public widget results | Public/owner callers only receive `status: "active"` records from `match()`, `resolveAll()`, and `GET /records`. Use admin calls to query by other statuses. |
|
|
312
|
-
| Setting `status: 'active'` and wondering why a record is still hidden | Check `startsAt` / `expiresAt` — a record must satisfy both the status check and the time window. |
|
|
313
|
-
|
|
314
|
-
---
|
|
315
|
-
|
|
316
|
-
## 6. Reference: the `EditorContext` your `renderEditor` receives
|
|
317
|
-
|
|
318
|
-
```ts
|
|
319
|
-
interface EditorContext<TData> {
|
|
320
|
-
value: TData;
|
|
321
|
-
onChange: (next: TData) => void;
|
|
322
|
-
source: 'self' | 'inherited' | 'empty';
|
|
323
|
-
recordId?: string;
|
|
324
|
-
parentValue?: TData | null;
|
|
325
|
-
scope: ParsedRef; // { kind: 'product' | 'rule' | …, productId?, … }
|
|
326
|
-
|
|
327
|
-
// Save lifecycle
|
|
328
|
-
isDirty: boolean;
|
|
329
|
-
isSaving?: boolean;
|
|
330
|
-
saveError?: unknown | null;
|
|
331
|
-
canSave?: boolean; // shell flips to false on empty rules
|
|
332
|
-
cannotSaveReason?: string;
|
|
333
|
-
save: () => Promise<void>;
|
|
334
|
-
reset: () => void;
|
|
335
|
-
|
|
336
|
-
// Deletion
|
|
337
|
-
remove: () => Promise<void>;
|
|
338
|
-
canRemove: boolean;
|
|
339
|
-
|
|
340
|
-
// Rule scope only
|
|
341
|
-
facetRule?: FacetRule | null;
|
|
342
|
-
onFacetRuleChange?: (next: FacetRule | null) => void;
|
|
343
|
-
}
|
|
344
|
-
```
|
|
345
|
-
|
|
346
|
-
---
|
|
347
|
-
|
|
348
|
-
## 7. Migration checklist (existing apps)
|
|
349
|
-
|
|
350
|
-
1. **Update SDKs:** `@proveanything/smartlinks@^2.0`, `@proveanything/smartlinks-utils-ui@^2.0`.
|
|
351
|
-
2. **Add `cardinality` and `allowFacetRules`** to every entry under `records` in `app.admin.json`.
|
|
352
|
-
3. **Add `'rule'` (and `'collection'` if missing) to `scopes`** wherever `allowFacetRules: true`.
|
|
353
|
-
4. **Pass `cardinality`** to `<RecordsAdminShell>`.
|
|
354
|
-
5. **Replace any handwritten chain walking** with `app.records.match()` (singleton) or `app.records.resolveAll()` (collection). If you are using React, the `useResolvedRecord` / `useCollectedRecords` hooks from `@proveanything/smartlinks-utils-ui` wrap these calls — but they are **admin-side React helpers**, not for public widgets.
|
|
355
|
-
6. **Delete any code that constructs `facet:key:value` refs** for matching. Use `facetRule` via the shell or `<FacetRuleEditor>` (React admin) or pass `facetRule` directly in `upsert()` calls.
|
|
356
|
-
7. **Search for the word "global"** in your code/docs and rename to "collection" — this is the most common source of confusion.
|
|
357
|
-
8. **Audit records that should not be public yet:** any record that previously relied on obscurity (e.g. not linked in the widget, no active product) is now filtered by `status`. Set `status: 'draft'` on records that are not ready and `status: 'active'` when publishing. Records without an explicit status were created as `active` and are unaffected.
|
|
358
|
-
|
|
359
|
-
---
|
|
360
|
-
|
|
361
|
-
## 8. Where the canonical exports live
|
|
362
|
-
|
|
363
|
-
### Public widgets (any framework)
|
|
364
|
-
|
|
365
|
-
| Need | Import from |
|
|
366
|
-
| ---- | ----------- |
|
|
367
|
-
| Best-match resolution (singleton) | `@proveanything/smartlinks` → `SL.app.records.match()` |
|
|
368
|
-
| All-matches resolution (collection) | `@proveanything/smartlinks` → `SL.app.records.resolveAll()` |
|
|
369
|
-
| Record CRUD (public path) | `@proveanything/smartlinks` → `SL.app.records.{list, get, create, update, remove, aggregate}` |
|
|
370
|
-
|
|
371
|
-
### Admin dashboards (React)
|
|
372
|
-
|
|
373
|
-
> All of the following are from `@proveanything/smartlinks-utils-ui`, a **React-only** package. Do not use in public widgets.
|
|
374
|
-
|
|
375
|
-
| Need | Import from |
|
|
376
|
-
| ---- | ----------- |
|
|
377
|
-
| Admin shell | `@proveanything/smartlinks-utils-ui/records-admin` → `RecordsAdminShell` |
|
|
378
|
-
| Standalone rule editor | `@proveanything/smartlinks-utils-ui/facet-rule-editor` → `FacetRuleEditor` |
|
|
379
|
-
| Conditions editor (non-facet) | `@proveanything/smartlinks-utils-ui/conditions-editor` → `ConditionsEditor` |
|
|
380
|
-
| Best-match hook (React convenience wrapper) | `@proveanything/smartlinks-utils-ui/records-admin` → `useResolvedRecord` |
|
|
381
|
-
| All-matches hook (React convenience wrapper) | `@proveanything/smartlinks-utils-ui/records-admin` → `useCollectedRecords` |
|
|
382
|
-
| Multi-type hook (React convenience wrapper) | `@proveanything/smartlinks-utils-ui/records-admin` → `useResolveAllRecords` |
|
|
383
|
-
| Rule preview hook | `@proveanything/smartlinks-utils-ui/records-admin` → `useRulePreview` |
|
|
384
|
-
| Admin record CRUD (admin path) | `@proveanything/smartlinks` → `SL.app.records.{upsert, bulkUpsert, bulkDelete, restore, previewRule}` |
|
|
385
|
-
|
|
386
|
-
---
|
|
387
|
-
|
|
388
|
-
_End of doc. If anything below the SDK contradicts this file, this file wins — open a PR against the SDK to bring the two back into sync._
|
|
1
|
+
# SmartLinks App Records Pattern
|
|
2
|
+
|
|
3
|
+
> Canonical guide for microapps that store **per-product**, **per-facet**, **per-variant**, **per-batch**, or **rule-targeted** data.
|
|
4
|
+
>
|
|
5
|
+
> Audience: microapp developers (ingredients, nutrition, allergy, FAQs, recipes, warranty, provenance, …).
|
|
6
|
+
>
|
|
7
|
+
> Status: **standard**. New apps MUST follow this contract; existing apps SHOULD migrate.
|
|
8
|
+
>
|
|
9
|
+
> SDK: `@proveanything/smartlinks` ≥ **2.0** (R5).
|
|
10
|
+
> Admin shell (React only): `@proveanything/smartlinks-utils-ui` ≥ **2.0** — required for the admin side if using the React shell; not needed in public widgets.
|
|
11
|
+
|
|
12
|
+
---
|
|
13
|
+
|
|
14
|
+
## 0. TL;DR — pick your shape, then copy the snippet
|
|
15
|
+
|
|
16
|
+
Every records-based app fits into a 2×2:
|
|
17
|
+
|
|
18
|
+
| | **Singleton** (one record per scope) | **Collection** (many records per scope) |
|
|
19
|
+
| ------------------------ | ---------------------------------------------------- | ------------------------------------------------------------- |
|
|
20
|
+
| **Best-match (one wins)**| Ingredients, nutrition, washing instructions | _(rare — usually you want all)_ |
|
|
21
|
+
| **All matches (aggregate)** | _(rare — usually you want best)_ | FAQs, recipes, SOPs, care tips, story cards |
|
|
22
|
+
|
|
23
|
+
That choice drives **three** things and nothing else:
|
|
24
|
+
|
|
25
|
+
1. **Manifest:** `cardinality: 'singleton' | 'collection'` and `allowFacetRules: boolean`.
|
|
26
|
+
2. **Admin:** use `<RecordsAdminShell>` (React) or call the admin SDK functions directly. Pass `cardinality` + include `'rule'` in `scopes` if `allowFacetRules`.
|
|
27
|
+
3. **Public widget:** call `app.records.match()` (best match / singleton) or `app.records.resolveAll()` (all matches / collection). These are plain SDK calls with no framework dependency.
|
|
28
|
+
|
|
29
|
+
If you only remember one rule: **never write your own resolution loop**. The server already walks the chain correctly — calling `match()` or `resolveAll()` is the entire public-side implementation.
|
|
30
|
+
|
|
31
|
+
---
|
|
32
|
+
|
|
33
|
+
## 1. The data model in one paragraph
|
|
34
|
+
|
|
35
|
+
A microapp owns a typed **records table** keyed by `(appId, recordType, id)`. Each `AppRecord` carries a `data` payload plus **either** a structured `scope` (anchored to a node in the chain) **or** a `facetRule` (matches products dynamically by their facets). Records also carry a `status` (`'active'` | `'draft'` | `'archived'`) and optional `startsAt` / `expiresAt` timestamps. The server resolves which record(s) apply to a given product context. There is no "global"; the top of the chain is **collection** — anything not explicitly scoped further applies to the whole collection.
|
|
36
|
+
|
|
37
|
+
```ts
|
|
38
|
+
import * as SL from '@proveanything/smartlinks';
|
|
39
|
+
|
|
40
|
+
await SL.app.records.upsert(collectionId, appId, {
|
|
41
|
+
recordType: 'ingredients',
|
|
42
|
+
scope: { productId: 'prod_abc', variantId: 'var_500ml' }, // server derives the ref
|
|
43
|
+
data: { /* domain payload */ },
|
|
44
|
+
}, /* admin */ true);
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
Or, for a rule-targeted record:
|
|
48
|
+
|
|
49
|
+
```ts
|
|
50
|
+
await SL.app.records.upsert(collectionId, appId, {
|
|
51
|
+
recordType: 'ingredients',
|
|
52
|
+
facetRule: {
|
|
53
|
+
all: [
|
|
54
|
+
{ facetKey: 'brand', anyOf: ['acme'] },
|
|
55
|
+
{ facetKey: 'category', anyOf: ['bread', 'pastry'] },
|
|
56
|
+
],
|
|
57
|
+
},
|
|
58
|
+
data: { /* domain payload */ },
|
|
59
|
+
}, true);
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
`scope` and `facetRule` are **mutually exclusive on save**.
|
|
63
|
+
|
|
64
|
+
---
|
|
65
|
+
|
|
66
|
+
## 2. Resolution order (one canonical chain)
|
|
67
|
+
|
|
68
|
+
The server walks **most-specific → least-specific** and stops at the first match (for best-match) or collects every match (for aggregate):
|
|
69
|
+
|
|
70
|
+
```
|
|
71
|
+
proof → batch → variant → product → rule(*) → facet(*) → collection
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
- `rule(*)` — facet-rule records are scored by **specificity** (number of clauses + number of constrained values). The most specific rule wins.
|
|
75
|
+
- `facet(*)` — legacy single-facet anchors, walked deterministically (alphabetical).
|
|
76
|
+
- `collection` — the top of the chain. **There is no "global" tier above collection.** A collection-level record is the catch-all for that collection.
|
|
77
|
+
|
|
78
|
+
The resolved value comes back tagged with `matchedAt: 'product' | 'rule' | 'facet' | …` so the UI can say things like _"Matched by rule: brand=Acme AND category=bread"_.
|
|
79
|
+
|
|
80
|
+
> ⚠️ Legacy `scope.facets[]` (colon-delimited single-facet refs) is deprecated and removed in SDK 1.12. Use `facetRule` for everything that isn't a one-off facet pin.
|
|
81
|
+
|
|
82
|
+
---
|
|
83
|
+
|
|
84
|
+
## 3. Manifest declaration
|
|
85
|
+
|
|
86
|
+
Declare each record type once in `app.admin.json`. The shell and the platform read this to render the right scope tabs and disable the wrong affordances.
|
|
87
|
+
|
|
88
|
+
```json
|
|
89
|
+
{
|
|
90
|
+
"records": {
|
|
91
|
+
"ingredients": {
|
|
92
|
+
"label": "Ingredients",
|
|
93
|
+
"cardinality": "singleton",
|
|
94
|
+
"allowFacetRules": true,
|
|
95
|
+
"scopes": ["collection", "facet", "rule", "product", "variant", "batch"],
|
|
96
|
+
"defaultScope": "product"
|
|
97
|
+
},
|
|
98
|
+
"faq": {
|
|
99
|
+
"label": "FAQs",
|
|
100
|
+
"cardinality": "collection",
|
|
101
|
+
"allowFacetRules": true,
|
|
102
|
+
"scopes": ["collection", "rule", "product"],
|
|
103
|
+
"defaultScope": "collection"
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
| Field | Meaning |
|
|
110
|
+
| ----------------- | ------------------------------------------------------------------------------------------------------------------------------------ |
|
|
111
|
+
| `cardinality` | `'singleton'` (one per scope, e.g. ingredients) or `'collection'` (many per scope, e.g. FAQs). Default `'singleton'`. |
|
|
112
|
+
| `allowFacetRules` | `true` to enable the `rule` scope tab + `<FacetRuleEditor>` in the shell. Default `false`. |
|
|
113
|
+
| `scopes` | Allowed scope kinds in **resolution order**. `'rule'` is a synthetic scope that holds rule-targeted records. |
|
|
114
|
+
| `defaultScope` | Where the "Create new" button lands. |
|
|
115
|
+
| `label` | Human-readable label used in headings and toasts. |
|
|
116
|
+
|
|
117
|
+
---
|
|
118
|
+
|
|
119
|
+
## 4. Admin side
|
|
120
|
+
|
|
121
|
+
> The admin shell and rule editor are part of `@proveanything/smartlinks-utils-ui`, which is a **React-only library**. It is only needed in admin dashboards — never import it in a public widget.
|
|
122
|
+
|
|
123
|
+
### `<RecordsAdminShell>` (React)
|
|
124
|
+
|
|
125
|
+
The shell owns: scope tabs, browser pane, rule editor, save/discard, dirty navigation, inheritance markers, deletion, CSV, bulk apply, deep linking. **You only supply the editor for one record's `data`.**
|
|
126
|
+
|
|
127
|
+
```tsx
|
|
128
|
+
import * as SL from '@proveanything/smartlinks';
|
|
129
|
+
import { RecordsAdminShell } from '@proveanything/smartlinks-utils-ui/records-admin';
|
|
130
|
+
|
|
131
|
+
<RecordsAdminShell<IngredientsConfig>
|
|
132
|
+
SL={SL}
|
|
133
|
+
collectionId={collectionId}
|
|
134
|
+
appId={appId}
|
|
135
|
+
recordType="ingredients"
|
|
136
|
+
label="Ingredients"
|
|
137
|
+
cardinality="singleton" // ← from manifest
|
|
138
|
+
scopes={['collection', 'facet', 'rule', 'product', 'variant', 'batch']}
|
|
139
|
+
defaultScope="product"
|
|
140
|
+
defaultData={() => emptyConfig()}
|
|
141
|
+
renderEditor={(ctx) => (
|
|
142
|
+
<IngredientsEditor
|
|
143
|
+
value={ctx.value}
|
|
144
|
+
onChange={ctx.onChange}
|
|
145
|
+
// For rule-targeted records, the shell hands you the live rule + setter:
|
|
146
|
+
facetRule={ctx.facetRule}
|
|
147
|
+
onFacetRuleChange={ctx.onFacetRuleChange}
|
|
148
|
+
/>
|
|
149
|
+
)}
|
|
150
|
+
/>
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
### What the shell gives you for free
|
|
154
|
+
|
|
155
|
+
- **Scope tabs** including a **`Rule`** tab when `'rule'` is in `scopes`. Selecting it opens `<FacetRuleEditor>` above your editor — no extra wiring.
|
|
156
|
+
- **`EditorContext.facetRule` / `onFacetRuleChange`** for rule-scoped records, plus `canSave: false` until at least one clause has values (avoids server 500s).
|
|
157
|
+
- **Inheritance markers** — when editing a variant, the product baseline is shown; per-field "↩ Inherited" / "● Override" is rendered by the inheritance helpers.
|
|
158
|
+
- **Collection cardinality flow** — set `cardinality="collection"` and the shell turns the right pane into a list of items (table / cards / gallery) with `+ New` and per-item nav.
|
|
159
|
+
- **Telemetry** — `record.save`, `record.delete`, `scope.change`, `csv.import`, `bulk.apply`, `item.create`, etc. via `onTelemetry`.
|
|
160
|
+
|
|
161
|
+
### Standalone rule editor
|
|
162
|
+
|
|
163
|
+
If you need a rule editor outside the shell (e.g. on a settings page):
|
|
164
|
+
|
|
165
|
+
```tsx
|
|
166
|
+
import { FacetRuleEditor } from '@proveanything/smartlinks-utils-ui/facet-rule-editor';
|
|
167
|
+
|
|
168
|
+
<FacetRuleEditor
|
|
169
|
+
value={rule}
|
|
170
|
+
onChange={setRule}
|
|
171
|
+
collectionId={collectionId} // lazy-fetches facets via SL.facets.publicList
|
|
172
|
+
preview={rulePreview} // optional — wire from useRulePreview
|
|
173
|
+
/>
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
---
|
|
177
|
+
|
|
178
|
+
## 5. Public side
|
|
179
|
+
|
|
180
|
+
> **Do not import `@proveanything/smartlinks-utils-ui` in a public widget.** It is a React admin library. Public widgets only need `@proveanything/smartlinks`.
|
|
181
|
+
|
|
182
|
+
The SDK is framework-agnostic. Public widgets call two endpoints depending on cardinality:
|
|
183
|
+
|
|
184
|
+
| Cardinality | Call | What it does |
|
|
185
|
+
|---|---|---|
|
|
186
|
+
| **Singleton** (one answer) | `app.records.match()` | Server walks the chain, returns the best-matching record |
|
|
187
|
+
| **Collection** (all answers) | `app.records.resolveAll()` | Server walks the chain, returns every matching record |
|
|
188
|
+
|
|
189
|
+
Neither call requires React or any other framework — wrap them in whatever async pattern your widget uses.
|
|
190
|
+
|
|
191
|
+
> **Admin vs public — the rule is simple:**
|
|
192
|
+
>
|
|
193
|
+
> | Function | Public widget | Admin dashboard |
|
|
194
|
+
> |---|---|---|
|
|
195
|
+
> | `app.records.create(…, false)` | ✅ default — omit the flag | ✅ pass `true` |
|
|
196
|
+
> | `app.records.list(…, false)` | ✅ default — omit the flag | ✅ pass `true` |
|
|
197
|
+
> | `app.records.get(…, false)` | ✅ default — omit the flag | ✅ pass `true` |
|
|
198
|
+
> | `app.records.update(…, false)` | ✅ default — omit the flag | ✅ pass `true` |
|
|
199
|
+
> | `app.records.remove(…, false)` | ✅ default — omit the flag | ✅ pass `true` |
|
|
200
|
+
> | `app.records.aggregate(…, false)` | ✅ default — omit the flag | ✅ pass `true` |
|
|
201
|
+
> | `app.records.match(…, false)` | ✅ default — omit the flag | ✅ pass `true` |
|
|
202
|
+
> | `app.records.resolveAll(…, false)` | ✅ default — omit the flag | ✅ pass `true` |
|
|
203
|
+
> | `app.records.upsert()` | ❌ admin only — no public path | ✅ |
|
|
204
|
+
> | `app.records.bulkUpsert()` | ❌ admin only — no public path | ✅ |
|
|
205
|
+
> | `app.records.bulkDelete()` | ❌ admin only — no public path | ✅ |
|
|
206
|
+
> | `app.records.restore()` | ❌ admin only — no public path | ✅ |
|
|
207
|
+
> | `app.records.previewRule()` | ❌ admin only — no public path | ✅ |
|
|
208
|
+
|
|
209
|
+
### 5a. Singleton — `app.records.match()` (best match wins)
|
|
210
|
+
|
|
211
|
+
Use when the widget shows **one** answer for the current product (ingredients, nutrition, warranty terms, washing instructions).
|
|
212
|
+
|
|
213
|
+
```ts
|
|
214
|
+
import * as SL from '@proveanything/smartlinks';
|
|
215
|
+
|
|
216
|
+
const result = await SL.app.records.match(collectionId, appId, {
|
|
217
|
+
target: { productId, variantId, batchId }, // pass whatever context you have
|
|
218
|
+
strategy: 'best',
|
|
219
|
+
recordType: 'ingredients',
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
// result.data[0] → the single highest-specificity MatchEntry (when strategy: 'best')
|
|
223
|
+
// result.data[0].matchedAt → 'product' | 'rule' | 'facet' | 'collection' | …
|
|
224
|
+
// result.data[0].data → your record payload
|
|
225
|
+
```
|
|
226
|
+
|
|
227
|
+
The server walks `proof → batch → variant → product → rule → facet → collection` and returns the first match. `result.data` will have at most one entry when `strategy: 'best'`.
|
|
228
|
+
|
|
229
|
+
### 5b. Collection — `app.records.resolveAll()` (every match, ordered)
|
|
230
|
+
|
|
231
|
+
Use when the widget shows **many** answers across the chain (FAQs, recipes, care tips, SOPs).
|
|
232
|
+
|
|
233
|
+
```ts
|
|
234
|
+
const result = await SL.app.records.resolveAll(collectionId, appId, {
|
|
235
|
+
context: { productId }, // note: resolveAll uses 'context', not 'target'
|
|
236
|
+
recordType: 'faq', // singular — omit to return all record types
|
|
237
|
+
});
|
|
238
|
+
|
|
239
|
+
// result.records → ResolveAllEntry[] sorted most-specific first
|
|
240
|
+
// each entry: { record: AppRecord, matchedAt, specificity, matchedRule? }
|
|
241
|
+
```
|
|
242
|
+
|
|
243
|
+
### 5c. Multi-type — `app.records.resolveAll()` without a recordType filter
|
|
244
|
+
|
|
245
|
+
When you need records of all types for a context in one call (rare; executors, SEO surfaces), omit `recordType`:
|
|
246
|
+
|
|
247
|
+
```ts
|
|
248
|
+
const result = await SL.app.records.resolveAll(collectionId, appId, {
|
|
249
|
+
context: {
|
|
250
|
+
productId,
|
|
251
|
+
facets: { brand: ['acme'] }, // include facets to match rule records
|
|
252
|
+
},
|
|
253
|
+
// no recordType → returns all declared types
|
|
254
|
+
});
|
|
255
|
+
|
|
256
|
+
// result.records → one ResolveAllEntry per matched record, all types interleaved
|
|
257
|
+
// filter client-side by entry.record.recordType if you need to separate them
|
|
258
|
+
```
|
|
259
|
+
|
|
260
|
+
### 5d. Status filtering and the draft → active lifecycle
|
|
261
|
+
|
|
262
|
+
Every record has a `status` field with three canonical values:
|
|
263
|
+
|
|
264
|
+
| Value | Meaning | Returned to public/owner callers? |
|
|
265
|
+
|---|---|---|
|
|
266
|
+
| `active` | Live and current | ✅ Yes |
|
|
267
|
+
| `draft` | Being prepared, not yet published | ❌ No |
|
|
268
|
+
| `archived` | Previously live, retained for history | ❌ No |
|
|
269
|
+
|
|
270
|
+
**Enforcement:** `match()`, `resolveAll()`, and `GET /records` (query) now only return `status: "active"` records to public and owner callers. Admin callers receive all statuses as before; use explicit `status` filters (`status=draft`, `status=archived`) to narrow results.
|
|
271
|
+
|
|
272
|
+
**`active` is the default** when no `status` is supplied on creation, so existing records and simple creation flows are unaffected.
|
|
273
|
+
|
|
274
|
+
**Draft → publish workflow:** create the record with `status: 'draft'` so it is invisible to public widgets, then update it to `status: 'active'` when ready to publish.
|
|
275
|
+
|
|
276
|
+
```ts
|
|
277
|
+
// Create a record that is not yet publicly visible
|
|
278
|
+
await SL.app.records.upsert(collectionId, appId, {
|
|
279
|
+
recordType: 'ingredients',
|
|
280
|
+
scope: { productId },
|
|
281
|
+
data: { /* draft payload */ },
|
|
282
|
+
status: 'draft',
|
|
283
|
+
}, /* admin */ true);
|
|
284
|
+
|
|
285
|
+
// Publish it
|
|
286
|
+
await SL.app.records.upsert(collectionId, appId, {
|
|
287
|
+
recordType: 'ingredients',
|
|
288
|
+
scope: { productId },
|
|
289
|
+
data: { /* final payload */ },
|
|
290
|
+
status: 'active',
|
|
291
|
+
}, true);
|
|
292
|
+
```
|
|
293
|
+
|
|
294
|
+
**Composes with `startsAt` / `expiresAt`:** a record must satisfy **both** the status check and the time window to be returned to public callers. A record that is `active` but whose `startsAt` is in the future, or whose `expiresAt` has passed, is excluded.
|
|
295
|
+
|
|
296
|
+
---
|
|
297
|
+
|
|
298
|
+
### Common mistakes (do not do these)
|
|
299
|
+
|
|
300
|
+
| ❌ Anti-pattern | ✅ Do this instead |
|
|
301
|
+
| ---------------------------------------------------------- | ----------------------------------------------------------------- |
|
|
302
|
+
| Importing anything from `@proveanything/smartlinks-utils-ui` in a public widget | That package is React-only and admin-only. Public widgets only use `@proveanything/smartlinks`. |
|
|
303
|
+
| Calling `SL.app.records.list()` and filtering client-side | `app.records.match()` (singleton) or `app.records.resolveAll()` (collection). The server walks the chain. |
|
|
304
|
+
| Calling `SL.app.records.list(…, true)` from a public widget | Omit the `admin` flag — it defaults to `false`. |
|
|
305
|
+
| Calling `SL.app.records.match(…, true)` from a public widget | Omit the `admin` flag — it defaults to `false`. |
|
|
306
|
+
| Calling `SL.app.records.resolveAll(…, true)` from a public widget | Omit the `admin` flag — it defaults to `false`. |
|
|
307
|
+
| Calling `upsert`, `bulkUpsert`, `bulkDelete`, or `previewRule` from widget code | Those are admin-only. Widget code reads data; it never writes records. |
|
|
308
|
+
| Walking the chain by hand with multiple `get` / `list` calls | One `match()` or `resolveAll()` call. The server handles the resolution order including rules. |
|
|
309
|
+
| Treating `facet:key:value` refs as the rule mechanism | Use `facetRule` (`{ all: [{ facetKey, anyOf: [...] }] }`). Multi-condition, scored by specificity. |
|
|
310
|
+
| Reading `matchedAt === 'global'` | There is no `'global'`. The top of the chain is `'collection'`. |
|
|
311
|
+
| Expecting `draft` or `archived` records to appear in public widget results | Public/owner callers only receive `status: "active"` records from `match()`, `resolveAll()`, and `GET /records`. Use admin calls to query by other statuses. |
|
|
312
|
+
| Setting `status: 'active'` and wondering why a record is still hidden | Check `startsAt` / `expiresAt` — a record must satisfy both the status check and the time window. |
|
|
313
|
+
|
|
314
|
+
---
|
|
315
|
+
|
|
316
|
+
## 6. Reference: the `EditorContext` your `renderEditor` receives
|
|
317
|
+
|
|
318
|
+
```ts
|
|
319
|
+
interface EditorContext<TData> {
|
|
320
|
+
value: TData;
|
|
321
|
+
onChange: (next: TData) => void;
|
|
322
|
+
source: 'self' | 'inherited' | 'empty';
|
|
323
|
+
recordId?: string;
|
|
324
|
+
parentValue?: TData | null;
|
|
325
|
+
scope: ParsedRef; // { kind: 'product' | 'rule' | …, productId?, … }
|
|
326
|
+
|
|
327
|
+
// Save lifecycle
|
|
328
|
+
isDirty: boolean;
|
|
329
|
+
isSaving?: boolean;
|
|
330
|
+
saveError?: unknown | null;
|
|
331
|
+
canSave?: boolean; // shell flips to false on empty rules
|
|
332
|
+
cannotSaveReason?: string;
|
|
333
|
+
save: () => Promise<void>;
|
|
334
|
+
reset: () => void;
|
|
335
|
+
|
|
336
|
+
// Deletion
|
|
337
|
+
remove: () => Promise<void>;
|
|
338
|
+
canRemove: boolean;
|
|
339
|
+
|
|
340
|
+
// Rule scope only
|
|
341
|
+
facetRule?: FacetRule | null;
|
|
342
|
+
onFacetRuleChange?: (next: FacetRule | null) => void;
|
|
343
|
+
}
|
|
344
|
+
```
|
|
345
|
+
|
|
346
|
+
---
|
|
347
|
+
|
|
348
|
+
## 7. Migration checklist (existing apps)
|
|
349
|
+
|
|
350
|
+
1. **Update SDKs:** `@proveanything/smartlinks@^2.0`, `@proveanything/smartlinks-utils-ui@^2.0`.
|
|
351
|
+
2. **Add `cardinality` and `allowFacetRules`** to every entry under `records` in `app.admin.json`.
|
|
352
|
+
3. **Add `'rule'` (and `'collection'` if missing) to `scopes`** wherever `allowFacetRules: true`.
|
|
353
|
+
4. **Pass `cardinality`** to `<RecordsAdminShell>`.
|
|
354
|
+
5. **Replace any handwritten chain walking** with `app.records.match()` (singleton) or `app.records.resolveAll()` (collection). If you are using React, the `useResolvedRecord` / `useCollectedRecords` hooks from `@proveanything/smartlinks-utils-ui` wrap these calls — but they are **admin-side React helpers**, not for public widgets.
|
|
355
|
+
6. **Delete any code that constructs `facet:key:value` refs** for matching. Use `facetRule` via the shell or `<FacetRuleEditor>` (React admin) or pass `facetRule` directly in `upsert()` calls.
|
|
356
|
+
7. **Search for the word "global"** in your code/docs and rename to "collection" — this is the most common source of confusion.
|
|
357
|
+
8. **Audit records that should not be public yet:** any record that previously relied on obscurity (e.g. not linked in the widget, no active product) is now filtered by `status`. Set `status: 'draft'` on records that are not ready and `status: 'active'` when publishing. Records without an explicit status were created as `active` and are unaffected.
|
|
358
|
+
|
|
359
|
+
---
|
|
360
|
+
|
|
361
|
+
## 8. Where the canonical exports live
|
|
362
|
+
|
|
363
|
+
### Public widgets (any framework)
|
|
364
|
+
|
|
365
|
+
| Need | Import from |
|
|
366
|
+
| ---- | ----------- |
|
|
367
|
+
| Best-match resolution (singleton) | `@proveanything/smartlinks` → `SL.app.records.match()` |
|
|
368
|
+
| All-matches resolution (collection) | `@proveanything/smartlinks` → `SL.app.records.resolveAll()` |
|
|
369
|
+
| Record CRUD (public path) | `@proveanything/smartlinks` → `SL.app.records.{list, get, create, update, remove, aggregate}` |
|
|
370
|
+
|
|
371
|
+
### Admin dashboards (React)
|
|
372
|
+
|
|
373
|
+
> All of the following are from `@proveanything/smartlinks-utils-ui`, a **React-only** package. Do not use in public widgets.
|
|
374
|
+
|
|
375
|
+
| Need | Import from |
|
|
376
|
+
| ---- | ----------- |
|
|
377
|
+
| Admin shell | `@proveanything/smartlinks-utils-ui/records-admin` → `RecordsAdminShell` |
|
|
378
|
+
| Standalone rule editor | `@proveanything/smartlinks-utils-ui/facet-rule-editor` → `FacetRuleEditor` |
|
|
379
|
+
| Conditions editor (non-facet) | `@proveanything/smartlinks-utils-ui/conditions-editor` → `ConditionsEditor` |
|
|
380
|
+
| Best-match hook (React convenience wrapper) | `@proveanything/smartlinks-utils-ui/records-admin` → `useResolvedRecord` |
|
|
381
|
+
| All-matches hook (React convenience wrapper) | `@proveanything/smartlinks-utils-ui/records-admin` → `useCollectedRecords` |
|
|
382
|
+
| Multi-type hook (React convenience wrapper) | `@proveanything/smartlinks-utils-ui/records-admin` → `useResolveAllRecords` |
|
|
383
|
+
| Rule preview hook | `@proveanything/smartlinks-utils-ui/records-admin` → `useRulePreview` |
|
|
384
|
+
| Admin record CRUD (admin path) | `@proveanything/smartlinks` → `SL.app.records.{upsert, bulkUpsert, bulkDelete, restore, previewRule}` |
|
|
385
|
+
|
|
386
|
+
---
|
|
387
|
+
|
|
388
|
+
_End of doc. If anything below the SDK contradicts this file, this file wins — open a PR against the SDK to bring the two back into sync._
|