@protobase/cli 0.121.8 → 0.121.9

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.
@@ -1,4 +1,4 @@
1
1
  //#region package.json
2
- var version = "0.121.8";
2
+ var version = "0.121.9";
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.121.8",
3
+ "version": "0.121.9",
4
4
  "description": "The protobase command: scaffold, doctor, dev, build and serve a Protobase project.",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
@@ -43,13 +43,13 @@
43
43
  "dependencies": {
44
44
  "@better-auth/core": "^1.7.12",
45
45
  "@hono/node-server": "^2.1.3",
46
- "@protobase/config": "0.121.8",
47
- "@protobase/layout": "0.121.8",
48
- "@protobase/query": "0.121.8",
49
- "@protobase/schema": "0.121.8",
50
- "@protobase/server": "0.121.8",
51
- "@protobase/ui": "0.121.8",
52
- "@protobase/workflows": "0.121.8",
46
+ "@protobase/config": "0.121.9",
47
+ "@protobase/layout": "0.121.9",
48
+ "@protobase/query": "0.121.9",
49
+ "@protobase/schema": "0.121.9",
50
+ "@protobase/server": "0.121.9",
51
+ "@protobase/ui": "0.121.9",
52
+ "@protobase/workflows": "0.121.9",
53
53
  "@react-email/render": "^2.1.0",
54
54
  "@scalar/hono-api-reference": "^0.12.10",
55
55
  "@sentry/vite-plugin": "^11.10.0",
@@ -56,6 +56,7 @@ Guides:
56
56
  - When showing another Protobase app's records → read [Showing another app's records](references/guides/remote-resources.md)
57
57
  - When letting users save a list as Excel or CSV → read [Exporting a list](references/guides/export-a-list.md)
58
58
  - When importing records from a spreadsheet → read [Importing records from a spreadsheet](references/guides/import-records.md)
59
+ - When charting a large events table over months, from an hourly rollup → read [Charts over millions of events](references/guides/event-charts.md)
59
60
  - When telling users about a change, in the bell and as push on their browsers → read [Send notifications](references/guides/push-notifications.md)
60
61
  - When sending mail from a person's own Gmail, such as an invoice with its PDF attached → read [Send mail from Gmail](references/guides/send-from-gmail.md)
61
62
  - When replacing a resource's create form with a page of your own → read [A create page of your own](references/guides/create-page.md)
