@protobase/cli 0.46.4 → 0.46.6

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.46.4";
2
+ var version = "0.46.6";
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.46.4",
3
+ "version": "0.46.6",
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.46.4",
40
- "@protobase/query": "0.46.4",
41
- "@protobase/schema": "0.46.4",
42
- "@protobase/server": "0.46.4",
43
- "@protobase/ui": "0.46.4",
39
+ "@protobase/layout": "0.46.6",
40
+ "@protobase/query": "0.46.6",
41
+ "@protobase/schema": "0.46.6",
42
+ "@protobase/server": "0.46.6",
43
+ "@protobase/ui": "0.46.6",
44
44
  "@react-email/render": "^2.1.0",
45
45
  "@scalar/hono-api-reference": "^0.12.10",
46
46
  "@tailwindcss/vite": "^4.3.3",
@@ -57,6 +57,7 @@ Guides:
57
57
  - When importing records from a spreadsheet → read [Importing records from a spreadsheet](references/guides/import-records.md)
58
58
  - When telling users about a change, in the bell and as push on their browsers → read [Send notifications](references/guides/push-notifications.md)
59
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)
60
+ - When letting each person set a choice of their own, such as which emails they get → read [Add a personal setting](references/guides/personal-setting.md)
60
61
  - When running the app on iOS or Android → read [A mobile app](references/guides/mobile-app.md)
61
62
 
62
63
  Reference:
