@protobase/cli 0.45.21 → 0.45.22

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.45.21";
2
+ var version = "0.45.22";
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.45.21",
3
+ "version": "0.45.22",
4
4
  "description": "The protobase command: scaffold, doctor, dev, build and serve a Protobase project.",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
@@ -36,11 +36,11 @@
36
36
  "@better-auth/core": "^1.7.12",
37
37
  "@hono/node-server": "^2.1.3",
38
38
  "@hono/vite-dev-server": "^0.26.1",
39
- "@protobase/layout": "0.45.21",
40
- "@protobase/query": "0.45.21",
41
- "@protobase/schema": "0.45.21",
42
- "@protobase/server": "0.45.21",
43
- "@protobase/ui": "0.45.21",
39
+ "@protobase/layout": "0.45.22",
40
+ "@protobase/query": "0.45.22",
41
+ "@protobase/schema": "0.45.22",
42
+ "@protobase/server": "0.45.22",
43
+ "@protobase/ui": "0.45.22",
44
44
  "@react-email/render": "^2.1.0",
45
45
  "@scalar/hono-api-reference": "^0.12.10",
46
46
  "@tailwindcss/vite": "^4.3.3",
@@ -55,6 +55,7 @@ Guides:
55
55
  - When showing another Protobase app's records → read [Showing another app's records](references/guides/remote-resources.md)
56
56
  - When letting users save a list as Excel or CSV → read [Exporting a list](references/guides/export-a-list.md)
57
57
  - When importing records from a spreadsheet → read [Importing records from a spreadsheet](references/guides/import-records.md)
58
+ - When telling users about a change, in the bell and as push on their browsers → read [Send notifications](references/guides/push-notifications.md)
58
59
  - 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)
59
60
  - When running the app on iOS or Android → read [A mobile app](references/guides/mobile-app.md)
60
61
 
@@ -4,7 +4,7 @@
4
4
 
5
5
  A bell in the top bar and push to browsers and phones, for what your app tells its users.
6
6
 