@@ -18,7 +18,7 @@ Describe the tables of an existing Postgres database as resources, with their fi
18
18
  - [Record validation](../reference/data-config.md#resource): A rule over the whole record runs on every create and update and reports each problem on the field it concerns.
19
19
  - [Rollups and formulas](../reference/data-config.md#rollups-and-formulas) (beta): Fields that hold counts, sums, averages, minimums or maximums of related records, or values calculated from the record's own numbers, kept current by the database.
20
20
  - [Lookups](../reference/data-config.md#lookups) (beta): A field can show a field of a linked record, such as an order's customer name, read with the record in the same query and only as far as the caller may read the linked record.
21
- - [Time rollups](../reference/data-config.md#time-rollups) (in development): A large events table gets an hourly or daily rollup table that the app's migrations create and the app keeps up to date, at most a minute behind, so charts over months of millions of rows can read a few thousand rows instead.
21
+ - [Time rollups](../reference/data-config.md#time-rollups) (beta): A large events table gets an hourly or daily rollup that the app keeps up to date, so charts over months of millions of rows answer in milliseconds.
22
22
  - [Sensitive fields](../reference/data-config.md#sensitive-fields): Values such as bank account numbers are left out of every response and shown masked. A permitted user reveals one value at a time, and each reveal is audited.
23
23
  - [Recent strong sign-in](../reference/data-config.md#recent-strong-sign-in): A sensitive field can ask for a passkey or a second step in the last minutes before it is revealed, or an emailed code for an account with neither. The record page asks the person to confirm it's them, then shows it.
24
24
  - [Computed reveals](../reference/data-config.md#computed-reveals) (beta): A secret kept elsewhere, or built from several values, is revealed like a sensitive field, with the same access, audit and recent sign-in check, but made when it is asked for.
@@ -0,0 +1,70 @@
1
+ <!-- Generated by scripts/protobase-skill.ts from docs/website/src/content/docs/guides/event-charts.mdx: run `bun run docs:skill`, do not edit. -->
2
+
3
+ # Charts over millions of events
4
+
5
+ Give a large events table an hourly rollup and chart months of it on a dashboard in milliseconds.
6
+
7
+ Every meter of an energy company sends a reading a minute, so `metering.readings` grows by millions of rows a month. A chart of 90 days over those rows reads them all each time someone opens the dashboard. This guide gives the readings an hourly [time rollup](../reference/data-config.md#time-rollups) and charts that instead: a few thousand rows, the same answers.
8
+
9
+ ## 1. The events and their rollup
10
+
11
+ ```ts title="config/metering/data.ts"
12
+ import { f, resource } from '@protobase/schema'
13
+
14
+ // Meter readings, millions of rows, and their hourly rollup per organization and meter.
15
+
16
+ /** metering.readings: one row per reading. */
17
+ export const readings = resource('readings')
18
+ .table('metering.readings')
19
+ .fields({
20
+ id: f.bigint().readOnly().dbDefault(),
21
+ organization_id: f.integer().filterable(),
22
+ meter_id: f.relation('meters').filterable(),
23
+ taken_at: f.timestamp().filterable().sortable(),
24
+ watts: f.integer(),
25
+ cost: f.decimal({ precision: 10, scale: 4 }),
26
+ })
27
+ .primaryKey((r) => r.id)
28
+ .tenant((r) => r.organization_id)
29
+
30
+ /** metering.readings_hourly: one row per hour, organization and meter, which the migration creates. */
31
+ export const readingsHourly = resource('readings_hourly')
32
+ .table('metering.readings_hourly')
33
+ .bucketsOf(readings, {
34
+ time: (r) => r.taken_at,
35
+ every: 'hour',
36
+ by: (r) => [r.organization_id, r.meter_id],
37
+ measures: { watts: ['sum', 'max'], cost: ['sum'] },
38
+ late: '2 hours',
39
+ })
40
+ // Its access is its own: every member of an organization reads its totals.
41
+ .access({ read: () => true })
42
+ ```
43
+
44
+ `readings` is the table the meters write to. `readings_hourly` is a resource over it with one row per hour, organization and meter: how many readings fell in it (`events`), their total and highest watts (`watts_sum`, `watts_max`) and their total cost (`cost_sum`). Run [`protobase migration`](../reference/cli.md#migration) and apply the file it writes: it creates the rollup's table and indexes and fills it.
45
+
46
+ ## 2. A dashboard on the rollup
47
+
48
+ ```tsx title="config/metering/page.tsx"
49
+ /** @jsxImportSource @protobase/layout */
50
+ import { Chart, Grid, Page, page } from '@protobase/layout'
51
+
52
+ // A dashboard over the hourly rollup: each chart reads a few thousand buckets instead of millions of readings.
53
+ export const energy = page(
54
+ 'energy',
55
+ <Page title="Energy" description="The last 90 days of every meter.">
56
+ <Grid columns={2}>
57
+ <Chart title="Readings per day" resource="readings_hourly" groupBy="bucket" range="90d" />
58
+ <Chart title="Average watts per day" resource="readings_hourly" groupBy="bucket" average="watts_sum" range="90d" type="line" />
59
+ <Chart title="Cost per week by meter" resource="readings_hourly" groupBy="bucket" splitBy="meter_id" sum="cost_sum" granularity="week" range="90d" />
60
+ <Chart title="Cost per meter" resource="readings_hourly" groupBy="meter_id" sum="cost_sum" filter="bucket >= now() - 90d" limit={5} />
61
+ </Grid>
62
+ </Page>,
63
+ )
64
+ ```
65
+
66
+ Each [`Chart`](../reference/blocks.md#chart) names the rollup as its `resource` and groups by `bucket`, or by `meter_id`. A chart counts the readings the buckets hold, and its measure names the rollup's field: `average="watts_sum"` divides the watts by the readings, as an average over the readings themselves would. The days, weeks and months are the viewer's, for any time zone a whole number of hours off UTC.
67
+
68
+ ## 3. What a rollup cannot answer
69
+
70
+ A rollup knows nothing finer than its hour, so a chart of 15 minutes, or of days that start at half past the hour, such as India's, is refused rather than answered wrongly. Point such a chart at `readings` instead. [Charts over a rollup](../reference/data-config.md#charts-over-a-rollup) lists every measure and what is refused.
@@ -548,7 +548,7 @@ Counts, sums or averages of the records that match `filter`, per value of a fiel
548
548
  <Chart title="Revenue per day by status" resource="orders" groupBy="created_at" splitBy="status" sum="total" />
549
549
  ```
550
550
 
551
- Each value takes its colour from the view's `valueColors`, else an enum value's place among its values, from the [chart tokens](theme.md#tokens). A chart whose field the user cannot read is left out of their page, and a field it cannot group or add up stops the server at startup.
551
+ Each value takes its colour from the view's `valueColors`, else an enum value's place among its values, from the [chart tokens](theme.md#tokens). A chart whose field the user cannot read is left out of their page, and a field it cannot group or add up stops the server at startup. On a [time rollup](data-config.md#charts-over-a-rollup) it adds up the stored buckets, such as `average="watts_sum"`, and gives what the source's records give.
552
552
 
553
553
  ### Timeline
554
554
 
@@ -277,10 +277,11 @@ export const orders = resource('orders')
277
277
 
278
278
  ## Time rollups
279
279
 
280
- :::caution[In development]
281
- Coming soon:
280
+ :::caution[Beta]
281
+ Not yet:
282
282
 
283
- - <gap>Charts and `:aggregate` and `:series` cannot read a time rollup's counts, sums and averages correctly because mapping them onto its stored columns is not implemented.</gap>
283
+ - <gap>A time rollup cannot recount records that arrive later than `late` by itself because a rebuild command for older ranges is not implemented.</gap>
284
+ - <gap>A time rollup cannot average an optional field because a count of its records with a value is not implemented.</gap>
284
285
  :::
285
286
 
286
287
  A large events table, such as millions of meter readings, gets a **time rollup**: a table with one row per hour or day and per group, holding how many records fell in it and the sums and greatest values of their numbers. A chart over months then reads a few thousand rows instead of millions. You declare the rollup as a resource over the events resource, and [`protobase migration`](cli.md#migration) writes its table, indexes and refresh function into the project's next migration (`NNN_computed.sql`), with the rollup filled once. From then on the app keeps it up to date by itself, at most a minute behind. It is plain Postgres: no extension.
@@ -351,6 +352,44 @@ A record that arrives, or changes, later than `late` after its time is counted o
351
352
 
352
353
  The server does not start while a rollup's table or function is missing or out of date, with a message that names the rollup; the migration fixes that. It stops with an error, without writing anything, when the table already exists as the project's own.
353
354
 
355
+ ### Charts over a rollup
356
+
357
+ A [`Chart`](blocks.md#chart), [`:aggregate`](api.md#routes) and `:series` read a rollup as they read any resource, and give the counts, sums, averages and maxima the source's records give over the same buckets. Group by `bucket` for a chart over time, or by a `by` field. A group's count is the records its buckets hold, and a measure names the rollup's field that keeps it:
358
+
359
+ | Over the source | Over the rollup |
360
+ | --- | --- |
361
+ | The count | The count, or `sum=events` |
362
+ | `sum=watts` | `sum=watts_sum` |
363
+ | `average=watts` | `average=watts_sum`: the sum divided by the records it holds, not an average of hourly averages |
364
+ | `maximum=watts` | `maximum=watts_max` |
365
+
366
+ Any other measure, such as `average=watts_max` or `sum` of a `by` field, is refused with `400 invalid-option`, and so is what the buckets cannot tell apart:
367
+
368
+ - A granularity finer than the bucket, such as `15_minutes` on an hourly rollup or `hour` on a daily one.
369
+ - For an hourly rollup, a `time_zone` whose hours or days start inside a UTC hour over the range, such as `Asia/Kolkata`. Zones a whole number of hours off UTC, such as `Europe/Amsterdam`, give their own days, weeks and months.
370
+ - For a daily rollup, any `time_zone` but `UTC`. A chart asks for the viewer's time zone, so a chart on a daily rollup answers only to viewers in UTC; chart an hourly rollup for the others.
371
+ - An `average` of a field that is optional in the source, since `events` counts its records without a value too. Its sum and maximum work. <gap>A time rollup cannot average an optional field because a count of its records with a value is not implemented.</gap>
372
+
373
+ A bucket counts when its start is inside the range, so a range that starts inside an hour or day counts from the next one. Facets and histograms of a rollup count its rows, as for any resource. This dashboard charts the example's readings over 90 days from the hourly rollup:
374
+
375
+ ```tsx title="docs-examples/reference/time-rollups-page.tsx"
376
+ /** @jsxImportSource @protobase/layout */
377
+ import { Chart, Grid, Page, page } from '@protobase/layout'
378
+
379
+ // A dashboard over the hourly rollup: each chart reads a few thousand buckets instead of millions of readings.
380
+ export const energy = page(
381
+ 'energy',
382
+ <Page title="Energy" description="The last 90 days of every meter.">
383
+ <Grid columns={2}>
384
+ <Chart title="Readings per day" resource="readings_hourly" groupBy="bucket" range="90d" />
385
+ <Chart title="Average watts per day" resource="readings_hourly" groupBy="bucket" average="watts_sum" range="90d" type="line" />
386
+ <Chart title="Cost per week by meter" resource="readings_hourly" groupBy="bucket" splitBy="meter_id" sum="cost_sum" granularity="week" range="90d" />
387
+ <Chart title="Cost per meter" resource="readings_hourly" groupBy="meter_id" sum="cost_sum" filter="bucket >= now() - 90d" limit={5} />
388
+ </Grid>
389
+ </Page>,
390
+ )
391
+ ```
392
+
354
393
  ## Sensitive fields
355
394
 
356
395
  A sensitive field, such as a bank account number, is written like any other field but never read with the record: lists, gets, `:search`, `:seek`, create and update responses and batch results leave it out, and it cannot be filtered, sorted, searched, counted or picked with `fields`. The record page shows it masked, with an eye button. Showing it asks the server for that one value with `POST /{resource}/{key}:reveal` (see [REST API](api.md#sensitive-fields-and-audit)), which needs the same access as reading the record and the field, inside the caller's tenant, and publishes an audit event before it answers.
@@ -73,11 +73,11 @@ The [dispatch](events.md) that turns each transaction's changes into events also
73
73
  { "kind": "create", "resource": "messages", "key": "42", "channel": "conversations/7", "mentions": ["u2"] }
74
74
  ```
75
75
 
76
- `kind` is `create`, `update`, `delete` or `undelete`; `mentions` is there with `mentions` set. A notice tells clients what changed, so they read only the records they show. On a channel with members it reaches its members as the database holds them at dispatch, and a person who joined or left with that change; on an open channel, everyone joined to it. The relay sends a notice once, however often the job sends it again.
76
+ `kind` is `create`, `update`, `delete` or `undelete`; `mentions` is there with `mentions` set. A notice tells clients what changed, so they read only the records they show. On a channel with members it reaches its members as the database holds them at dispatch, and a person who joined or left with that change; on an open channel, everyone joined to it, while the anonymous role reads its record as it stands at dispatch. The relay sends a notice once, however often the job sends it again.
77
77
 
78
78
  A members record that is created or undeleted adds its person to the channel on the sockets they have open, before its notice, and one that is deleted or soft-deleted takes them out after it: they can join, or are closed out, without connecting again. Commands go in commit order, one job at a time; while the relay is down, they wait and try again, once a minute at most. A command the relay refuses, such as one over its size limit, goes to the app's error reporter and is passed over.
79
79
 
80
- <gap>An open channel's notices still reach the sockets joined to it after its record stops being public, until they reconnect, because checking read access again on the relay is not implemented.</gap>
80
+ Once an open channel's record stops being public, its notices stop, and a new ticket refuses it. <gap>Sockets that joined an open channel stay joined after its record stops being public, hearing nothing of it, until they reconnect, because closing a channel on every socket is not implemented.</gap>
81
81
 
82
82
  ## Sessions
83
83
 
@@ -843,6 +843,10 @@ With `reactions`, `edit`, `delete` or `thread`, people act on the messages where
843
843
 
844
844
  #### Calls
845
845
 
846
+ :::caution[Beta]
847
+ Calls on a conversation page may still change, and they carry the limits of [calls](calls.md) themselves. Joining or leaving the call closes an open thread.
848
+ :::
849
+
846
850
  With `call`, people start or join the record's call from the conversation's header, and talk in it beside the messages, while what they write stays the conversation's own messages. `call` names the [API function](../guides/api-functions.md) that admits people to the call with `calls.join`, as in [Admitting members](calls.md#admitting-members); the page opens its socket with the record's key as `key`:
847
851
 
848
852
  ```ts