@@ -0,0 +1,117 @@
1
+ <!-- Generated by scripts/protobase-skill.ts from website/src/content/docs/guides/personal-setting.mdx: run `bun run docs:skill`, do not edit. -->
2
+
3
+ # Add a personal setting
4
+
5
+ A choice each person makes for themselves, such as which emails they get, in a tab of the account page, from a table, a resource and roles, with no UI code.
6
+
7
+ Rijnland's accountants want an email for each invoice that passes its due date; the warehouse would rather get none. That is a choice each person makes for themselves, so it belongs on their [account page](../reference/auth.md#account-page), and the fields the page shows come from a resource like any other. This guide adds it to the ERP example (`examples/erp`) in four steps: a table, a resource, roles and a tab.
8
+
9
+ ## 1. A table with one row per person
10
+
11
+ The table is the app's own, keyed by the person's user id. With Better Auth the id is text, and a reference to `auth."user"` removes the row with its person. Each choice is a column with its default:
12
+
13
+ ```sql title="db/migrations/021_notification_settings.sql"
14
+ -- Each person's choice of notification emails, one row per person, keyed by their Better Auth id. Someone who never
15
+ -- saved a choice has no row: read the default for them.
16
+ create table core.notification_settings (
17
+ user_id text primary key references auth."user" (id) on delete cascade,
18
+ overdue_invoices text not null default 'each' check (overdue_invoices in ('each', 'daily', 'never'))
19
+ );
20
+ ```
21
+
22
+ Apply it with `bun run db:migrate`. A person has no row until they first save; the default stands for them until then.
23
+
24
+ ## 2. A resource each person owns
25
+
26
+ The resource names the table, its fields and their defaults, and makes the key its owner with `.owner(...)`, so each person has one record of their own. The view gives the field its label, help and the words for its values, and keeps the resource out of the sidebar:
27
+
28
+ ```ts title="config/notification_settings/data.ts"
29
+ import { f, resource } from '@protobase/schema'
30
+ import { erpAccess } from '../roles'
31
+
32
+ /** core.notification_settings: each person's own, shown by the account page's Notifications tab. */
33
+ export const notificationSettings = resource('notification_settings')
34
+ .table('core.notification_settings')
35
+ .fields({
36
+ user_id: f.text(),
37
+ overdue_invoices: f.enum(['each', 'daily', 'never']).default('each'),
38
+ })
39
+ .primaryKey((r) => r.user_id)
40
+ .owner((r) => r.user_id)
41
+ .access(erpAccess('notification_settings'))
42
+ ```
43
+ ```ts title="config/notification_settings/ui.ts"
44
+ import { view } from '@protobase/schema'
45
+ import type { notificationSettings } from './data'
46
+
47
+ export const notificationSettingsView = view<typeof notificationSettings>('notification_settings')
48
+ .names({ singular: 'Notification setting', plural: 'Notification settings' })
49
+ // Each person changes their own on the account page.
50
+ .nav({ hidden: true })
51
+ .fields((r) => ({
52
+ overdue_invoices: r.overdue_invoices
53
+ .label('Overdue invoices')
54
+ .help('An email when an invoice passes its due date unpaid.')
55
+ .valueLabels({ each: 'For each invoice', daily: 'Once a day', never: 'Never' }),
56
+ }))
57
+ ```
58
+
59
+ Export both from `config/index.ts`, as every resource is.
60
+
61
+ ## 3. Roles that have settings
62
+
63
+ Give each role that should have the setting `.read.own`, `.create.own` and `.update.own` on the resource, in `config/roles.ts`:
64
+
65
+ ```ts
66
+ const ownNotificationSettings = ['notification_settings.read.own', 'notification_settings.create.own', 'notification_settings.update.own'] as const
67
+
68
+ export const roles = defineRoles<ErpName>({
69
+ admin: ['*'],
70
+ auditor: ['*.read', ...ownNotificationSettings],
71
+ sales: [...ownNotificationSettings, 'orders.read.own', ...],
72
+ // the other roles alike: manager holds them already through '*.read', '*.create' and '*.update', and the
73
+ // integration role, which is no person, has none
74
+ })
75
+ ```
76
+
77
+ A role that may read but not write sees the choice without a control to change it; a role that may not read has no Notifications tab. Once a `Settings` block shows the resource, every caller reads and writes only their own record, an admin holding `*` included, in the block and in the API alike.
78
+
79
+ ## 4. A tab on the account page
80
+
81
+ Export a page named `account` to replace the standard one: the standard tabs from `accountTabs`, in the order to show them, and a tab of your own with a [`Settings`](../reference/blocks.md#settings) block on the resource:
82
+
83
+ ```tsx title="config/account.tsx"
84
+ /** @jsxImportSource @protobase/layout */
85
+ import { Page, Settings, Tab, Tabs, accountTabs, page } from '@protobase/layout'
86
+
87
+ /** The standard account page with a Notifications tab, where each person picks the emails they get. */
88
+ export const account = page(
89
+ 'account',
90
+ <Page title="Account">
91
+ <Tabs>
92
+ {accountTabs.profile}
93
+ {accountTabs.security}
94
+ <Tab name="notifications" label="Notifications">
95
+ <Settings resource="notification_settings" title="Emails" description="What the ERP emails you about." />
96
+ </Tab>
97
+ {accountTabs.devices}
98
+ {accountTabs.apps}
99
+ </Tabs>
100
+ </Page>,
101
+ )
102
+ ```
103
+
104
+ Export it from `config/index.ts` too. Open **Account** in the user menu and then **Notifications**, at `/-/account/notifications`: the choice shows as a select with its label and help, set to **For each invoice** until it changes. Picking another value saves it at once; the first save creates the person's row.
105
+
106
+ ## Reading the choice
107
+
108
+ Code of your own, such as a job that sends the emails, reads the choice with SQL. Someone who never saved has no row, so read the default for them:
109
+
110
+ ```sql
111
+ select u.email
112
+ from auth."user" u
113
+ left join core.notification_settings s on s.user_id = u.id
114
+ where coalesce(s.overdue_invoices, 'each') = 'each'
115
+ ```
116
+
117
+ The [`Settings` block](../reference/blocks.md#settings) has its props, the field types it draws and the checks it runs at startup.
@@ -416,7 +416,7 @@ The signed-in person's own record of `resource` as settings, one field under the
416
416
 
417
417
  The startup check stops the config when the resource has no `.owner(...)`, when its owner is not its whole key, when it has a [tenant](data-config.md#resource) (settings are each person's own, not an organization's), when it has no field a person can set, and when `fields` names an unknown, key, sensitive or file field.
418
418
 
419
- A person's notification choices, in a tab of the [account page](auth.md#account-page). The table has one row per person, keyed by their user id, which is text with Better Auth:
419
+ A person's notification choices, in a tab of the [account page](auth.md#account-page); the guide [Add a personal setting](../guides/personal-setting.md) walks through the ERP example's. The table has one row per person, keyed by their user id, which is text with Better Auth:
420
420
 
421
421
  ```sql
422
422
  -- db/migrations/012_preferences.sql