@proveanything/smartlinks 2.2.1 → 3.0.0-alpha.0

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.
Files changed (330) hide show
  1. package/README.md +741 -754
  2. package/dist/README.md +573 -573
  3. package/dist/adminCardsView.js +55 -55
  4. package/dist/api/adminCards.d.ts +8 -0
  5. package/dist/api/adminCards.js +9 -1
  6. package/dist/api/ai.d.ts +22 -11
  7. package/dist/api/ai.js +18 -14
  8. package/dist/api/analytics.d.ts +13 -0
  9. package/dist/api/analytics.js +13 -0
  10. package/dist/api/appConfiguration.d.ts +17 -14
  11. package/dist/api/appConfiguration.js +14 -12
  12. package/dist/api/appObjects.d.ts +80 -16
  13. package/dist/api/appObjects.js +74 -11
  14. package/dist/api/asset.d.ts +55 -19
  15. package/dist/api/asset.js +52 -32
  16. package/dist/api/attestation.d.ts +3 -2
  17. package/dist/api/attestation.js +1 -1
  18. package/dist/api/attestations.d.ts +22 -7
  19. package/dist/api/attestations.js +22 -7
  20. package/dist/api/authKit.d.ts +22 -5
  21. package/dist/api/authKit.js +21 -4
  22. package/dist/api/batch.d.ts +23 -16
  23. package/dist/api/batch.js +19 -13
  24. package/dist/api/bookings.d.ts +45 -9
  25. package/dist/api/bookings.js +39 -3
  26. package/dist/api/broadcasts.d.ts +26 -6
  27. package/dist/api/broadcasts.js +22 -2
  28. package/dist/api/claimSet.d.ts +48 -13
  29. package/dist/api/claimSet.js +43 -7
  30. package/dist/api/collection.d.ts +20 -6
  31. package/dist/api/collection.js +19 -8
  32. package/dist/api/comms.d.ts +43 -12
  33. package/dist/api/comms.js +43 -13
  34. package/dist/api/conditions.d.ts +12 -2
  35. package/dist/api/conditions.js +10 -1
  36. package/dist/api/config.d.ts +15 -2
  37. package/dist/api/config.js +12 -0
  38. package/dist/api/contact.d.ts +34 -13
  39. package/dist/api/contact.js +35 -10
  40. package/dist/api/containers.d.ts +41 -23
  41. package/dist/api/containers.js +37 -21
  42. package/dist/api/crate.d.ts +9 -8
  43. package/dist/api/crate.js +8 -8
  44. package/dist/api/dpp.d.ts +58 -10
  45. package/dist/api/dpp.js +57 -15
  46. package/dist/api/facets.d.ts +71 -0
  47. package/dist/api/facets.js +71 -0
  48. package/dist/api/files.d.ts +11 -5
  49. package/dist/api/files.js +9 -1
  50. package/dist/api/form.d.ts +9 -2
  51. package/dist/api/form.js +7 -1
  52. package/dist/api/index.d.ts +0 -1
  53. package/dist/api/index.js +0 -1
  54. package/dist/api/integrations.d.ts +82 -27
  55. package/dist/api/integrations.js +63 -7
  56. package/dist/api/interactions.d.ts +42 -6
  57. package/dist/api/interactions.js +41 -5
  58. package/dist/api/jobs.d.ts +9 -2
  59. package/dist/api/jobs.js +6 -0
  60. package/dist/api/journeys.d.ts +13 -2
  61. package/dist/api/journeys.js +10 -1
  62. package/dist/api/journeysAnalytics.d.ts +12 -3
  63. package/dist/api/journeysAnalytics.js +9 -1
  64. package/dist/api/knowledge.d.ts +43 -10
  65. package/dist/api/knowledge.js +37 -5
  66. package/dist/api/location.d.ts +10 -3
  67. package/dist/api/location.js +7 -1
  68. package/dist/api/lots.d.ts +40 -13
  69. package/dist/api/lots.js +38 -8
  70. package/dist/api/loyalty.d.ts +70 -12
  71. package/dist/api/loyalty.js +60 -3
  72. package/dist/api/nfc.d.ts +8 -2
  73. package/dist/api/nfc.js +6 -1
  74. package/dist/api/order.d.ts +51 -160
  75. package/dist/api/order.js +46 -208
  76. package/dist/api/partner.d.ts +41 -8
  77. package/dist/api/partner.js +36 -4
  78. package/dist/api/products.d.ts +41 -8
  79. package/dist/api/products.js +33 -4
  80. package/dist/api/proof.d.ts +50 -13
  81. package/dist/api/proof.js +43 -7
  82. package/dist/api/realtime.d.ts +1 -11
  83. package/dist/api/realtime.js +1 -22
  84. package/dist/api/secrets.d.ts +25 -5
  85. package/dist/api/secrets.js +21 -2
  86. package/dist/api/segments.d.ts +26 -6
  87. package/dist/api/segments.js +22 -2
  88. package/dist/api/serviceAccounts.d.ts +8 -1
  89. package/dist/api/serviceAccounts.js +8 -1
  90. package/dist/api/shop.d.ts +41 -13
  91. package/dist/api/shop.js +35 -4
  92. package/dist/api/subscriptions.d.ts +47 -12
  93. package/dist/api/subscriptions.js +40 -4
  94. package/dist/api/tags.d.ts +40 -19
  95. package/dist/api/tags.js +38 -18
  96. package/dist/api/template.d.ts +31 -3
  97. package/dist/api/template.js +27 -0
  98. package/dist/api/translations.d.ts +17 -2
  99. package/dist/api/translations.js +14 -0
  100. package/dist/api/userGroups.d.ts +35 -9
  101. package/dist/api/userGroups.js +30 -5
  102. package/dist/api/variant.d.ts +11 -3
  103. package/dist/api/variant.js +9 -2
  104. package/dist/api/wallet.d.ts +23 -10
  105. package/dist/api/wallet.js +20 -2
  106. package/dist/apiVersion.d.ts +1 -1
  107. package/dist/apiVersion.js +1 -1
  108. package/dist/baseline.css +194 -194
  109. package/dist/docs/API_SUMMARY.md +1389 -2298
  110. package/dist/docs/PRODUCT_FACETS_SDK.md +10 -15
  111. package/dist/docs/admin-cards.md +189 -189
  112. package/dist/docs/admin-lists-and-pickers.md +1 -1
  113. package/dist/docs/agent-tools.md +156 -156
  114. package/dist/docs/ai-tools-and-skills.md +180 -180
  115. package/dist/docs/ai.md +1721 -1721
  116. package/dist/docs/analytics.md +3 -2
  117. package/dist/docs/api-versions.md +85 -76
  118. package/dist/docs/app-data-storage.md +277 -277
  119. package/dist/docs/app-docs.md +73 -73
  120. package/dist/docs/app-manifest.md +852 -852
  121. package/dist/docs/app-objects.md +38 -50
  122. package/dist/docs/app-records-pattern.md +388 -388
  123. package/dist/docs/appConfig.md +550 -550
  124. package/dist/docs/assets.md +17 -3
  125. package/dist/docs/assistant-apps.md +172 -172
  126. package/dist/docs/assistant-context.md +119 -119
  127. package/dist/docs/attestations.md +5 -4
  128. package/dist/docs/auth-kit.md +1 -1
  129. package/dist/docs/bookings.md +442 -442
  130. package/dist/docs/comms.md +689 -686
  131. package/dist/docs/connectors.md +340 -340
  132. package/dist/docs/contact-search.md +4 -3
  133. package/dist/docs/container-tracking.md +7 -7
  134. package/dist/docs/css-baseline.md +133 -133
  135. package/dist/docs/customer-guide.md +70 -70
  136. package/dist/docs/deploying-apps.md +340 -340
  137. package/dist/docs/dpp.md +115 -115
  138. package/dist/docs/entitlements.md +90 -90
  139. package/dist/docs/executor.md +1 -1
  140. package/dist/docs/files.md +69 -69
  141. package/dist/docs/forms.md +1 -1
  142. package/dist/docs/headless-providers.md +3 -3
  143. package/dist/docs/host-dependency-contract.md +252 -210
  144. package/dist/docs/i18n.md +292 -46
  145. package/dist/docs/integrations.md +322 -322
  146. package/dist/docs/interactions.md +544 -545
  147. package/dist/docs/item-context.md +181 -181
  148. package/dist/docs/knowledge.md +105 -102
  149. package/dist/docs/lists.md +114 -0
  150. package/dist/docs/lots.md +112 -111
  151. package/dist/docs/loyalty.md +348 -352
  152. package/dist/docs/members.md +183 -183
  153. package/dist/docs/memberships.md +150 -149
  154. package/dist/docs/mobile-admin-container.md +693 -693
  155. package/dist/docs/operations.md +281 -281
  156. package/dist/docs/overview.md +485 -485
  157. package/dist/docs/proof-claiming-methods.md +6 -6
  158. package/dist/docs/proof-comms-triggers.md +142 -142
  159. package/dist/docs/proof-ownership-transfer.md +155 -155
  160. package/dist/docs/proof-product-data-scoping.md +462 -462
  161. package/dist/docs/proof-share-grants.md +305 -305
  162. package/dist/docs/realtime.md +139 -138
  163. package/dist/docs/records-admin-pattern.md +304 -304
  164. package/dist/docs/server-functions.md +2 -2
  165. package/dist/docs/service-accounts.md +64 -64
  166. package/dist/docs/shop.md +4 -2
  167. package/dist/docs/site-seo.md +2 -1
  168. package/dist/docs/standard-query.md +187 -187
  169. package/dist/docs/tag-codes.md +97 -97
  170. package/dist/docs/tag-ranges.md +106 -106
  171. package/dist/docs/theme-tokens.md +252 -252
  172. package/dist/docs/translations.md +6 -1
  173. package/dist/docs/upgrading-to-3.md +430 -0
  174. package/dist/docs/user-groups.md +4 -4
  175. package/dist/docs/utils.md +1 -1
  176. package/dist/docs/wallet-passes.md +107 -107
  177. package/dist/docs/websites.md +123 -123
  178. package/dist/headless.d.ts +1 -1
  179. package/dist/headless.js +2 -2
  180. package/dist/http.d.ts +15 -2
  181. package/dist/http.js +60 -2
  182. package/dist/i18n.d.ts +2 -14
  183. package/dist/i18n.js +6 -23
  184. package/dist/index.d.ts +7 -8
  185. package/dist/index.js +2 -0
  186. package/dist/list.d.ts +24 -0
  187. package/dist/list.js +80 -0
  188. package/dist/mobile-admin/types.d.ts +0 -16
  189. package/dist/openapi.yaml +290 -2723
  190. package/dist/shared-dependencies.d.ts +3 -3
  191. package/dist/shared-dependencies.js +13 -5
  192. package/dist/site.d.ts +1 -7
  193. package/dist/site.js +1 -14
  194. package/dist/theme-boot.js +82 -82
  195. package/dist/theme.css +75 -75
  196. package/dist/types/adminCards.d.ts +6 -3
  197. package/dist/types/ai.d.ts +0 -6
  198. package/dist/types/analytics.d.ts +5 -8
  199. package/dist/types/appManifest.d.ts +4 -5
  200. package/dist/types/appObjects.d.ts +14 -18
  201. package/dist/types/appObjects.js +0 -1
  202. package/dist/types/appOptions.d.ts +18 -0
  203. package/dist/types/asset.d.ts +3 -22
  204. package/dist/types/attestations.d.ts +7 -10
  205. package/dist/types/batch.d.ts +6 -14
  206. package/dist/types/bookings.d.ts +22 -4
  207. package/dist/types/broadcasts.d.ts +5 -13
  208. package/dist/types/broadcasts.js +0 -1
  209. package/dist/types/claimSet.d.ts +0 -5
  210. package/dist/types/comms.d.ts +11 -9
  211. package/dist/types/contact.d.ts +5 -29
  212. package/dist/types/contact.js +0 -13
  213. package/dist/types/containers.d.ts +15 -26
  214. package/dist/types/crate.d.ts +4 -12
  215. package/dist/types/dpp.d.ts +3 -3
  216. package/dist/types/dpp.js +0 -1
  217. package/dist/types/error.d.ts +8 -5
  218. package/dist/types/error.js +12 -5
  219. package/dist/types/facets.d.ts +15 -15
  220. package/dist/types/index.d.ts +1 -0
  221. package/dist/types/index.js +1 -0
  222. package/dist/types/integrations.d.ts +3 -28
  223. package/dist/types/interaction.d.ts +3 -9
  224. package/dist/types/jobs.d.ts +3 -8
  225. package/dist/types/jobs.js +0 -1
  226. package/dist/types/journeys.d.ts +3 -8
  227. package/dist/types/journeys.js +0 -1
  228. package/dist/types/journeysAnalytics.d.ts +0 -1
  229. package/dist/types/list.d.ts +24 -0
  230. package/dist/types/list.js +2 -0
  231. package/dist/types/location.d.ts +0 -4
  232. package/dist/types/lots.d.ts +0 -9
  233. package/dist/types/loyalty.d.ts +0 -9
  234. package/dist/types/order.d.ts +7 -134
  235. package/dist/types/product.d.ts +11 -24
  236. package/dist/types/proof.d.ts +16 -0
  237. package/dist/types/realtime.d.ts +0 -29
  238. package/dist/types/segments.d.ts +5 -13
  239. package/dist/types/segments.js +0 -1
  240. package/dist/types/serviceAccounts.d.ts +3 -2
  241. package/dist/types/serviceAccounts.js +0 -1
  242. package/dist/types/shop.d.ts +5 -12
  243. package/dist/types/shop.js +0 -4
  244. package/dist/types/subscriptions.d.ts +18 -0
  245. package/dist/types/subscriptions.js +0 -8
  246. package/dist/types/tags.d.ts +6 -16
  247. package/dist/types/tags.js +0 -7
  248. package/dist/types/translations.d.ts +3 -9
  249. package/dist/types/variant.d.ts +0 -2
  250. package/dist/version.d.ts +1 -1
  251. package/dist/version.js +1 -1
  252. package/docs/API_SUMMARY.md +1389 -2298
  253. package/docs/PRODUCT_FACETS_SDK.md +10 -15
  254. package/docs/admin-cards.md +189 -189
  255. package/docs/admin-lists-and-pickers.md +1 -1
  256. package/docs/agent-tools.md +156 -156
  257. package/docs/ai-tools-and-skills.md +180 -180
  258. package/docs/ai.md +1721 -1721
  259. package/docs/analytics.md +3 -2
  260. package/docs/api-versions.md +85 -76
  261. package/docs/app-data-storage.md +277 -277
  262. package/docs/app-docs.md +73 -73
  263. package/docs/app-manifest.md +852 -852
  264. package/docs/app-objects.md +38 -50
  265. package/docs/app-records-pattern.md +388 -388
  266. package/docs/appConfig.md +550 -550
  267. package/docs/assets.md +17 -3
  268. package/docs/assistant-apps.md +172 -172
  269. package/docs/assistant-context.md +119 -119
  270. package/docs/attestations.md +5 -4
  271. package/docs/auth-kit.md +1 -1
  272. package/docs/bookings.md +442 -442
  273. package/docs/comms.md +689 -686
  274. package/docs/connectors.md +340 -340
  275. package/docs/contact-search.md +4 -3
  276. package/docs/container-tracking.md +7 -7
  277. package/docs/css-baseline.md +133 -133
  278. package/docs/customer-guide.md +70 -70
  279. package/docs/deploying-apps.md +340 -340
  280. package/docs/dpp.md +115 -115
  281. package/docs/entitlements.md +90 -90
  282. package/docs/executor.md +1 -1
  283. package/docs/files.md +69 -69
  284. package/docs/forms.md +1 -1
  285. package/docs/headless-providers.md +3 -3
  286. package/docs/host-dependency-contract.md +252 -210
  287. package/docs/i18n.md +292 -46
  288. package/docs/integrations.md +322 -322
  289. package/docs/interactions.md +544 -545
  290. package/docs/item-context.md +181 -181
  291. package/docs/knowledge.md +105 -102
  292. package/docs/lists.md +114 -0
  293. package/docs/lots.md +112 -111
  294. package/docs/loyalty.md +348 -352
  295. package/docs/members.md +183 -183
  296. package/docs/memberships.md +150 -149
  297. package/docs/mobile-admin-container.md +693 -693
  298. package/docs/operations.md +281 -281
  299. package/docs/overview.md +485 -485
  300. package/docs/proof-claiming-methods.md +6 -6
  301. package/docs/proof-comms-triggers.md +142 -142
  302. package/docs/proof-ownership-transfer.md +155 -155
  303. package/docs/proof-product-data-scoping.md +462 -462
  304. package/docs/proof-share-grants.md +305 -305
  305. package/docs/realtime.md +139 -138
  306. package/docs/server-functions.md +2 -2
  307. package/docs/service-accounts.md +64 -64
  308. package/docs/shop.md +4 -2
  309. package/docs/site-seo.md +2 -1
  310. package/docs/standard-query.md +187 -187
  311. package/docs/tag-codes.md +97 -97
  312. package/docs/tag-ranges.md +106 -106
  313. package/docs/theme-lookbook.v1.json +120 -120
  314. package/docs/theme-tokens.md +252 -252
  315. package/docs/translations.md +6 -1
  316. package/docs/upgrading-to-3.md +430 -0
  317. package/docs/user-groups.md +4 -4
  318. package/docs/utils.md +1 -1
  319. package/docs/wallet-passes.md +107 -107
  320. package/docs/websites.md +123 -123
  321. package/openapi.yaml +290 -2723
  322. package/package.json +91 -87
  323. package/scripts/headless-check.mjs +87 -87
  324. package/scripts/lib/bare-imports.mjs +57 -57
  325. package/scripts/lib/keyed-app-data.mjs +21 -21
  326. package/scripts/lib/upgrade.mjs +512 -0
  327. package/scripts/publish.mjs +272 -272
  328. package/scripts/register-release.mjs +91 -91
  329. package/scripts/upgrade.mjs +56 -0
  330. package/upgrade/3.json +2213 -0
@@ -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.items[0] → the single highest-specificity MatchEntry (when strategy: 'best')
223
+ // result.items[0].matchedAt → 'product' | 'rule' | 'facet' | 'collection' | …
224
+ // result.items[0].data → your record payload
225
+ ```
226
+
227
+ The server walks `proof → batch → variant → product → rule → facet → collection` and returns the first match. `result.items` 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.items → 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.items → 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._