@proveanything/smartlinks 2.1.20 → 2.1.23
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/dist/adminCardsView.d.ts +1 -1
- package/dist/adminCardsView.js +70 -55
- package/dist/api/appObjects.d.ts +26 -0
- package/dist/api/appObjects.js +31 -1
- package/dist/api/conditions.d.ts +51 -0
- package/dist/api/conditions.js +57 -0
- package/dist/api/contact.d.ts +83 -1
- package/dist/api/contact.js +104 -0
- package/dist/api/index.d.ts +1 -0
- package/dist/api/index.js +1 -0
- package/dist/api/products.d.ts +22 -0
- package/dist/api/products.js +29 -0
- package/dist/api/proof.d.ts +30 -0
- package/dist/api/proof.js +37 -0
- package/dist/api/realtime.d.ts +19 -0
- package/dist/api/realtime.js +26 -1
- package/dist/docs/API_SUMMARY.md +396 -170
- package/dist/docs/admin-cards.md +1 -1
- package/dist/docs/app-manifest.md +19 -0
- package/dist/docs/assistant-apps.md +4 -0
- package/dist/docs/assistant-context.md +119 -0
- package/dist/docs/realtime.md +20 -0
- package/dist/docs/standard-query.md +187 -0
- package/dist/docs/utils.md +22 -8
- package/dist/index.d.ts +2 -0
- package/dist/index.js +0 -1
- package/dist/openapi.yaml +675 -0
- package/dist/types/adminCards.d.ts +2 -1
- package/dist/types/appManifest.d.ts +64 -1
- package/dist/types/contact.d.ts +55 -0
- package/dist/types/index.d.ts +1 -0
- package/dist/types/index.js +1 -0
- package/dist/types/query.d.ts +121 -0
- package/dist/types/query.js +5 -0
- package/dist/types/realtime.d.ts +7 -1
- package/dist/utils/conditions.d.ts +1 -1
- package/dist/utils/conditions.js +21 -6
- package/docs/API_SUMMARY.md +396 -170
- package/docs/admin-cards.md +1 -1
- package/docs/app-manifest.md +19 -0
- package/docs/assistant-apps.md +4 -0
- package/docs/assistant-context.md +119 -0
- package/docs/realtime.md +20 -0
- package/docs/standard-query.md +187 -0
- package/docs/utils.md +22 -8
- package/openapi.yaml +675 -0
- package/package.json +1 -1
package/docs/admin-cards.md
CHANGED
|
@@ -116,7 +116,7 @@ export async function cardUnanswered(ctx): Promise<AdminCardPayload> {
|
|
|
116
116
|
| `trend` | `{ direction: 'up'\|'down'\|'flat', label?, good? }` — `good` picks the colour |
|
|
117
117
|
| `status` | `{ tone: 'ok'\|'attention'\|'neutral'\|'error', text }` — `attention`/`error` also go to the To do strip |
|
|
118
118
|
| `progress` | `{ done, total, label? }` — a progress bar |
|
|
119
|
-
| `items` | Up to 5 rows `{ title, subtitle?, meta?, path? }` |
|
|
119
|
+
| `items` | Up to 5 rows `{ title, subtitle?, meta?, path?, image? }` — `image` is an https picture shown small beside the row (a product photo); an ISO-timestamp `meta` reads as a short date |
|
|
120
120
|
| `todo` | `[{ text, action? }]` — collected into the dashboard's To do strip |
|
|
121
121
|
| `actions` | Up to 2 buttons `{ label, path? \| url? }` — `path` opens your app's admin at that path |
|
|
122
122
|
| `empty` | `{ text, action? }` — shown instead of the body when there's nothing yet |
|
package/docs/app-manifest.md
CHANGED
|
@@ -219,6 +219,25 @@ A relative path (from the app's public root) to the `app.admin.json` file. Omit
|
|
|
219
219
|
"admin": "app.admin.json"
|
|
220
220
|
```
|
|
221
221
|
|
|
222
|
+
#### `adminPage`
|
|
223
|
+
|
|
224
|
+
The page a host opens as the app's **admin screen**, from the app's root: `"admin.html"` or `"index.html"`.
|
|
225
|
+
Studio, the console and Forge's preview read it rather than guess. Omit it to take the host's default:
|
|
226
|
+
|
|
227
|
+
| App | Default admin page |
|
|
228
|
+
|-----|--------------------|
|
|
229
|
+
| An installed app | `admin.html` |
|
|
230
|
+
| A core app (`core-<name>`, one of Studio's own sections) | the root page, `index.html` |
|
|
231
|
+
|
|
232
|
+
Declare it when the default is wrong. AuthKit (`core-account`) is the example: its root page is the
|
|
233
|
+
**public sign-in screen** every site sends people to, so its admin lives at `admin.html`:
|
|
234
|
+
|
|
235
|
+
```json
|
|
236
|
+
"adminPage": "admin.html"
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
Never move a public page off the root to make room for the admin: sign-in links and embeds point at the root.
|
|
240
|
+
|
|
222
241
|
#### `widgets`
|
|
223
242
|
|
|
224
243
|
Declares the widget bundle. Omit if the app has no widget component.
|
package/docs/assistant-apps.md
CHANGED
|
@@ -37,6 +37,10 @@ and change them through change sets. The declarations make it *good* at your app
|
|
|
37
37
|
The files sit in `public/assistant/` in your project and ship with every release like any other static
|
|
38
38
|
file (they are read from the release's CDN folder, on the channel the collection runs your app on).
|
|
39
39
|
|
|
40
|
+
The same block also says **what your app's data means** to the assistant (`purpose`, `objects`, `words`,
|
|
41
|
+
`recipes`): which standard objects are what in your app, and which may only be changed through your
|
|
42
|
+
actions. See **[assistant-context.md](assistant-context.md)**.
|
|
43
|
+
|
|
40
44
|
### How they are loaded
|
|
41
45
|
|
|
42
46
|
When an admin opens a chat, the assistant's box gets the **index** of every installed app's skills: one
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
# Telling the assistant what your app's data means
|
|
2
|
+
|
|
3
|
+
SmartLinks keeps a business's data in standard objects: work orders, containers and stock, bookings,
|
|
4
|
+
records, contacts, history. The assistant can read all of them. What it can't know is what they **mean in
|
|
5
|
+
your app**: that a work order is a job, that a container of type `drum` is a drum of juice, that a
|
|
6
|
+
booking with `data.event = "christmas-fair"` is a stall at the school fair.
|
|
7
|
+
|
|
8
|
+
Tell it, in the manifest's `assistant` block, beside any skills and knowledge files
|
|
9
|
+
([assistant-apps.md](assistant-apps.md)). Four keys:
|
|
10
|
+
|
|
11
|
+
```jsonc
|
|
12
|
+
// app.manifest.json
|
|
13
|
+
"assistant": {
|
|
14
|
+
"purpose": "Production manager for a fragrance filling and decorating factory: jobs from sales order to put-away.",
|
|
15
|
+
"objects": [
|
|
16
|
+
{
|
|
17
|
+
"object": "workOrder",
|
|
18
|
+
"where": { "appId": "accord-production" },
|
|
19
|
+
"means": "A job.",
|
|
20
|
+
"fields": {
|
|
21
|
+
"ref": "job number, from the sales order",
|
|
22
|
+
"kind": "job type: FillOnly, DecorateOnly, CompleteJob, PackagingOnly, Conversion",
|
|
23
|
+
"dueAt": "target date",
|
|
24
|
+
"data.stage": "the stage shown on screen",
|
|
25
|
+
"stages": "picking, then decoration, filling, packaging (\"boxing\" on screen), wrapping"
|
|
26
|
+
},
|
|
27
|
+
"write": "actions"
|
|
28
|
+
},
|
|
29
|
+
{ "object": "container", "where": { "containerType": "drum" }, "means": "A drum of bulk juice; its ref is the batch number.", "write": "actions" },
|
|
30
|
+
{ "object": "container", "where": { "containerType": "location" }, "means": "A place in the factory.", "write": "direct" },
|
|
31
|
+
{ "object": "history", "where": { "type": "job.*" }, "means": "A job's timeline.", "write": "none" }
|
|
32
|
+
],
|
|
33
|
+
"words": {
|
|
34
|
+
"GMP": "the batch manufacturing record printed for each job",
|
|
35
|
+
"the diary": "production runs by day"
|
|
36
|
+
},
|
|
37
|
+
"recipes": [
|
|
38
|
+
{ "question": "What's on tomorrow?", "answer": "this app's work order stages planned for that day, by stage; show machine or crew" },
|
|
39
|
+
{ "question": "Which jobs are late?", "answer": "use the action jobs.late: it applies the app's own rule for late" }
|
|
40
|
+
]
|
|
41
|
+
}
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
| Key | What it is | Limit |
|
|
45
|
+
|---|---|---|
|
|
46
|
+
| `purpose` | One line on what the app or site is for. The assistant always has it in view. | 200 characters |
|
|
47
|
+
| `objects` | What each standard object means here, and whether the assistant may change it. | 20 |
|
|
48
|
+
| `words` | The business's own terms. | 40 |
|
|
49
|
+
| `recipes` | How to answer the questions people actually ask. | 30 |
|
|
50
|
+
|
|
51
|
+
All four together: at most 6,000 characters. The assistant reads this every time a question touches your
|
|
52
|
+
app, so short wins. Anything malformed is left out and anything too long is cut (recipes first), without
|
|
53
|
+
an error: check what you wrote reads well.
|
|
54
|
+
|
|
55
|
+
## Objects
|
|
56
|
+
|
|
57
|
+
- **`object`**: the standard object: `workOrder`, `workOrderStage`, `container`, `stock`, `booking`,
|
|
58
|
+
`record`, `contact`, `product`, `history`.
|
|
59
|
+
- **`where`**: which ones, with the filters the SDK takes (`appId`, `containerType`, `recordType`,
|
|
60
|
+
`data.<field>`).
|
|
61
|
+
- **`means`**: what it is here, in a sentence.
|
|
62
|
+
- **`fields`**: only the fields whose name doesn't say what they hold.
|
|
63
|
+
- **`write`**: may the assistant change it?
|
|
64
|
+
|
|
65
|
+
| `write` | Means |
|
|
66
|
+
|---|---|
|
|
67
|
+
| `direct` | Through the API, as a planned change the person applies. |
|
|
68
|
+
| `actions` | **Only through one of your app's actions**, never another way. |
|
|
69
|
+
| `none` | Never. |
|
|
70
|
+
| absent | Nothing is said; the platform's usual rules apply. |
|
|
71
|
+
|
|
72
|
+
**Record types you declared in the `data` block don't need repeating.** The assistant gets their titles
|
|
73
|
+
and fields from there ([admin-lists-and-pickers.md](admin-lists-and-pickers.md)); add an `objects`
|
|
74
|
+
entry only to say what one means or to set its `write` rule.
|
|
75
|
+
|
|
76
|
+
## Actions, when changing something has rules
|
|
77
|
+
|
|
78
|
+
If creating or changing a thing involves rules that live in your code (a number from a counter, a check,
|
|
79
|
+
someone who must be told), don't describe the rules and hope: mark the object `"write": "actions"` and
|
|
80
|
+
give the assistant an **action** that does it. An action is one of your functions offered as a tool
|
|
81
|
+
([agent-tools.md](agent-tools.md)):
|
|
82
|
+
|
|
83
|
+
```jsonc
|
|
84
|
+
{
|
|
85
|
+
"name": "run.plan",
|
|
86
|
+
"trigger": { "type": "http", "methods": ["POST"] },
|
|
87
|
+
"agent": {
|
|
88
|
+
"tool": true,
|
|
89
|
+
"audience": ["assistant"],
|
|
90
|
+
"title": "Plan a production run",
|
|
91
|
+
"description": "Plan one stage of a job for a day, by hand or on a machine, with its crew.",
|
|
92
|
+
"input": { "type": "object", "properties": { "job": { "type": "string" }, "stage": { "type": "string" }, "date": { "type": "string" } }, "required": ["job", "stage", "date"] },
|
|
93
|
+
"approval": "require"
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
Your rules then run in code every time. Keep actions few and coarse: one "find jobs" with filters, not
|
|
99
|
+
five narrow ones. Use `"approval": "require"` for anything that changes or sends something: the person
|
|
100
|
+
confirms it on a card.
|
|
101
|
+
|
|
102
|
+
## A site speaks for the business
|
|
103
|
+
|
|
104
|
+
A site is the business's own app, so its description is about the **business**:
|
|
105
|
+
|
|
106
|
+
- its `purpose` leads the assistant's map of the business;
|
|
107
|
+
- it may describe objects that **other installed apps** keep. Narrow them with `where.appId`:
|
|
108
|
+
|
|
109
|
+
```jsonc
|
|
110
|
+
{ "object": "booking", "where": { "appId": "appointment-booking", "data.event": "christmas-fair" }, "means": "A stall at the Christmas fair." }
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
- where the site and an app describe the same object, the site's meaning comes first.
|
|
114
|
+
|
|
115
|
+
So "how many bookings for the Christmas fair?" is answered from the right bookings.
|
|
116
|
+
|
|
117
|
+
## Types
|
|
118
|
+
|
|
119
|
+
`AppAssistantDeclaration` (`purpose`, `objects`, `words`, `recipes`), `AssistantObject`, `AssistantRecipe`.
|
package/docs/realtime.md
CHANGED
|
@@ -37,6 +37,26 @@ off()
|
|
|
37
37
|
- **A change carries ids, never data.** Refetch through the normal API, which applies the normal
|
|
38
38
|
permissions, so realtime can never show someone data they couldn't read anyway.
|
|
39
39
|
|
|
40
|
+
### Changes to what your app works on: `onStaffChange`
|
|
41
|
+
|
|
42
|
+
Work orders, containers and stock aren't public and aren't only for admins: the people on a production
|
|
43
|
+
floor or in a warehouse need to see them move. Those changes go to the app's **staff feed**:
|
|
44
|
+
|
|
45
|
+
```ts
|
|
46
|
+
// Every screen of a production app refetches when a job, a container or stock changes.
|
|
47
|
+
const off = realtime.onStaffChange({ collectionId, appId }, {}, () => queryClient.invalidateQueries())
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
- **Types:** `workOrder` (`id`, `appId`), `container` (`id`, `kind` = its container type), `stock`
|
|
51
|
+
(`id` = the container, `productId`). Filter as with `onChange`: `{ type: 'workOrder', id }`.
|
|
52
|
+
- **What is sent:** a change to one of the app's work orders; a change to a container or stock made
|
|
53
|
+
**by the app's own server functions**; and a change to one of the app's own records (`type: 'record'`,
|
|
54
|
+
with `recordType`), except a person's private ones. Sent after the change is saved, each thing once.
|
|
55
|
+
- **Who hears it:** someone in one of the app's user groups, and collection admins. By default the
|
|
56
|
+
groups are the ones the app's server functions are limited to (`userGroups` on each function).
|
|
57
|
+
To say otherwise, name them: `"realtime": { "staff": ["manager", "warehouse-operative"] }`.
|
|
58
|
+
- **IDs only**, as always: refetch through your own functions, which apply your own rules.
|
|
59
|
+
|
|
40
60
|
With React Query, invalidate on change:
|
|
41
61
|
|
|
42
62
|
```ts
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
# The standard query
|
|
2
|
+
|
|
3
|
+
One request body to **filter, sort, page, count, total and follow links**, the same on every resource
|
|
4
|
+
that takes it. Learn it once.
|
|
5
|
+
|
|
6
|
+
| Resource | Function | Call |
|
|
7
|
+
|---|---|---|
|
|
8
|
+
| Proofs | `proof.query(collectionId, body)` | `POST /admin/collection/:id/proof/query` |
|
|
9
|
+
| Contacts | `contact.query(collectionId, body)` | `POST /admin/collection/:id/contacts/query` |
|
|
10
|
+
| Products | `products.standardQuery(collectionId, body)` | `POST /admin/collection/:id/products/standard/query` |
|
|
11
|
+
| App records | `app.records.query(collectionId, appId, body)` | `POST /admin/collection/:id/app/:appId/records/query` |
|
|
12
|
+
|
|
13
|
+
All are admin calls and read-only. Products also keep `products.query`, whose own body is unchanged;
|
|
14
|
+
`standardQuery` sits beside it.
|
|
15
|
+
|
|
16
|
+
Each resource says what its query can use: `proof.queryFields`, `contact.queryFields`,
|
|
17
|
+
`products.standardQueryFields`, `app.records.queryFields` (a `GET` on the same address). It returns the
|
|
18
|
+
fields, what each can be used for, and the links.
|
|
19
|
+
|
|
20
|
+
## The body
|
|
21
|
+
|
|
22
|
+
Everything is optional. An empty body returns the first page.
|
|
23
|
+
|
|
24
|
+
```typescript
|
|
25
|
+
interface StandardQuery {
|
|
26
|
+
where?: QueryWhere
|
|
27
|
+
orderBy?: Array<Record<string, 'asc' | 'desc'>>
|
|
28
|
+
limit?: number // rows: 1–200 (default 50); groups: 1–500
|
|
29
|
+
cursor?: string | null // nextCursor from the previous page
|
|
30
|
+
select?: string[]
|
|
31
|
+
count?: boolean
|
|
32
|
+
groupBy?: string[]
|
|
33
|
+
aggregates?: Record<string, QueryAggregate>
|
|
34
|
+
expand?: string[] | Record<string, string[] | true>
|
|
35
|
+
}
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
It answers in one of two shapes:
|
|
39
|
+
|
|
40
|
+
```typescript
|
|
41
|
+
{ items: [...], nextCursor: string | null, count?: number } // rows
|
|
42
|
+
{ groups: [...], more: boolean } // with groupBy and/or aggregates
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## Filtering: `where`
|
|
46
|
+
|
|
47
|
+
`field: value` means "equal to". `field: { op: value }` uses a comparison. Every entry must hold.
|
|
48
|
+
|
|
49
|
+
| Comparison | Meaning |
|
|
50
|
+
|---|---|
|
|
51
|
+
| `eq`, `ne` | equal, not equal |
|
|
52
|
+
| `gt`, `gte`, `lt`, `lte` | more / less than (numbers, dates, text) |
|
|
53
|
+
| `in`, `nin` | one of / none of a list (1–100 values) |
|
|
54
|
+
| `contains`, `startsWith` | text, in any letter case |
|
|
55
|
+
| `isNull` | `true`: empty; `false`: has a value |
|
|
56
|
+
| `has`, `hasAny`, `hasAll` | a list field (a contact's `tags`) holds one / any / all |
|
|
57
|
+
|
|
58
|
+
Combine with `and`, `or` and `not`, nested up to 4 levels:
|
|
59
|
+
|
|
60
|
+
```typescript
|
|
61
|
+
await proof.query(collectionId, {
|
|
62
|
+
where: {
|
|
63
|
+
createdAt: { gte: '2026-01-01', lt: '2026-02-01' },
|
|
64
|
+
or: [{ productId: 'toaster' }, { and: [{ productId: 'kettle' }, { claimable: true }] }],
|
|
65
|
+
},
|
|
66
|
+
})
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
Dates are ISO 8601 text (`"2026-01-31"` or `"2026-01-31T09:00:00Z"`).
|
|
70
|
+
|
|
71
|
+
### Product facets
|
|
72
|
+
|
|
73
|
+
A product's `facets` are filtered by key. Name the facet by its key or its name, and the value by its
|
|
74
|
+
key or its name, in any letter case. Every facet given must match.
|
|
75
|
+
|
|
76
|
+
```typescript
|
|
77
|
+
await products.standardQuery(collectionId, {
|
|
78
|
+
where: { facets: { brand: 'Breville', category: { in: ['Toasters', 'Kettles'] } } },
|
|
79
|
+
})
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
### An app's own fields: paths into a record
|
|
83
|
+
|
|
84
|
+
An app record keeps the app's own fields inside `data` (and `owner`, `admin`). Reach one by path, as a
|
|
85
|
+
field name:
|
|
86
|
+
|
|
87
|
+
```typescript
|
|
88
|
+
await app.records.query(collectionId, appId, {
|
|
89
|
+
where: { recordType: 'work_order', 'data.status': 'released', 'data.qty': { gt: 10 } },
|
|
90
|
+
select: ['ref', 'data.status', 'data.line.name'],
|
|
91
|
+
})
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
- A path can be filtered, selected, grouped and totalled. It cannot be sorted on.
|
|
95
|
+
- It reads as text. Compared with a **number** (`gt`, `gte`, `lt`, `lte`), and in `sum`, `avg`, `min`
|
|
96
|
+
and `max`, it is read as a number; a value that is not a number is left out, it does not fail the
|
|
97
|
+
query.
|
|
98
|
+
- A path follows named fields, up to 4 deep. It does not reach into lists.
|
|
99
|
+
- The documents themselves (`data`, `owner`, `admin`) are returned only when selected.
|
|
100
|
+
|
|
101
|
+
## Sorting and paging
|
|
102
|
+
|
|
103
|
+
`orderBy` takes up to 3 `{ field: 'asc' | 'desc' }`. Only fields that are never empty can be sorted
|
|
104
|
+
on (ask `queryFields`). Pass the answer's `nextCursor` as `cursor` for the next page, with the **same**
|
|
105
|
+
`where` and `orderBy`; a cursor from a different query is refused.
|
|
106
|
+
|
|
107
|
+
```typescript
|
|
108
|
+
let cursor: string | null = null
|
|
109
|
+
do {
|
|
110
|
+
const page = await contact.query(collectionId, { orderBy: [{ createdAt: 'asc' }], limit: 200, cursor })
|
|
111
|
+
if (!('items' in page)) break
|
|
112
|
+
// …use page.items
|
|
113
|
+
cursor = page.nextCursor
|
|
114
|
+
} while (cursor)
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
`count: true` adds how many rows match `where` in all, not just on this page.
|
|
118
|
+
|
|
119
|
+
## Totals: `groupBy` and `aggregates`
|
|
120
|
+
|
|
121
|
+
```typescript
|
|
122
|
+
// How many people own each product, and when the latest was registered
|
|
123
|
+
await proof.query(collectionId, {
|
|
124
|
+
where: { userId: { isNull: false } },
|
|
125
|
+
groupBy: ['productId'],
|
|
126
|
+
aggregates: { owners: { countDistinct: 'userId' }, latest: { max: 'createdAt' } },
|
|
127
|
+
})
|
|
128
|
+
// → { groups: [{ productId: 'kettle', owners: 2, latest: '2026-01-06T12:00:00.000Z' }, …], more: false }
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
- A total is one of `{ count: true }`, `{ countDistinct: field }`, `{ sum | avg | min | max: field }`.
|
|
132
|
+
- `aggregates` without `groupBy` gives one group: the totals over everything matched.
|
|
133
|
+
- Groups come ordered by the first total, largest first, unless `orderBy` names a group field or a
|
|
134
|
+
total. `more: true` means there were more groups than `limit`.
|
|
135
|
+
- A grouped query has no pages, no `select` and no `expand`.
|
|
136
|
+
|
|
137
|
+
## Following a link: `expand`
|
|
138
|
+
|
|
139
|
+
`expand` returns the thing a row points at, inside the row, under the link's name.
|
|
140
|
+
|
|
141
|
+
```typescript
|
|
142
|
+
await proof.query(collectionId, {
|
|
143
|
+
where: { productId: 'toaster' },
|
|
144
|
+
select: ['id'],
|
|
145
|
+
expand: { product: ['name', 'sku'], owner: ['firstName', 'email'] },
|
|
146
|
+
})
|
|
147
|
+
// → items: [{ id: 'p1', product: { name: 'Breville Toaster', sku: '…' }, owner: { firstName: 'Ana', email: '…' } }]
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
| Resource | Links |
|
|
151
|
+
|---|---|
|
|
152
|
+
| Proofs | `product`, `owner` (the contact with the owner's login id) |
|
|
153
|
+
| App records | `product`, `contact` |
|
|
154
|
+
|
|
155
|
+
`expand: ['product']` returns the linked thing's usual fields. A link is null when there is nothing
|
|
156
|
+
linked, and also when the linked thing would not be returned by its own query (a deleted product, an
|
|
157
|
+
erased contact). Up to 3 links, one level deep, rows only.
|
|
158
|
+
|
|
159
|
+
## What is refused
|
|
160
|
+
|
|
161
|
+
A query that asks for something a resource does not offer is refused with status 400 and a plain
|
|
162
|
+
message saying what is allowed:
|
|
163
|
+
|
|
164
|
+
```json
|
|
165
|
+
{ "errorCode": "INVALID_QUERY", "errorText": "There is no field \"colour\" on proofs. Fields: id, productId, …", "at": "where.colour" }
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
That includes a field that is not offered (anywhere: filter, sort, group or select), an unknown
|
|
169
|
+
comparison, a value of the wrong type, too many rows or values, and nesting that is too deep. A query
|
|
170
|
+
that runs for more than 5 seconds is stopped with `QUERY_TOO_SLOW`: narrow `where`.
|
|
171
|
+
|
|
172
|
+
## A question across resources
|
|
173
|
+
|
|
174
|
+
"How many people own a Breville toaster, and who registered one most recently?"
|
|
175
|
+
|
|
176
|
+
```typescript
|
|
177
|
+
const found = await products.standardQuery(collectionId, {
|
|
178
|
+
where: { facets: { brand: 'Breville', category: 'Toasters' } }, select: ['id'],
|
|
179
|
+
})
|
|
180
|
+
const ids = 'items' in found ? found.items.map((p) => String(p.id)) : []
|
|
181
|
+
const where = { productId: { in: ids }, userId: { isNull: false } }
|
|
182
|
+
|
|
183
|
+
const owners = await proof.query(collectionId, { where, aggregates: { owners: { countDistinct: 'userId' } } })
|
|
184
|
+
const latest = await proof.query(collectionId, {
|
|
185
|
+
where, orderBy: [{ createdAt: 'desc' }], limit: 1, expand: { owner: ['firstName', 'lastName'] },
|
|
186
|
+
})
|
|
187
|
+
```
|
package/docs/utils.md
CHANGED
|
@@ -670,8 +670,24 @@ await utils.validateCondition({
|
|
|
670
670
|
})
|
|
671
671
|
```
|
|
672
672
|
|
|
673
|
-
####
|
|
674
|
-
|
|
673
|
+
#### Saved Conditions (by id)
|
|
674
|
+
A collection keeps **saved conditions**: named rule sets such as "UK only" or "In the stadium", made in Studio or with the `conditions` API. Check one by id and the SDK loads it from the collection by itself (cached for the page):
|
|
675
|
+
|
|
676
|
+
```typescript
|
|
677
|
+
import { utils, conditions } from '@proveanything/smartlinks'
|
|
678
|
+
|
|
679
|
+
const show = await utils.validateCondition({
|
|
680
|
+
conditionId: 'nfCmuWDY',
|
|
681
|
+
collection: { id: 'col1' },
|
|
682
|
+
user: { valid: true, location: { country: 'GB' } },
|
|
683
|
+
})
|
|
684
|
+
|
|
685
|
+
// Admin: list, create, change, remove
|
|
686
|
+
const all = await conditions.list('col1', true)
|
|
687
|
+
await conditions.create('col1', { title: 'UK only', type: 'and', conditions: [{ type: 'country', countries: ['GB'], contains: true }] })
|
|
688
|
+
```
|
|
689
|
+
|
|
690
|
+
A rule can also refer to another saved condition, for reuse:
|
|
675
691
|
|
|
676
692
|
```typescript
|
|
677
693
|
await utils.validateCondition({
|
|
@@ -683,15 +699,13 @@ await utils.validateCondition({
|
|
|
683
699
|
passes: true // true = must pass, false = must fail
|
|
684
700
|
}]
|
|
685
701
|
},
|
|
686
|
-
|
|
687
|
-
fetchCondition: async (collectionId, conditionId) => {
|
|
688
|
-
// Your logic to fetch condition by ID
|
|
689
|
-
return { type: 'and', conditions: [...] }
|
|
690
|
-
},
|
|
691
|
-
collection: { id: 'col1' }
|
|
702
|
+
collection: { id: 'col1' },
|
|
692
703
|
})
|
|
693
704
|
```
|
|
694
705
|
|
|
706
|
+
- A saved condition that's switched off (`disabled: true`) passes for everyone; so does one that can't be found.
|
|
707
|
+
- Pass `fetchCondition: async (collectionId, conditionId) => ConditionSet | null` to load them your own way (from data you already have, or in a test).
|
|
708
|
+
|
|
695
709
|
### Combining Conditions (AND/OR Logic)
|
|
696
710
|
|
|
697
711
|
```typescript
|