7
- - [Notifications](../reference/notifications.md#turning-them-on) (in development): `extends: [notifications()]` gives every signed-in user a bell in the top bar and push to their browsers and phones; `ui: false` keeps only the backend.
7
+ - [Notifications](../reference/notifications.md#turning-them-on) (beta): `extends: [notifications()]` gives every signed-in user a bell in the top bar and push to their browsers and phones; `ui: false` keeps only the backend.
8
8
  - [Devices](../reference/notifications.md#devices) (beta): Each signed-in user registers their own browsers and phones to receive push, and a device gets it only while the sign-in it was registered in lasts.
9
9
  - [Your own parts](../reference/notifications.md#your-own-parts) (beta): Senders, the device store, the history store and the bell can each be replaced by your own, such as a store outside the database.
10
10
  - [Table names](../reference/notifications.md#table-names) (beta): The built-in stores' schema, tables and columns can be renamed, as auth's users table can.
@@ -0,0 +1,113 @@
1
+ <!-- Generated by scripts/protobase-skill.ts from website/src/content/docs/guides/push-notifications.mdx: run `bun run docs:skill`, do not edit. -->
2
+
3
+ # Send notifications
4
+
5
+ Turn notifications on, send one when a record changes, and see it in the bell and as push on the user's browser.
6
+
7
+ You turn notifications on, send one when something happens in your app, and see it in the bell and as push on your browser. This guide uses the ERP example (`examples/erp`, set up as its README says, with a user from `protobase users create`): when an order's status changes, its owner hears about it. The [Notifications reference](../reference/notifications.md) has every option.
8
+
9
+ ## 1. Turn notifications on
10
+
11
+ Add `notifications()` to `protobase.config.ts`:
12
+
13
+ ```ts
14
+ import { defineConfig, notifications } from '@protobase/server'
15
+
16
+ export default defineConfig({
17
+ config,
18
+ extends: [notifications()],
19
+ // auth, authenticate, jobs, ...
20
+ })
21
+ ```
22
+
23
+ Then write the tables it needs as a migration of your own, and apply it:
24
+
25
+ ```sh
26
+ bun run protobase migration # writes db/migrations/NNN_notifications.sql
27
+ bun run db:migrate
28
+ ```
29
+
30
+ `protobase dev` refuses to start until the [devices and history tables](../reference/notifications.md#table-names) are there.
31
+
32
+ ## 2. Generate the Web Push keys
33
+
34
+ Browsers receive push through [Web Push](../reference/notifications.md#web-push), which needs a key pair of your own:
35
+
36
+ ```sh
37
+ bun run protobase notifications vapid-keys
38
+ ```
39
+
40
+ Paste the two lines it prints into `.env`, and add how push services can reach you:
41
+
42
+ ```sh
43
+ PROTOBASE_VAPID_PUBLIC_KEY=...
44
+ PROTOBASE_VAPID_PRIVATE_KEY=...
45
+ PROTOBASE_VAPID_SUBJECT=mailto:you@example.com
46
+ ```
47
+
48
+ Keep the private key out of the repository. Without the keys, notifications still reach the bell, and browsers get no push.
49
+
50
+ ## 3. Send one when a record changes
51
+
52
+ Declare the [event](../reference/events.md#declaring-events) on the orders, in `config/orders/data.ts`:
53
+
54
+ ```ts
55
+ .events((e, r) => [e.updated('statusChanged', { changed: [r.status] })])
56
+ ```
57
+
58
+ Then add a [subscription](../reference/webhooks.md#subscription) that [sends a notification](../reference/notifications.md#sending) to the order's owner, with a link to the order, and export it from `config/index.ts` beside the resources:
59
+
60
+ ```ts title="config/orders/notifications.ts"
61
+ import { appPath } from '@protobase/schema'
62
+ import { sendNotification, subscription } from '@protobase/server'
63
+
64
+ // The app's public origin: the address its sign-in runs on, from its own configuration.
65
+ const appOrigin = process.env.BETTER_AUTH_URL ?? 'http://localhost:5173'
66
+
67
+ /** Tells an order's owner when its status changes, in their inbox and as push, with a link to the order. */
68
+ export const orderStatusToOwner = subscription('orderStatusToOwner', {
69
+ events: ['orders.statusChanged'],
70
+ run: async ({ data }, { db }) => {
71
+ const owner = data.after?.owner_id
72
+ if (owner == null) return
73
+ const url = new URL(appPath({ kind: 'record', resource: 'orders', key: data.key }), appOrigin).href
74
+ await sendNotification(db, { userIds: [String(owner)], inbox: true }, { title: `Order ${data.after?.number} changed status`, url })
75
+ },
76
+ })
77
+ ```
78
+
79
+ ```ts
80
+ // config/index.ts
81
+ export { orderStatusToOwner } from './orders/notifications'
82
+ ```
83
+
84
+ `inbox: true` keeps it in the owner's [history](../reference/notifications.md#history), which the bell shows, besides pushing it; `inbox: false` only pushes it. The same `sendNotification` works from a [job](../reference/jobs.md), a write hook or an [API function](api-functions.md).
85
+
86
+ The event needs triggers on the orders table, so write and apply the migration again:
87
+
88
+ ```sh
89
+ bun run protobase migration # writes db/migrations/NNN_changes.sql
90
+ bun run db:migrate
91
+ ```
92
+
93
+ ## 4. Turn push on from the bell
94
+
95
+ Run `bun run protobase dev`, open `http://localhost:5173` in a desktop browser and sign in. The [bell](../reference/notifications.md#the-bell) in the top bar opens your notifications; at its foot, **Turn on** asks the browser for permission and registers this browser as one of your [devices](../reference/notifications.md#devices). The panel then says that notifications are on for this device.
96
+
97
+ ## 5. See it arrive
98
+
99
+ Make yourself the owner of an order, then change its status, in the app or with SQL: both count, since the change is captured in the database. Within a few seconds:
100
+
101
+ - the browser shows a notification such as "Order SO-2024-000245 changed status", and a click on it opens the order;
102
+ - the bell shows one unread notification, and opening it marks it read.
103
+
104
+ Every send is a background job: a failed one is on the [failed jobs page](../reference/jobs.md#failed-jobs-page), and the server log has a line per delivery of the subscription.
105
+
106
+ On an iPhone or iPad, push reaches the app once it is added to the Home Screen, not a browser tab. Push to the Android and iOS builds of your app is coming soon; the [mobile guide](mobile-app.md) builds them.
107
+
108
+ ## 6. Turn the bell off or replace a part
109
+
110
+ - `notifications({ ui: false })` keeps sending, the history and devices, and draws no bell: your own UI reads the history and turns push on with [the browser helper](../reference/notifications.md#in-the-browser) ([without the bell](../reference/notifications.md#without-the-bell)).
111
+ - `shell.bell` in `protobase.ui.tsx` draws your own bell in its place ([the bell's slot](../reference/notifications.md#the-bells-slot)).
112
+ - Your own device store, history store or senders replace the built-in ones, such as a store outside the database ([your own parts](../reference/notifications.md#your-own-parts)).
113
+ - The tables' schema, names and columns can be renamed ([table names](../reference/notifications.md#table-names)).
@@ -506,7 +506,7 @@ For a project with [organizations](auth.md#organizations); `<organization>` is a
506
506
  protobase migration [--dir db/migrations] [--extend <dir>]
507
507
  ```
508
508
 
509
- Writes what the database still needs for every feature the config enables, as the project's next migrations in `--dir`, one file per feature: the auth schema when `protobase.config.ts` exports `auth` (`NNN_auth.sql`, as [`auth migration`](#auth-migration) writes it), the [jobs schema](jobs.md#the-jobs-schema) when the config has jobs or a resource has events, history, comments or an [AI field](ai.md) (`NNN_jobs.sql`), the [capture of events](events.md#migrations-and-startup) and the [history table](history.md#the-table) (`NNN_changes.sql`), the columns of [rollup and formula fields](data-config.md#rollups-and-formulas) and the triggers that keep the rollups (`NNN_computed.sql`), the role the [assistant's read-only queries](assistant.md#the-built-in-backend) run as, when it finds the built-in assistant's key (`NNN_query.sql`), the [comments table](comments.md#the-table) (`NNN_comments.sql`), and the [webhook subscriptions](webhooks.md#subscriptions-in-the-admin-app) of `webhooks()` (`NNN_webhooks.sql`). It compares the database, with the project's migrations applied, with the config, merged with the configs it extends and those of `--extend` as `dev` merges them, and writes nothing when the database is current. Apply the files with the project's other migrations. It reads the `.env` nearest the current directory and needs `DATABASE_URL` unless the config exports `db`.
509
+ Writes what the database still needs for every feature the config enables, as the project's next migrations in `--dir`, one file per feature: the auth schema when `protobase.config.ts` exports `auth` (`NNN_auth.sql`, as [`auth migration`](#auth-migration) writes it), the [jobs schema](jobs.md#the-jobs-schema) when the config has jobs, `webhooks()` or `notifications()`, or a resource has events, history, comments or an [AI field](ai.md) (`NNN_jobs.sql`), the [capture of events](events.md#migrations-and-startup) and the [history table](history.md#the-table) (`NNN_changes.sql`), the columns of [rollup and formula fields](data-config.md#rollups-and-formulas) and the triggers that keep the rollups (`NNN_computed.sql`), the role the [assistant's read-only queries](assistant.md#the-built-in-backend) run as, when it finds the built-in assistant's key (`NNN_query.sql`), the [comments table](comments.md#the-table) (`NNN_comments.sql`), the [webhook subscriptions](webhooks.md#subscriptions-in-the-admin-app) of `webhooks()` (`NNN_webhooks.sql`), and the [devices and history tables](notifications.md#table-names) of `notifications()` (`NNN_notifications.sql`). It compares the database, with the project's migrations applied, with the config, merged with the configs it extends and those of `--extend` as `dev` merges them, and writes nothing when the database is current. Apply the files with the project's other migrations. It reads the `.env` nearest the current directory and needs `DATABASE_URL` unless the config exports `db`.
510
510
 
511
511
  ```sh
512
512
  bun run db:migrate # the database at the project's current schema
@@ -6,11 +6,11 @@ Turn on notifications, send them to users or devices, show each signed-in user t
6
6
 
7
7
  ## Turning them on
8
8
 
9
- :::caution[In development]
10
- Sending keeps notifications in the history, which the bell shows, and pushes them to browsers; push on Android and iOS is coming soon.
9
+ :::caution[Beta]
10
+ Push reaches browsers; push on Android and iOS is coming soon.
11
11
  :::
12
12
 
13
- Notifications tell a user about something in the app, in a bell in the top bar and as push on their devices. You turn them on in `protobase.config.ts`:
13
+ Notifications tell a user about something in the app, in a bell in the top bar and as push on their devices. [Send notifications](../guides/push-notifications.md) walks through turning them on and sending the first one. You turn them on in `protobase.config.ts`:
14
14
 
15
15
  ```ts
16
16
  import { defineConfig, notifications } from '@protobase/server'
@@ -71,7 +71,7 @@ The path comes from `devicesPath('/api', id)`, and the name of its id parameter
71
71
  Only browsers receive push yet; push on Android and iOS is coming soon. <gap>A send can reach a device twice, or keep an entry twice in a history store of your own, when a worker stops mid-send because saving the send's progress in the same step as the push is not implemented.</gap>
72
72
  :::
73
73
 
74
- You send a notification from your server code, such as a job, a hook, an event subscription or an API function, with `sendNotification(db, to, message)` from `@protobase/server`. It is queued as a background job in `db`'s transaction when `db` is one, so it goes out only once your write commits, and nothing goes out when the transaction rolls back. A running job also has it as `sendNotification` in its context, beside `mail`, when the app has `notifications()`.
74
+ [Send notifications](../guides/push-notifications.md) sends one from a record change, step by step. You send a notification from your server code, such as a job, a hook, an event subscription or an API function, with `sendNotification(db, to, message)` from `@protobase/server`. It is queued as a background job in `db`'s transaction when `db` is one, so it goes out only once your write commits, and nothing goes out when the transaction rolls back. A running job also has it as `sendNotification` in its context, beside `mail`, when the app has `notifications()`.
75
75
 
76
76
  Every send to users says whether it shows in their inbox:
77
77
 
@@ -333,7 +333,7 @@ export default defineUi({ shell: { bell: Bell } })
333
333
  ## Table names
334
334
 
335
335
  :::caution[Beta]
336
- Both tables are written and checked; nothing writes to the history table until sending lands.
336
+ The names and the checks at startup may still change.
337
337
  :::
338
338
 
339
339
  The default store keeps devices in the table `devices` of the schema `notifications`: