@protobase/cli 0.23.3 → 0.23.4

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/package.js CHANGED
@@ -1,4 +1,4 @@
1
1
  //#region package.json
2
- var version = "0.23.3";
2
+ var version = "0.23.4";
3
3
  //#endregion
4
4
  export { version };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@protobase/cli",
3
- "version": "0.23.3",
3
+ "version": "0.23.4",
4
4
  "description": "The protobase command: scaffold, doctor, dev, build and serve a Protobase project.",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
@@ -35,11 +35,11 @@
35
35
  "dependencies": {
36
36
  "@hono/node-server": "^2.1.3",
37
37
  "@hono/vite-dev-server": "^0.26.1",
38
- "@protobase/layout": "0.23.3",
39
- "@protobase/query": "0.23.3",
40
- "@protobase/schema": "0.23.3",
41
- "@protobase/server": "0.23.3",
42
- "@protobase/ui": "0.23.3",
38
+ "@protobase/layout": "0.23.4",
39
+ "@protobase/query": "0.23.4",
40
+ "@protobase/schema": "0.23.4",
41
+ "@protobase/server": "0.23.4",
42
+ "@protobase/ui": "0.23.4",
43
43
  "@react-email/render": "^2.1.0",
44
44
  "@scalar/hono-api-reference": "^0.12.10",
45
45
  "@tailwindcss/vite": "^4.3.3",
@@ -65,6 +65,7 @@ Reference:
65
65
  - When delivering events to URLs → read [Webhooks](references/reference/webhooks.md)
66
66
  - When keeping or showing the changes of records → read [Record history](references/reference/history.md)
67
67
  - When adding comments to records → read [Record comments](references/reference/comments.md)
68
+ - When open pages should refresh as records change → read [Live updates](references/reference/live.md)
68
69
  - When filling fields with a model → read [AI fields and actions](references/reference/ai.md)
69
70
  - When letting people without an account create records → read [Public forms](references/reference/forms.md)
70
71
  - When running background jobs → read [Jobs](references/reference/jobs.md)
@@ -53,6 +53,7 @@ Under `/api/v1`:
53
53
  | `GET /{resource}:map?latitude=&longitude=&zoom=&filter=&order_by=&fields=&labels=&photos=&photos_order_by=` | the records in a map's view, the bounding box being in `filter` (`lat >= 51.9 AND lat <= 52.1 AND lng >= 4.2 AND lng <= 4.5`), as `{ items, buckets, total_size }`; see [Maps](#maps) |
54
54
  | `GET /{resource}:seek?position=&order_by=&filter=&page_size=&fields=&labels=&photos=&photos_order_by=` | the page at a row position (scrollbar jumps), with `next_page_token` and `prev_page_token`; same items as a list |
55
55
  | `GET /api/meta` | resource, view and page models (fields carry `default`: `{ value }` or `{ db: true }`) and the caller's `permissions`; the `ETag` hashes both, so it differs per user; `If-None-Match` gives `304` |
56
+ | `GET /api/live?after=` | a `text/event-stream` of which records of the [live](live.md)) resources the caller may read change, keys only; see [The stream](live.md#the-stream)) |
56
57
  | `POST /api/files:cleanup` | runs the [scheduled file deletes](files.md#deleting-files)) that are due, for an admin; `{ deleted, kept }` |
57
58
  | `GET /api/openapi.json`, `GET /api/docs` | OpenAPI 3.1 for the caller (only the resources, fields and operations their roles allow), and the Scalar reference |
58
59
 
@@ -17,7 +17,7 @@ const { record, etag } = await api.get(orders, id)
17
17
  await api.update(orders, id, { status: 'shipped' }, etag) // 412 rejects with PreconditionFailedError
18
18
  ```
19
19
 
20
- Pass a resource builder to get its record type ([`InferRecord`](https://docs.protobase.net/api/schema/type-aliases/inferrecord/))), or a name for loose records. Filters are AIP-160 text or [`where`](https://docs.protobase.net/api/schema/variables/where/)) results (printed with [`printFilter`](https://docs.protobase.net/api/schema/functions/printfilter/))). Errors are `ApiError` (`status`, `slug`, `errors`, `filterErrors` with spans, `blockedBy` for a delete other records point at). Also: `search`, `create`, `remove`, `facets`, `series`, `histogram`, `map(resource, { latitude, longitude, zoom, filter?, orderBy?, fields? })` (the records in a map's view, or buckets of them, see [`:map`](api.md#maps))), `seek`, `reveal(resource, key, field)` (a [sensitive field](data-config.md#sensitive-fields))'s value), `generate(resource, key, action)` (runs an [AI action](ai.md#ai-actions)) and resolves to the record as written), `meta(etag?, { signal? })`, `invoke(name, { body?, method?, path?, query? })` (one of the app's [API functions](../guides/api-functions.md#calling-a-function))), `upload(resource, field, file, { onProgress?, signal? })` (a file for a [file field](files.md#uploading)); its `value` goes in the next write, and `onProgress` uses XMLHttpRequest, since fetch reports no upload progress).
20
+ Pass a resource builder to get its record type ([`InferRecord`](https://docs.protobase.net/api/schema/type-aliases/inferrecord/))), or a name for loose records. Filters are AIP-160 text or [`where`](https://docs.protobase.net/api/schema/variables/where/)) results (printed with [`printFilter`](https://docs.protobase.net/api/schema/functions/printfilter/))). Errors are `ApiError` (`status`, `slug`, `errors`, `filterErrors` with spans, `blockedBy` for a delete other records point at). Also: `search`, `create`, `remove`, `facets`, `series`, `histogram`, `map(resource, { latitude, longitude, zoom, filter?, orderBy?, fields? })` (the records in a map's view, or buckets of them, see [`:map`](api.md#maps))), `seek`, `reveal(resource, key, field)` (a [sensitive field](data-config.md#sensitive-fields))'s value), `generate(resource, key, action)` (runs an [AI action](ai.md#ai-actions)) and resolves to the record as written), `meta(etag?, { signal? })`, `invoke(name, { body?, method?, path?, query? })` (one of the app's [API functions](../guides/api-functions.md#calling-a-function))), `upload(resource, field, file, { onProgress?, signal? })` (a file for a [file field](files.md#uploading)); its `value` goes in the next write, and `onProgress` uses XMLHttpRequest, since fetch reports no upload progress), `live({ onChanges, onResync, onRetry? })` (hears which records of the [live](live.md)) resources change until the function it returns is called, connecting again by itself).
21
21
 
22
22
  With an absolute `baseUrl`, such as another app's `https://desk.example.com/api/v1`, the `url` of each [file](files.md)) in a record the client answers is absolute too, on that API's origin, where the API answers a path on its own origin. `routedClient(own, sourceOf)` is one client over several APIs: each call about a resource goes to the client `sourceOf(resource)` gives, else to `own`, as the admin app does for [remote resources](remote-resources.md)), and `sourceMeta(meta, source)` keeps the part of another API's `/meta` an app draws.
23
23
 
@@ -0,0 +1,50 @@
1
+ <!-- Generated by scripts/protobase-skill.ts from website/src/content/docs/reference/live.mdx: run `bun run docs:skill`, do not edit. -->
2
+
3
+ # Live updates
4
+
5
+ Open lists and record pages refresh as soon as a record of a live resource changes, through the API or not, each reader seeing only what they may read.
6
+
7
+ A resource with [`.live()`](https://docs.protobase.net/api/schema/interfaces/resourcebuilder/#live)) keeps the pages people have open on its records current: when a record changes, every open list, record page, count and chart of the resource reads it again within a second or two, without a reload. The changes are captured by the same database triggers as [events](events.md)), so writes from psql, other services and cron SQL show up too, not only those through the API.
8
+
9
+ ```ts title="config/tasks.ts"
10
+ import { f, resource } from '@protobase/schema'
11
+
12
+ // A team's tasks: a list or task page someone has open shows a colleague's change within a second or two.
13
+
14
+ /** tasks */
15
+ export const tasks = resource('tasks')
16
+ .table('tasks')
17
+ .fields({
18
+ id: f.integer().readOnly().filterable().sortable(),
19
+ title: f.text(),
20
+ status: f.enum(['todo', 'doing', 'done']).default('todo').filterable(),
21
+ })
22
+ .primaryKey((r) => r.id)
23
+ .live()
24
+ ```
25
+
26
+ ## What a page hears
27
+
28
+ The server tells each open app which records changed, by resource and key, never their values. The app then reads again what it shows of that resource through the regular API, as its user, so the same tenant, row filters and field rules apply as to any other read. A page only hears of the live resources its user may list or read, in their tenant.
29
+
30
+ Changes that come close together are read once: a page gathers them for 300 milliseconds. A form with unsaved changes keeps the record it was opened with, so its Save still finds a conflict when someone else changed the record; the record is read again once the form is saved or left.
31
+
32
+ It is live data, not multiplayer editing: two people editing the same record still see a conflict on Save, as without `.live()`.
33
+
34
+ ## The stream
35
+
36
+ `GET /api/live` answers a `text/event-stream` to anyone who may read a live resource, a visitor of the anonymous role too. Each event's data is JSON with a `cursor`:
37
+
38
+ | `type` | |
39
+ | --- | --- |
40
+ | `current` | Nothing new up to `cursor`. Sent first, and every 25 seconds. |
41
+ | `changes` | `changes`: `[{ resource, key, operation }]` in commit order, `operation` being `create`, `update`, `delete` or `undelete`. |
42
+ | `resync` | Changes went by that are no longer kept: read everything shown again. |
43
+
44
+ Connecting again with `?after=<cursor>` catches up on what was missed while disconnected. [`client.live({ onChanges, onResync })`](client.md)) of `@protobase/client` follows the stream and connects again by itself, as the app does.
45
+
46
+ ## The table
47
+
48
+ [`protobase migration`](cli.md#migration)) adds the table `changes.notices` to the `changes` schema of the event capture (renamed with `events: { schema }`), and the capture triggers of each live resource; apply it with the project's other migrations. `protobase dev` and `protobase serve` do not start while the table or a trigger is missing.
49
+
50
+ A notice is a resource, a record key, the record's tenant and the operation, written by the `changes.dispatch` job in the transaction that removes the change from the outbox; a live resource therefore needs the [`jobs` schema](jobs.md#the-jobs-schema)) and the app's workers. Notices are kept for 10 minutes. Each serving process with at least one page open reads the table once a second, however many pages it serves, so live updates work with several serving processes, with `PROTOBASE_JOBS=false` and through a pooler in transaction mode.
@@ -455,6 +455,7 @@ view<typeof repairs>('repairs').create({ page: 'book-repair' })
455
455
  - `/<resource>/new` shows that page instead of the create form, so the list's **New** button and every [create address](layouts.md#addresses)) open it. The breadcrumb reads the resource and **New**, as for the form.
456
456
  - **New** shows to whoever `/api/meta` gives the page, as its `roles` say, whatever the resource's `create` access. The resource's API can then refuse creates (`create: () => false`) while the page creates through an [API function](../guides/api-functions.md)) that writes with `db`. Someone without the page gets no **New**, and `/<resource>/new` tells them they cannot create there: the create form never shows. **Import** still follows the resource's `create` access.
457
457
  - The address's `values` stay in it. A component on the page reads them with `useAppAddress()` and can keep its answers there as they are given, so a reload, a link, or a round trip through another site keeps them. See [Create pages](custom-components.md#create-pages)).
458
+ - A create address's `values` may name what is not a field of the resource, such as `{ kind: 'create', resource: 'tasks', values: { objective_id: '{id}' } }` for a task's objectives kept in a join resource, since the page reads them. Its `{field}` placeholders are still checked against the record the link sits on. A resource without a create page still refuses such values, as its create form would drop them.
458
459
  - The server refuses a page that does not exist when it starts.
459
460
 
460
461
  [A create page of your own](../guides/create-page.md)) builds one step by step.