@protobase/cli 0.40.7 → 0.40.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.
- package/dist/cli/package.js +1 -1
- package/package.json +6 -6
- package/skills/protobase/references/getting-started.md +8 -8
- package/skills/protobase/references/guides/api-functions.md +12 -12
- package/skills/protobase/references/guides/billing-page.md +2 -2
- package/skills/protobase/references/guides/create-page.md +5 -5
- package/skills/protobase/references/guides/deploy.md +6 -6
- package/skills/protobase/references/guides/export-a-list.md +2 -2
- package/skills/protobase/references/guides/field-type.md +2 -2
- package/skills/protobase/references/guides/first-composed-page.md +4 -4
- package/skills/protobase/references/guides/first-plugin.md +3 -3
- package/skills/protobase/references/guides/import-records.md +1 -1
- package/skills/protobase/references/guides/mobile-app.md +11 -4
- package/skills/protobase/references/guides/plugin-build.md +5 -5
- package/skills/protobase/references/guides/record-card.md +3 -3
- package/skills/protobase/references/guides/remote-resources.md +10 -10
- package/skills/protobase/references/guides/table-to-admin.md +5 -5
- package/skills/protobase/references/guides/theme-an-app.md +4 -4
- package/skills/protobase/references/reference/access.md +9 -9
- package/skills/protobase/references/reference/ai.md +6 -6
- package/skills/protobase/references/reference/api.md +25 -25
- package/skills/protobase/references/reference/app-identity.md +4 -4
- package/skills/protobase/references/reference/architecture.md +1 -1
- package/skills/protobase/references/reference/assistant.md +6 -6
- package/skills/protobase/references/reference/auth.md +20 -20
- package/skills/protobase/references/reference/blocks.md +18 -13
- package/skills/protobase/references/reference/bottom-bar.md +5 -5
- package/skills/protobase/references/reference/cli.md +63 -63
- package/skills/protobase/references/reference/client.md +5 -5
- package/skills/protobase/references/reference/comments.md +8 -8
- package/skills/protobase/references/reference/custom-components.md +18 -18
- package/skills/protobase/references/reference/data-config.md +26 -26
- package/skills/protobase/references/reference/email.md +8 -8
- package/skills/protobase/references/reference/events.md +5 -5
- package/skills/protobase/references/reference/files.md +7 -7
- package/skills/protobase/references/reference/forms.md +6 -6
- package/skills/protobase/references/reference/history.md +7 -7
- package/skills/protobase/references/reference/installable-app.md +1 -1
- package/skills/protobase/references/reference/jobs.md +5 -5
- package/skills/protobase/references/reference/layouts.md +11 -11
- package/skills/protobase/references/reference/live.md +4 -4
- package/skills/protobase/references/reference/mcp.md +5 -5
- package/skills/protobase/references/reference/plugins.md +20 -20
- package/skills/protobase/references/reference/remote-resources.md +15 -15
- package/skills/protobase/references/reference/sidebar.md +1 -1
- package/skills/protobase/references/reference/supabase-realtime.md +2 -2
- package/skills/protobase/references/reference/supabase-rest.md +2 -2
- package/skills/protobase/references/reference/supabase-storage.md +1 -1
- package/skills/protobase/references/reference/theme.md +5 -5
- package/skills/protobase/references/reference/ui-config.md +60 -60
- package/skills/protobase/references/reference/user-menu.md +3 -3
- package/skills/protobase/references/reference/versioning.md +5 -5
- package/skills/protobase/references/reference/webhooks.md +7 -7
|
@@ -47,7 +47,7 @@ export const OrdersPerDay = ({ days }: { days: 7 | 30 | 90 }) => {
|
|
|
47
47
|
}
|
|
48
48
|
```
|
|
49
49
|
|
|
50
|
-
`protobase dev` serves it with hot reload, and `protobase build` builds it as the [plugin](plugins.md)
|
|
50
|
+
`protobase dev` serves it with hot reload, and `protobase build` builds it as the [plugin](plugins.md) `app` in the bundle's `public/plugins/app/`, beside the prebuilt admin app, which loads it when someone signs in. It is the only project code that reaches the browser, so keep secrets out of it. `definePlugin` types its default export as well as `defineUi`, which stays as its alias. The same exports can come from other plugins too, which the app loads at runtime and which keep working when the app moves to a newer release.
|
|
51
51
|
|
|
52
52
|
## Components
|
|
53
53
|
|
|
@@ -62,7 +62,7 @@ export const OrdersPerDay = component<{ days: 7 | 30 | 90 }>('OrdersPerDay')
|
|
|
62
62
|
export const overview = page('overview', <Page title="Overview"><Card title="Orders per day"><OrdersPerDay days={30} /></Card></Page>)
|
|
63
63
|
```
|
|
64
64
|
|
|
65
|
-
The element stays data, `{ type: 'OrdersPerDay', props: { days: 30 }, children: [], custom: true }`, so its props follow the same [rules](layouts.md#what-a-layout-may-hold)
|
|
65
|
+
The element stays data, `{ type: 'OrdersPerDay', props: { days: 30 }, children: [], custom: true }`, so its props follow the same [rules](layouts.md#what-a-layout-may-hold) as any block's. The name must be PascalCase and not a built-in block's.
|
|
66
66
|
|
|
67
67
|
`protobase.ui.tsx` exports the React component under the same name. It gets the props, and the rendered children when the element has any. Inside it:
|
|
68
68
|
|
|
@@ -89,7 +89,7 @@ A prop that names a field is a field reference, `fieldRef('plan.name')` from `@p
|
|
|
89
89
|
|
|
90
90
|
## List displays
|
|
91
91
|
|
|
92
|
-
A list can show its rows with a component of the project's own, declared in the view with [`d.custom`](ui-config.md#list-displays)
|
|
92
|
+
A list can show its rows with a component of the project's own, declared in the view with [`d.custom`](ui-config.md#list-displays). The list page loads the rows, with the filter, search and order of the address, and the component draws them: `useListDisplay()` gives them, `RecordLink` opens one. A board in columns of an enum field, with cards dragged between them, is built in as [`d.board`](ui-config.md#boards), a month or week calendar of a date field as [`d.calendar`](ui-config.md#calendars), and a Gantt-style timeline of bars from a start to an end date as [`d.timeline`](ui-config.md#timelines); the board below draws its own.
|
|
93
93
|
|
|
94
94
|
```tsx
|
|
95
95
|
// components/repair-board.tsx, exported from protobase.ui.tsx: export { RepairBoard } from './components/repair-board'
|
|
@@ -124,7 +124,7 @@ A display never fetches its list, changes the address or keeps its own history:
|
|
|
124
124
|
|
|
125
125
|
## Record sections
|
|
126
126
|
|
|
127
|
-
A record page can hold a component of the project's own beside its sections, declared in the view with [`l.custom`](ui-config.md#the-record-page)
|
|
127
|
+
A record page can hold a component of the project's own beside its sections, declared in the view with [`l.custom`](ui-config.md#the-record-page). `useLayoutRecord()` gives it the record the page shows, as inside a `RecordCard`, so one component works in both. It reads more through the hooks (`useList`, `useRecord`), and acts through named actions or the API. Breadcrumbs, the heading, Save, the action rail with Edit and Delete, and the related records stay the page's.
|
|
128
128
|
|
|
129
129
|
```tsx
|
|
130
130
|
// components/customer-repairs.tsx
|
|
@@ -162,21 +162,21 @@ export const RepairCard = ({ row }: RecordCardProps) => (
|
|
|
162
162
|
)
|
|
163
163
|
```
|
|
164
164
|
|
|
165
|
-
`row` is `{ key, record, labels }`, as a [list display's](#list-displays) rows are. A card is inside a link, so it holds no buttons or links of its own. Without the component, from a plugin that is off for instance, and when it throws, each card is the built-in one: the title, subtitle, badge and meta fields. [A custom record card](../guides/record-card.md)
|
|
165
|
+
`row` is `{ key, record, labels }`, as a [list display's](#list-displays) rows are. A card is inside a link, so it holds no buttons or links of its own. Without the component, from a plugin that is off for instance, and when it throws, each card is the built-in one: the title, subtitle, badge and meta fields. [A custom record card](../guides/record-card.md) builds one step by step.
|
|
166
166
|
|
|
167
167
|
## Field types
|
|
168
168
|
|
|
169
|
-
A [field type](plugins.md#field-types)
|
|
169
|
+
A [field type](plugins.md#field-types) shows and edits a field everywhere the app shows it: a view picks one with `r.status.widget('steps')`, and `protobase.ui.tsx` registers it under that name in `fields`:
|
|
170
170
|
|
|
171
171
|
```tsx
|
|
172
172
|
export default definePlugin({ fields: { steps: { of: ['enum'], display: StepsDisplay, editor: StepsEditor } } })
|
|
173
173
|
```
|
|
174
174
|
|
|
175
|
-
[Plugins](plugins.md#field-types)
|
|
175
|
+
[Plugins](plugins.md#field-types) describes the contract, and [Adding a field type](../guides/field-type.md) builds one.
|
|
176
176
|
|
|
177
177
|
## Addresses
|
|
178
178
|
|
|
179
|
-
Every place in the app has an [address](layouts.md#addresses)
|
|
179
|
+
Every place in the app has an [address](layouts.md#addresses), and a component never writes a path or reads the query string itself, so the address can change shape in one place. `useAppAddress()` from `@protobase/ui` gives the current one as values, with the list's defaults filled in, and moves the app:
|
|
180
180
|
|
|
181
181
|
```tsx
|
|
182
182
|
const { address, set, listTo, go, href } = useAppAddress()
|
|
@@ -187,11 +187,11 @@ go({ kind: 'record', resource: 'repairs', key: 42 }) // moves to another addres
|
|
|
187
187
|
href({ kind: 'create', resource: 'repairs' }) // for an <a href>, with the base path
|
|
188
188
|
```
|
|
189
189
|
|
|
190
|
-
`RecordLink` (a record, from a display's `row` or a `recordKey`) and `AppLink` (`to` any address) are links written the same way. Code outside React builds and reads addresses with [`appPath(address, basePath?)`](https://docs.protobase.net/api/schema/functions/apppath/)
|
|
190
|
+
`RecordLink` (a record, from a display's `row` or a `recordKey`) and `AppLink` (`to` any address) are links written the same way. Code outside React builds and reads addresses with [`appPath(address, basePath?)`](https://docs.protobase.net/api/schema/functions/apppath/) and [`readAppPath(path, pages)`](https://docs.protobase.net/api/schema/functions/readapppath/) from `@protobase/schema`, whose words (`filter`, `order_by`, `display`) are in [`addressWords`](https://docs.protobase.net/api/schema/variables/addresswords/).
|
|
191
191
|
|
|
192
192
|
## Create pages
|
|
193
193
|
|
|
194
|
-
A view whose records a page of the app's own creates names it with [`.create({ page })`](ui-config.md#create-pages)
|
|
194
|
+
A view whose records a page of the app's own creates names it with [`.create({ page })`](ui-config.md#create-pages), and `/<resource>/new` shows that page. The address keeps its `values`: a component on the page reads them from `useAppAddress()`, and writes its answers back with `go(..., { replace: true })`, so a reload, a link, or a round trip through another site, such as an install page that sends the user back, keeps them:
|
|
195
195
|
|
|
196
196
|
```tsx
|
|
197
197
|
const { address, go } = useAppAddress()
|
|
@@ -242,7 +242,7 @@ export const BookRepair = () => {
|
|
|
242
242
|
|
|
243
243
|
## Components in the chat
|
|
244
244
|
|
|
245
|
-
The same components draw the assistant's [widget parts](assistant.md#widget-parts)
|
|
245
|
+
The same components draw the assistant's [widget parts](assistant.md#widget-parts): a part names a component, and the dock draws it with the part's props, so one component can sit on a page and in the chat. In the chat there is no record around it, so a component that needs `useLayoutRecord()` works only on pages; take ids as props instead. Besides the API hooks, a widget can read the assistant backend with [`useBackendData`](assistant.md#reading-the-backend-from-a-widget).
|
|
246
246
|
|
|
247
247
|
## Action handlers
|
|
248
248
|
|
|
@@ -252,23 +252,23 @@ A named action declared with `a.action(name, ...)` in a view has no built-in beh
|
|
|
252
252
|
| --- | --- |
|
|
253
253
|
| `resource` | The action's resource. |
|
|
254
254
|
| `record`, `recordKey`, `etag` | The record around the button, when there is one. |
|
|
255
|
-
| `client` | The API client (`@protobase/client`) of the API that serves the resource, with the user's token: the app's own, or a source's for a [remote resource](remote-resources.md)
|
|
256
|
-
| `navigate(to)` | Moves the app to an [address](layouts.md#addresses)
|
|
257
|
-
| `values` | The answers to the action's [inputs](ui-config.md#actions-that-ask-for-input)
|
|
255
|
+
| `client` | The API client (`@protobase/client`) of the API that serves the resource, with the user's token: the app's own, or a source's for a [remote resource](remote-resources.md), whose functions `invoke` then calls. |
|
|
256
|
+
| `navigate(to)` | Moves the app to an [address](layouts.md#addresses), such as `{ kind: 'record', resource: 'orders', key: 42 }`. |
|
|
257
|
+
| `values` | The answers to the action's [inputs](ui-config.md#actions-that-ask-for-input), by input name, when it has some. |
|
|
258
258
|
|
|
259
259
|
When the handler resolves, everything shown of the resource reloads and a toast says it is done; when it throws, the toast shows the error, or for an action with inputs its dialog does, under the inputs an `ActionInputError` names. Without a handler the button is disabled. Actions with built-in behaviour (`a.update`, `a.remove`, `a.link`) do not need one.
|
|
260
260
|
|
|
261
261
|
## Shell slots
|
|
262
262
|
|
|
263
|
-
`shell` adds components to the shell around every page: `actions` at the end of the top bar, and `rightPanel` beside the page, such as a `SidePanel` from `@protobase/ui` (see [the primitives](assistant.md#in-the-ui))
|
|
263
|
+
`shell` adds components to the shell around every page: `actions` at the end of the top bar, and `rightPanel` beside the page, such as a `SidePanel` from `@protobase/ui` (see [the primitives](assistant.md#in-the-ui)). They take no props and use the same hooks as custom components:
|
|
264
264
|
|
|
265
265
|
```tsx
|
|
266
266
|
export default defineUi({ shell: { actions: NotesButton, rightPanel: Notes } })
|
|
267
267
|
```
|
|
268
268
|
|
|
269
|
-
When the app has an [assistant](assistant.md)
|
|
269
|
+
When the app has an [assistant](assistant.md), its button follows the project's actions and its dock the project's panel.
|
|
270
270
|
|
|
271
|
-
`logo` replaces Protobase's logo at the top of the sidebar with the app's own. It gets `compact`, true when the sidebar shows only icons, so it draws a mark alone, `workspace`, the name of the user's organization that Protobase's logo shows under it, `appName`, the app's [name](auth.md#configuration)
|
|
271
|
+
`logo` replaces Protobase's logo at the top of the sidebar with the app's own. It gets `compact`, true when the sidebar shows only icons, so it draws a mark alone, `workspace`, the name of the user's organization that Protobase's logo shows under it, `appName`, the app's [name](auth.md#configuration), which Protobase's logo shows beside its mark, and `appIcon`, the path of the project's own [icon](app-identity.md#the-icons) when it has one (`ShellLogoProps`). Its colors follow the [theme](theme.md), and a `dark:` class gives dark its own. When it throws, Protobase's logo shows instead. The tab's icon is [the favicon](theme.md#logo-and-favicon).
|
|
272
272
|
|
|
273
273
|
```tsx
|
|
274
274
|
import { defineUi, type ShellLogoProps } from '@protobase/ui'
|
|
@@ -287,4 +287,4 @@ export default defineUi({ shell: { logo: Logo } })
|
|
|
287
287
|
|
|
288
288
|
`<App ui={...} />` from `@protobase/ui` takes the same object, for an app that mounts the admin itself, and so do stories.
|
|
289
289
|
|
|
290
|
-
Below 1.0, a project built before 0.12 had its `protobase.ui.tsx` built into the admin app; [Moving onto the plugin build](../guides/plugin-build.md)
|
|
290
|
+
Below 1.0, a project built before 0.12 had its `protobase.ui.tsx` built into the admin app; [Moving onto the plugin build](../guides/plugin-build.md) says what changes.
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
|
|
5
5
|
Resources in data.ts, every field type and option, keys and relations, soft delete, tenants, search, validation and access.
|
|
6
6
|
|
|
7
|
-
A **resource** describes one table of a database you already have: its columns as fields, its key, and the rules around it. It is plain TypeScript in `config/<resource>/data.ts`, exported from `config/index.ts`; [`protobase scaffold`](cli.md#scaffold)
|
|
7
|
+
A **resource** describes one table of a database you already have: its columns as fields, its key, and the rules around it. It is plain TypeScript in `config/<resource>/data.ts`, exported from `config/index.ts`; [`protobase scaffold`](cli.md#scaffold) writes a first version from the database. Every example on this page is a file in the repository that CI typechecks and tests.
|
|
8
8
|
|
|
9
9
|
```ts title="docs-examples/bike-shop/data.ts"
|
|
10
10
|
import { f, resource } from '@protobase/schema'
|
|
@@ -54,21 +54,21 @@ export const repairs = resource('repairs')
|
|
|
54
54
|
|
|
55
55
|
| | |
|
|
56
56
|
| --- | --- |
|
|
57
|
-
| [`resource(name)`](https://docs.protobase.net/api/schema/functions/resource/)
|
|
57
|
+
| [`resource(name)`](https://docs.protobase.net/api/schema/functions/resource/) | The name is the resource's address (`/api/v1/<name>`, `/<name>` in the admin) and how other config refers to it. It may not contain `/`, `:`, `?` or `#`, and must be unique. |
|
|
58
58
|
| `.table('schema.table')` | The table, with or without its schema. Required. |
|
|
59
59
|
| `.fields({ ... })` | The columns, by field name. `name: null` ignores a column on purpose (scaffold writes it, so it does not offer the column again). Required. |
|
|
60
60
|
| `.primaryKey((r) => r.id)` | The key, one field or a list (`(r) => [r.order_id, r.line_no]`). Required; records are addressed by it. |
|
|
61
|
-
| `.softDelete((r) => r.deleted_at)` | A nullable timestamp or date: deleting sets it, lists leave such rows out, `:undelete` clears it. See [REST API](api.md#routes)
|
|
61
|
+
| `.softDelete((r) => r.deleted_at)` | A nullable timestamp or date: deleting sets it, lists leave such rows out, `:undelete` clears it. See [REST API](api.md#routes). |
|
|
62
62
|
| `.tenant((r) => r.organization_id)` | The column that separates tenants: every query is limited to the caller's tenant, creates set it, and a request body may not. |
|
|
63
63
|
| `.owner((r) => r.owner_id)` | The field holding the owning user's id, which `.own` capabilities filter on (see [Access](#access)). |
|
|
64
|
-
| `.search((r) => [r.number, r.name])` | The fields that `search("...")` and bare words in a filter match, and what the admin's [global search](client.md#global-search)
|
|
65
|
-
| `.position((r) => r.position, { within?, after? })` | The integer field holding the records' order by hand, 1 the first, which a list's **Reorder** button changes by dragging; with `within: (r) => r.invoice_id`, a filterable relation, each parent's records have an order of their own, sorted in its related section; with `after`, a record never ranks above the records it depends on. The field must be sortable. See [Reordering by hand](ui-config.md#reordering-by-hand)
|
|
66
|
-
| `.parent((r) => r.vm_id, 'update')` | The relation whose record each record belongs to, and the access on it (`'read'` or `'update'`) that creating, changing, deleting and undeleting a record needs, checked with the caller's own rules on that resource. The relation is one column, to a resource with a key of one field. See [Records that belong to another](access.md#records-that-belong-to-another)
|
|
64
|
+
| `.search((r) => [r.number, r.name])` | The fields that `search("...")` and bare words in a filter match, and what the admin's [global search](client.md#global-search) looks in. Every word of the text must appear in one of them, ignoring case, and part of a word is enough: `port` finds "Portfolio". `r.phone.digitsEnd()` matches only the end of a field's digits instead, ignoring spaces, `+` and dashes, and only words written like phone numbers: `9623`, `47069623` and `+31 6 47069623` find "+31 6 47069623", `4706` and `+316` do not. Each field needs a pg_trgm index (`using gin (name gin_trgm_ops)`), a `digitsEnd` one on `regexp_replace(phone, '[^0-9]', '', 'g')`, or the API refuses the search. |
|
|
65
|
+
| `.position((r) => r.position, { within?, after? })` | The integer field holding the records' order by hand, 1 the first, which a list's **Reorder** button changes by dragging; with `within: (r) => r.invoice_id`, a filterable relation, each parent's records have an order of their own, sorted in its related section; with `after`, a record never ranks above the records it depends on. The field must be sortable. See [Reordering by hand](ui-config.md#reordering-by-hand) and [Ranked after what they depend on](ui-config.md#ranked-after-what-they-depend-on). |
|
|
66
|
+
| `.parent((r) => r.vm_id, 'update')` | The relation whose record each record belongs to, and the access on it (`'read'` or `'update'`) that creating, changing, deleting and undeleting a record needs, checked with the caller's own rules on that resource. The relation is one column, to a resource with a key of one field. See [Records that belong to another](access.md#records-that-belong-to-another). |
|
|
67
67
|
| `.validate((record) => issues)` | A rule over the whole record, run after the field checks on every create and update. It returns `[{ field, message }]` or `undefined`; issues answer `400` with the field named. |
|
|
68
68
|
| `.access({ ... })` | Rules per operation. See [Access](#access). |
|
|
69
|
-
| `.events((e, r) => [...])` | What changes of its records mean, such as `orders.created`, captured in the database for every write. See [Events](events.md)
|
|
70
|
-
| `.history()` | Keeps every change of its records with who made it, shown on the record page and listed for admins as Activity. See [Record history](history.md)
|
|
71
|
-
| `.comments()` | Lets whoever may read a record comment on it and @mention others who may, who are emailed; shown on the record page and listed for admins. See [Record comments](comments.md)
|
|
69
|
+
| `.events((e, r) => [...])` | What changes of its records mean, such as `orders.created`, captured in the database for every write. See [Events](events.md). |
|
|
70
|
+
| `.history()` | Keeps every change of its records with who made it, shown on the record page and listed for admins as Activity. See [Record history](history.md). |
|
|
71
|
+
| `.comments()` | Lets whoever may read a record comment on it and @mention others who may, who are emailed; shown on the record page and listed for admins. See [Record comments](comments.md). |
|
|
72
72
|
|
|
73
73
|
Field and resource names are snake_case by convention, like the columns: they are the names in URLs, the API, filters and sorts as written (`/stock_moves?order_by=moved_at desc`). The column is the snake_case of the name unless `.column()` says otherwise, so a camelCase name such as `movedAt` maps to `moved_at` too.
|
|
74
74
|
|
|
@@ -76,7 +76,7 @@ Field and resource names are snake_case by convention, like the columns: they ar
|
|
|
76
76
|
|
|
77
77
|
| Builder | Column types | API value | Options |
|
|
78
78
|
| --- | --- | --- | --- |
|
|
79
|
-
| `f.text()` | `text`, `varchar`, `bpchar` | string | `.min(n)`, `.max(n)` (length), `.regex(re, message)`; a text of several lines, such as a description, takes the view's [`.multiline()`](ui-config.md#names-and-columns)
|
|
79
|
+
| `f.text()` | `text`, `varchar`, `bpchar` | string | `.min(n)`, `.max(n)` (length), `.regex(re, message)`; a text of several lines, such as a description, takes the view's [`.multiline()`](ui-config.md#names-and-columns), and markdown its [`.markdown()`](ui-config.md#names-and-columns) |
|
|
80
80
|
| `f.integer()` | `int2`, `int4` | number | `.min(n)`, `.max(n)` |
|
|
81
81
|
| `f.bigint()` | `int8` | string (all 64 bits) | |
|
|
82
82
|
| `f.decimal({ precision, scale })` | `numeric` | string (exact) | |
|
|
@@ -89,13 +89,13 @@ Field and resource names are snake_case by convention, like the columns: they ar
|
|
|
89
89
|
| `f.relation('resource')` | the key column(s) of another resource | its key | `.columns([...])` |
|
|
90
90
|
| `f.currency()` | `text` | ISO 4217 code (`EUR`) | |
|
|
91
91
|
| `f.country()` | `text` | ISO 3166-1 alpha-2 code (`NL`) | |
|
|
92
|
-
| `f.file()` | `text` | `{ uri, name, type, size, url }`; written with a ticket from `:upload` | `.accept([...])`, `.maxSize(size)`, `.public()`, `.storage(name)`, `.derive({...})`; see [Files](files.md)
|
|
92
|
+
| `f.file()` | `text` | `{ uri, name, type, size, url }`; written with a ticket from `:upload` | `.accept([...])`, `.maxSize(size)`, `.public()`, `.storage(name)`, `.derive({...})`; see [Files](files.md) |
|
|
93
93
|
|
|
94
|
-
A constraint a type does not support (`f.boolean().min(1)`) throws when the config loads. The builders are the members of [`f`](https://docs.protobase.net/api/schema/variables/f/)
|
|
94
|
+
A constraint a type does not support (`f.boolean().min(1)`) throws when the config loads. The builders are the members of [`f`](https://docs.protobase.net/api/schema/variables/f/).
|
|
95
95
|
|
|
96
96
|
## Field options
|
|
97
97
|
|
|
98
|
-
Every option returns a new [field](https://docs.protobase.net/api/schema/interfaces/field/)
|
|
98
|
+
Every option returns a new [field](https://docs.protobase.net/api/schema/interfaces/field/), so they chain in any order.
|
|
99
99
|
|
|
100
100
|
| Option | Effect |
|
|
101
101
|
| --- | --- |
|
|
@@ -103,14 +103,14 @@ Every option returns a new [field](https://docs.protobase.net/api/schema/interfa
|
|
|
103
103
|
| `.readOnly()` | Never written through the API: ids, computed columns, audit timestamps. |
|
|
104
104
|
| `.default(value)` | The value a create uses when none is given; the admin's create form shows it. |
|
|
105
105
|
| `.dbDefault()` | The database supplies a value when none is given (identity, `now()`, a sequence). |
|
|
106
|
-
| `.filterable()` | May appear in `filter`, in filter widgets and in `Stat`/`Table` filters. It needs an index: the API refuses a filter on a column no index serves (see the [scan guard](api.md#safety))
|
|
106
|
+
| `.filterable()` | May appear in `filter`, in filter widgets and in `Stat`/`Table` filters. It needs an index: the API refuses a filter on a column no index serves (see the [scan guard](api.md#safety)), and [`protobase doctor`](cli.md#doctor) fails without one. |
|
|
107
107
|
| `.sortable()` | May appear in `order_by` and in sorts. It needs a btree index that serves it alone, as a filterable field does, since a list may be sorted by it. A list's header of a field without it is no button and says "This column can't be sorted" on hover and in its menu. |
|
|
108
108
|
| `.alias(...names)` | Other names filters may use for the field. |
|
|
109
109
|
| `.column('name')` | The column, when it is not the snake_case of the field name (`'EMP_NAME'`). |
|
|
110
110
|
| `.access({ read, create, update })` | Role rules for this column. See [Access](#access). |
|
|
111
111
|
| `.sensitive()` | Hidden like a password: no response carries it, and a caller who may read it reveals one record's value at a time. See [Sensitive fields](#sensitive-fields). |
|
|
112
112
|
| `.encrypted()` | Sensitive, and stored encrypted with the app's own key, so the database only ever holds ciphertext. Text only. See [Encrypted fields](#encrypted-fields). |
|
|
113
|
-
| `.ai({ prompt })` | A job fills the field from `prompt`, `{field}` replaced by the record's values, after a create and after an update of a field the prompt reads, as the user who wrote. Text or enum, optional. See [AI fields and actions](ai.md)
|
|
113
|
+
| `.ai({ prompt })` | A job fills the field from `prompt`, `{field}` replaced by the record's values, after a create and after an update of a field the prompt reads, as the user who wrote. Text or enum, optional. See [AI fields and actions](ai.md). |
|
|
114
114
|
| `.rollup({ from, via, fn, field })` | The database keeps the field from the records of another resource: their count, sum, average, least or greatest value. Read-only. See [Rollups and formulas](#rollups-and-formulas). |
|
|
115
115
|
| `.formula(expression)` | The database computes the field from the record's other number fields, as a generated column. Read-only. See [Rollups and formulas](#rollups-and-formulas). |
|
|
116
116
|
|
|
@@ -146,7 +146,7 @@ export const products = resource('products')
|
|
|
146
146
|
|
|
147
147
|
## Rollups and formulas
|
|
148
148
|
|
|
149
|
-
A **rollup** is a value of the other records that point at a record, such as an order's total of its lines. A **formula** is a value computed from the record's own fields, such as a line's amount. Neither needs SQL: you declare the field, and [`protobase migration`](cli.md#migration)
|
|
149
|
+
A **rollup** is a value of the other records that point at a record, such as an order's total of its lines. A **formula** is a value computed from the record's own fields, such as a line's amount. Neither needs SQL: you declare the field, and [`protobase migration`](cli.md#migration) writes its column into the project's next migration (`NNN_computed.sql`). Both are real columns, kept by the database in the transaction that writes, so they are right for writes from psql or other services too, and they filter and sort like any column once they have an index.
|
|
150
150
|
|
|
151
151
|
```ts title="docs-examples/reference/rollups.ts"
|
|
152
152
|
import { f, resource } from '@protobase/schema'
|
|
@@ -198,21 +198,21 @@ Neither field can be written through the API, have a default, or be sensitive. T
|
|
|
198
198
|
|
|
199
199
|
## Sensitive fields
|
|
200
200
|
|
|
201
|
-
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))
|
|
201
|
+
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.
|
|
202
202
|
|
|
203
203
|
```ts
|
|
204
204
|
iban: f.text().sensitive().access({ read: roles.can('screening.read') }),
|
|
205
205
|
```
|
|
206
206
|
|
|
207
|
-
A role may also set a sensitive field it cannot read, such as a sealed value only a worker reads, when the field's own `create` or `update` rule allows it: see [write-only fields](access.md#write-only-fields)
|
|
207
|
+
A role may also set a sensitive field it cannot read, such as a sealed value only a worker reads, when the field's own `create` or `update` rule allows it: see [write-only fields](access.md#write-only-fields).
|
|
208
208
|
|
|
209
209
|
A sensitive field cannot be `.filterable()`, `.sortable()`, aliased, searched, or the key, tenant, owner or soft delete field, and a view may only put it in a record page section: in a list, filter, chart, sidebar, title, recent-record group or action it would always be empty, so the config fails to load.
|
|
210
210
|
|
|
211
|
-
A [webhook subscription](webhooks.md)
|
|
211
|
+
A [webhook subscription](webhooks.md) delivering `as` a role runs that role's access rules on a captured change, which holds no sensitive field. A rule or row filter that reads one there throws, and the change is kept as failed for that subscription instead of delivered, so decide that access on a field that is not sensitive. A rule that reads the field before it checks the role, such as `record.api_secret == null || ctx.user.roles.includes('admin')`, fails even for `as: { roles: ['admin'] }`: check the role first, as in `ctx.user.roles.includes('admin') || ...`.
|
|
212
212
|
|
|
213
213
|
## Encrypted fields
|
|
214
214
|
|
|
215
|
-
An encrypted field is a [sensitive field](#sensitive-fields) whose value the server encrypts before it reaches the database, so a dump, a backup, a replica, a realtime subscriber or anyone running SQL sees only ciphertext. Only its reveal decrypts it, for a caller who may read the record and the field, and every reveal is audited. Every API write encrypts it: the record form, REST, batches, imports, [public forms](forms.md)
|
|
215
|
+
An encrypted field is a [sensitive field](#sensitive-fields) whose value the server encrypts before it reaches the database, so a dump, a backup, a replica, a realtime subscriber or anyone running SQL sees only ciphertext. Only its reveal decrypts it, for a caller who may read the record and the field, and every reveal is audited. Every API write encrypts it: the record form, REST, batches, imports, [public forms](forms.md) and `records` in [API functions](../guides/api-functions.md). The [Supabase-compatible REST API](supabase-rest.md#encrypted-fields) refuses to write or filter it.
|
|
216
216
|
|
|
217
217
|
```ts
|
|
218
218
|
api_key: f.text().encrypted().access({ read: roles.can('keys.read') }),
|
|
@@ -220,7 +220,7 @@ api_key: f.text().encrypted().access({ read: roles.can('keys.read') }),
|
|
|
220
220
|
|
|
221
221
|
It takes `f.text()` only, and cannot be `.readOnly()` or have a `.dbDefault()`. Its column must be `text`, with no length, pattern or unique constraint, index, default, trigger or generated column reading it: the database only ever sees ciphertext.
|
|
222
222
|
|
|
223
|
-
**Write it only through the API.** SQL of your own, in an API function's `db`, a write hook's transaction or the assistant's approved changes, stores the value unencrypted, and its reveal then fails until [`protobase encryption apply`](cli.md#encryption-apply)
|
|
223
|
+
**Write it only through the API.** SQL of your own, in an API function's `db`, a write hook's transaction or the assistant's approved changes, stores the value unencrypted, and its reveal then fails until [`protobase encryption apply`](cli.md#encryption-apply) encrypts it.
|
|
224
224
|
|
|
225
225
|
**Your code never sees the stored value, only whether it is set.** Access rules cannot filter on it: a rule that returns a filter on it fails the request. Record rules, validators and write hooks get a stored record with a set value as `encrypted` (exported from `@protobase/schema`) and an empty one as `null`. Check whether it is set with `== null`; the marker is never equal to anything, so a rule comparing it with a constant decides as if it held another value, and reading it as text throws. A [webhook subscription's access check](#sensitive-fields) cannot know whether it is set, as for every sensitive field. Rules and validators see the plaintext only where a write gives it, so a validator written this way checks each value written and leaves a stored one alone:
|
|
226
226
|
|
|
@@ -254,11 +254,11 @@ A value opens only where it was sealed: its table as `.table()` names it (with o
|
|
|
254
254
|
- Renaming the resource or the field in the config changes nothing.
|
|
255
255
|
- Renaming the table or the column, moving the table to another schema, or adding or changing the schema in `.table()` makes the field's values unreadable until `protobase encryption apply --rebind people --from-table <old table>` (or `--rebind people.iban --from-column <old column>`) runs. Run it at update time, like a migration; reveals fail until then.
|
|
256
256
|
- Changing a resource's declared key or tenant field makes its values unreadable, and no command rebinds them: make such a change before encrypting.
|
|
257
|
-
- The record key must be unique in its table, as [`protobase doctor`](cli.md#doctor)
|
|
257
|
+
- The record key must be unique in its table, as [`protobase doctor`](cli.md#doctor) checks, or at least within each tenant. A key changed in the database, by SQL, a trigger or a foreign key `on update cascade` (which `status` warns about), makes that row's values unreadable; the API refuses to change the key of a record with encrypted fields.
|
|
258
258
|
|
|
259
259
|
### Encrypting an existing column
|
|
260
260
|
|
|
261
|
-
1. With [events](events.md)
|
|
261
|
+
1. With [events](events.md), [history](history.md) or live updates on the resource, [`protobase migration`](cli.md#migration) writes the change of its capture trigger; apply it with your other migrations.
|
|
262
262
|
2. At update time, before the new version serves, run `protobase encryption apply`. It encrypts every plaintext value and removes the field's old values from the history and from the changes not yet dispatched.
|
|
263
263
|
3. With a rolling deploy, the old version writes plaintext until it stops: run `apply` again once the new version serves alone, and gate on `protobase encryption status`, which exits with 1 while anything is left. Until then, reveals of plaintext values fail.
|
|
264
264
|
|
|
@@ -308,13 +308,13 @@ A relation's value in the API is the other record's key. The admin shows the oth
|
|
|
308
308
|
|
|
309
309
|
## Access
|
|
310
310
|
|
|
311
|
-
Without rules everyone who can sign in may do everything. Rules come in three kinds, and the [Access](access.md)
|
|
311
|
+
Without rules everyone who can sign in may do everything. Rules come in three kinds, and the [Access](access.md) reference explains the defaults:
|
|
312
312
|
|
|
313
313
|
- **Per operation** (`list`, `read`, `create`, `update`, `delete`): `.access({ update: rule })`. A rule returns `true`, `false`, or an AIP-160 filter that limits the rows, like row-level security. A rule that reads the record (`record.status`) is checked against the stored row.
|
|
314
314
|
- **Per row**: a filter from a rule, or a `.own` capability with `.owner(...)`.
|
|
315
315
|
- **Per field**: `.access({ read, create, update })` on the field. Field rules see only the user, never a record, so the fields a user can read are the same for every row.
|
|
316
316
|
|
|
317
|
-
[`defineRoles`](https://docs.protobase.net/api/schema/functions/defineroles/)
|
|
317
|
+
[`defineRoles`](https://docs.protobase.net/api/schema/functions/defineroles/) turns roles into capabilities (`work_orders.update.own`, `money.read`, `*`), and `roles.can(...)` and `roles.is(...)` make rules of them; `.and()` and `.or()` combine rules.
|
|
318
318
|
|
|
319
319
|
```ts title="docs-examples/reference/access.ts"
|
|
320
320
|
import { defineRoles, f, resource } from '@protobase/schema'
|
|
@@ -383,4 +383,4 @@ export const orders = resource('orders')
|
|
|
383
383
|
.access(erpAccess('orders'))
|
|
384
384
|
```
|
|
385
385
|
|
|
386
|
-
The UI side of a resource, its names, columns, filters and record page, is in [UI config](ui-config.md)
|
|
386
|
+
The UI side of a resource, its names, columns, filters and record page, is in [UI config](ui-config.md).
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
|
|
5
5
|
The SMTP server and sender an app's mail goes out through, and emails of the app's own, written as React components with typed data and sent from jobs, hooks and API functions.
|
|
6
6
|
|
|
7
|
-
An app sends mail through one SMTP server: Protobase's own mail ([password reset](auth.md#password-reset)
|
|
7
|
+
An app sends mail through one SMTP server: Protobase's own mail ([password reset](auth.md#password-reset), [emailed sign-in codes](auth.md#emailed-sign-in-codes), [invitations](auth.md#organizations), staff sign-in notices and [mentions in comments](comments.md#mentions)) and the emails the app defines itself. Both are React components, rendered to HTML with a plain-text part beside it.
|
|
8
8
|
|
|
9
9
|
## Configuration
|
|
10
10
|
|
|
@@ -12,7 +12,7 @@ An app sends mail through one SMTP server: Protobase's own mail ([password reset
|
|
|
12
12
|
| --- | --- |
|
|
13
13
|
| `PROTOBASE_SMTP_URL` | The SMTP server, credential included: `smtp://user:password@host:587` (STARTTLS when the server offers it) or `smtps://user:password@host:465` (TLS from the start). |
|
|
14
14
|
| `PROTOBASE_MAIL_FROM` | The sender, a plain address such as `noreply@shop.example.com`. The client greets the server with its domain; a `?name=` on the URL overrides that. |
|
|
15
|
-
| `PROTOBASE_APP_NAME` | Optional: the app's name, such as `Shop`, which mail clients show as the sender's name (`Shop <noreply@shop.example.com>`). It overrides [`createAuth`'s `appName`](auth.md#configuration)
|
|
15
|
+
| `PROTOBASE_APP_NAME` | Optional: the app's name, such as `Shop`, which mail clients show as the sender's name (`Shop <noreply@shop.example.com>`). It overrides [`createAuth`'s `appName`](auth.md#configuration). Without either the sender has no name. |
|
|
16
16
|
|
|
17
17
|
Set both or neither. One without the other, a URL that is not `smtp://` or `smtps://`, or a sender that is not a plain address stops the app at startup. Without them mail is off: password reset and emailed codes are not offered, a job's `mail` is `undefined`, and sending an app's email rejects.
|
|
18
18
|
|
|
@@ -26,13 +26,13 @@ PROTOBASE_SMTP_URL=smtp://localhost:1025
|
|
|
26
26
|
PROTOBASE_MAIL_FROM=noreply@shop.example.com
|
|
27
27
|
```
|
|
28
28
|
|
|
29
|
-
A project that delivers mail another way passes a `mailer`, `{ send({ to, subject, text, html, headers }) }`, to [`createAuth`](auth.md#password-reset)
|
|
29
|
+
A project that delivers mail another way passes a `mailer`, `{ send({ to, subject, text, html, headers }) }`, to [`createAuth`](auth.md#password-reset) and `startJobs`, or `mailer: false` to turn mail off whatever the environment says. A template's `send` takes one too, see [Templates](#templates).
|
|
30
30
|
|
|
31
31
|
## Protobase's own mail
|
|
32
32
|
|
|
33
|
-
The auth emails (sign-in, two-factor and sign-up codes, the note to an address that asked to sign up while it has an account, password reset, invitations and staff sign-in notices) and the email to someone a [comment mentions](comments.md#mentions)
|
|
33
|
+
The auth emails (sign-in, two-factor and sign-up codes, the note to an address that asked to sign up while it has an account, password reset, invitations and staff sign-in notices) and the email to someone a [comment mentions](comments.md#mentions) are built with React Email's components: a heading, the message, one code or button, and a footer naming the app.
|
|
34
34
|
|
|
35
|
-
- They name the app by [`appName`](auth.md#configuration)
|
|
35
|
+
- They name the app by [`appName`](auth.md#configuration), and link it on the origin the request came to: the footer links its home page, and the note to an address that has an account links its sign-in page.
|
|
36
36
|
- The plain-text part has each code on a line of its own and each link in full.
|
|
37
37
|
- React Email and the templates are loaded when the first auth email is sent, so an app that sends none never loads them and the server starts as fast without them.
|
|
38
38
|
|
|
@@ -89,7 +89,7 @@ The template that comes back has two methods, both typed by the component's prop
|
|
|
89
89
|
|
|
90
90
|
React and React Email's renderer are loaded on the first email rendered, not when the app starts.
|
|
91
91
|
|
|
92
|
-
Mail an app sends in bulk, such as alerts or newsletters, should carry a one-click unsubscribe link, which Gmail and Yahoo expect of bulk senders ([RFC 8058](https://www.rfc-editor.org/rfc/rfc8058)): a `List-Unsubscribe` header with the recipient's own link, and `List-Unsubscribe-Post`. The link must stop the mail on a `POST` without a sign-in, such as an [API function](../guides/api-functions.md)
|
|
92
|
+
Mail an app sends in bulk, such as alerts or newsletters, should carry a one-click unsubscribe link, which Gmail and Yahoo expect of bulk senders ([RFC 8058](https://www.rfc-editor.org/rfc/rfc8058)): a `List-Unsubscribe` header with the recipient's own link, and `List-Unsubscribe-Post`. The link must stop the mail on a `POST` without a sign-in, such as an [API function](../guides/api-functions.md) that takes a token of the recipient's in its address:
|
|
93
93
|
|
|
94
94
|
```ts
|
|
95
95
|
headers: ({ unsubscribeUrl }: Digest) => ({ 'List-Unsubscribe': `<${unsubscribeUrl}>`, 'List-Unsubscribe-Post': 'List-Unsubscribe=One-Click' }),
|
|
@@ -176,7 +176,7 @@ export default defineConfig({
|
|
|
176
176
|
})
|
|
177
177
|
```
|
|
178
178
|
|
|
179
|
-
An **[API function](../guides/api-functions.md)
|
|
179
|
+
An **[API function](../guides/api-functions.md)** may send directly, as the answer to a request:
|
|
180
180
|
|
|
181
181
|
```ts title="functions/resend-repair-ready.ts"
|
|
182
182
|
import { z } from 'zod'
|
|
@@ -201,4 +201,4 @@ A message is sent once per call, so a job try that sends and then fails sends ag
|
|
|
201
201
|
|
|
202
202
|
## In a bundle
|
|
203
203
|
|
|
204
|
-
A [bundle](cli.md#the-config-module)
|
|
204
|
+
A [bundle](cli.md#the-config-module) gets React and React Email's renderer from the serve runtime, as it gets `@protobase/server` and `@protobase/server/email`, so an email's component and the renderer share one React and hooks such as `useContext` work. React Email's other components are inlined, though `@protobase/server` uses them too: it loads them only to send its own mail, so the runtime does not supply them. Its `<Tailwind>` component needs `tailwindcss`, which the runtime does not supply, so a served config cannot use it: style with `style` props instead.
|
|
@@ -6,7 +6,7 @@ Events a resource declares, and how the database captures every change they are
|
|
|
6
6
|
|
|
7
7
|
A resource can declare **events**: what a change of its records means, such as an order that was created or became large. The changes are captured in the database itself, by triggers, in the transaction that writes them. Writes through the API count, and so do writes from psql, other services, cron SQL and the database's own triggers and generated columns.
|
|
8
8
|
|
|
9
|
-
The app's [jobs](jobs.md)
|
|
9
|
+
The app's [jobs](jobs.md) then turn the captured changes into events and deliver them to [subscriptions](webhooks.md): URLs, the app's API functions or code of its own. A resource with [history](history.md) is captured the same way, and its changes are kept.
|
|
10
10
|
|
|
11
11
|
```ts title="docs-examples/events/orders.ts"
|
|
12
12
|
import { f, resource } from '@protobase/schema'
|
|
@@ -45,7 +45,7 @@ export const orders = resource('orders')
|
|
|
45
45
|
|
|
46
46
|
## Declaring events
|
|
47
47
|
|
|
48
|
-
[`.events((e, r) => [...])`](https://docs.protobase.net/api/schema/interfaces/resourcebuilder/#events)
|
|
48
|
+
[`.events((e, r) => [...])`](https://docs.protobase.net/api/schema/interfaces/resourcebuilder/#events) returns the resource's events, built with the [helpers of `e`](https://docs.protobase.net/api/schema/type-aliases/eventhelpers/). Each is named `<resource>.<name>`, such as `orders.statusChanged`; names are camelCase and unique per resource.
|
|
49
49
|
|
|
50
50
|
| | |
|
|
51
51
|
| --- | --- |
|
|
@@ -65,7 +65,7 @@ The options combine with AND:
|
|
|
65
65
|
| `changed: [r.status]` | One of these fields changed. It works on sensitive fields too. |
|
|
66
66
|
| `match` | A function of the change that returns `true` for the event, for what a filter cannot say. |
|
|
67
67
|
|
|
68
|
-
Filters use the language of [access rules](access.md)
|
|
68
|
+
Filters use the language of [access rules](access.md) and views, on any field, filterable or not. They are checked against the resource when the config loads, so a typo stops startup. A filter cannot use a sensitive field, since captured records never hold their values (use `changed`), nor `search()`, `similar()` or a field of another resource.
|
|
69
69
|
|
|
70
70
|
`match` sees the change as the event does: `operation` (`create`, `update`, `delete` or `undelete`), `before` and `after` (the records by field name, without sensitive fields), `changed` (field names), `actor` (`{ id, roles }` for a write through the API, `null` otherwise), `source` (`api` or `database`), `tenant` and `transaction`.
|
|
71
71
|
|
|
@@ -85,7 +85,7 @@ Filters use the language of [access rules](access.md)) and views, on any field,
|
|
|
85
85
|
- **`changes.capture()`**, the trigger function. It runs as its owner, with a fixed search path, so a role that writes the table needs no rights on `changes`: another service writing as its own role keeps working once a resource has events.
|
|
86
86
|
- **Two triggers per resource with events or history**, on its table: `<table>_changes` for inserts and deletes, and `<table>_changes_update` for updates that change the row.
|
|
87
87
|
|
|
88
|
-
The first row a transaction captures also enqueues a `changes.dispatch` job with `jobs.enqueue()`, in the same transaction, so a resource with events needs the [`jobs` schema](jobs.md#the-jobs-schema)
|
|
88
|
+
The first row a transaction captures also enqueues a `changes.dispatch` job with `jobs.enqueue()`, in the same transaction, so a resource with events needs the [`jobs` schema](jobs.md#the-jobs-schema) too. Dispatch jobs run one at a time, in the app's workers. After a write through the API commits, or an API function returns, the server has its own worker look for that job at once, so its events usually go out within milliseconds; a write from elsewhere waits for the next poll, two seconds at most.
|
|
89
89
|
|
|
90
90
|
A trigger keeps the columns of the resource's fields, minus sensitive ones, whose names it lists in `changed` when they change. Columns that are not fields, such as a password hash, are never captured. One table can have events for one resource.
|
|
91
91
|
|
|
@@ -102,7 +102,7 @@ export default defineConfig({ config, auth, authenticate, events: { schema: 'row
|
|
|
102
102
|
|
|
103
103
|
## Migrations and startup
|
|
104
104
|
|
|
105
|
-
[`protobase migration`](cli.md#migration)
|
|
105
|
+
[`protobase migration`](cli.md#migration) compares the database with the resources' events and writes what it lacks as the project's next migrations: the jobs schema, then the `changes` schema, the function, and a trigger pair per resource with events. Apply it with the project's other migrations. A trigger changes when the resource's fields do, a field that became sensitive for instance, so run the command after changing the config. Once a resource has no events, the next migration drops its triggers; it never drops a table.
|
|
106
106
|
|
|
107
107
|
`protobase dev` and `protobase serve` do not start while a resource with events lacks its triggers, or has out-of-date ones, or the jobs schema is missing, and name the command to run. `dev` checks again whenever it reloads the config. Neither changes the schema itself.
|
|
108
108
|
|
|
@@ -55,7 +55,7 @@ export const bikes = resource('bikes')
|
|
|
55
55
|
| `.variants({ card: { … } }, { original? })` | Resized copies of each image stored beside it, see [Image variants and placeholders](#image-variants-and-placeholders). |
|
|
56
56
|
| `.externalUrls()` | Also takes an https URL of a file elsewhere instead of an upload, see [External URLs](#external-urls). |
|
|
57
57
|
|
|
58
|
-
A file field cannot be filterable, sortable, aliased, searched, sensitive, or the key, tenant, owner or soft delete field. [`protobase doctor`](cli.md#doctor)
|
|
58
|
+
A file field cannot be filterable, sortable, aliased, searched, sensitive, or the key, tenant, owner or soft delete field. [`protobase doctor`](cli.md#doctor) checks that its column is a text type.
|
|
59
59
|
|
|
60
60
|
## What the column holds
|
|
61
61
|
|
|
@@ -159,9 +159,9 @@ Uploads go through Protobase's own route, so the type is checked and processors
|
|
|
159
159
|
1. `POST /api/v1/{resource}:upload?field=photo&name=bike.png` with the file as the body. A `multipart/form-data` body with exactly one file part works too, using that part's name and type; it is read whole, so the raw body is better for large files. The caller needs `create` or `update` on the resource and write access to the field.
|
|
160
160
|
2. The server reads the first bytes, decides the type and refuses a type the field does not allow with `415`, before anything is stored. A `Content-Length` over `maxSize` gets `413` before a byte is read, and a body that turns out larger stops there with `413`.
|
|
161
161
|
3. The rest streams to the provider while it is counted. The answer is `201 { value, file: { name, type, size }, derived, corrected? }`.
|
|
162
|
-
4. Send `value` as the field's value in a create, update or [batch write](api.md#batch-writes)
|
|
162
|
+
4. Send `value` as the field's value in a create, update or [batch write](api.md#batch-writes) within 24 hours. It is a ticket, signed for that user, tenant, resource and field: the write stores the clean URI and the derived values in the same transaction.
|
|
163
163
|
|
|
164
|
-
A write takes only a ticket, `null` to clear the field, or the stored `uri` unchanged, and in a field with `.externalUrls()` an https URL. Anything else is refused with `400`, so a row can only point at a file its writer uploaded for that field, or at a URL elsewhere where the field allows one. From the client, `client.upload(resource, field, file, { onProgress })` does step 1; see [Client](client.md)
|
|
164
|
+
A write takes only a ticket, `null` to clear the field, or the stored `uri` unchanged, and in a field with `.externalUrls()` an https URL. Anything else is refused with `400`, so a row can only point at a file its writer uploaded for that field, or at a URL elsewhere where the field allows one. From the client, `client.upload(resource, field, file, { onProgress })` does step 1; see [Client](client.md).
|
|
165
165
|
|
|
166
166
|
### How the type is decided
|
|
167
167
|
|
|
@@ -218,7 +218,7 @@ photo_placeholder: f.text().readOnly().optional(),
|
|
|
218
218
|
|
|
219
219
|
Variants and placeholders turn the image upright first, following its EXIF orientation, and carry no metadata. JPEG, PNG, WebP, AVIF, GIF and TIFF images are processed; any other file, HEIC and SVG included, is stored as it is, without variants, and its placeholder is `null`. Files uploaded before a field declared them keep none.
|
|
220
220
|
|
|
221
|
-
sharp is a native package, so the config passes it in, and a [built bundle](cli.md#native-packages)
|
|
221
|
+
sharp is a native package, so the config passes it in, and a [built bundle](cli.md#native-packages) carries it with its add-on for linux:
|
|
222
222
|
|
|
223
223
|
```ts
|
|
224
224
|
// protobase.config.ts
|
|
@@ -259,7 +259,7 @@ The admin app draws every image of a file field with `referrerPolicy="no-referre
|
|
|
259
259
|
|
|
260
260
|
- the page's own origin, which serves the app's downloads and the public files without `publicUrl`;
|
|
261
261
|
- the host of each provider's `publicUrl`, such as a bucket's custom domain on another domain than the app;
|
|
262
|
-
- the hosts of the app's [sources](remote-resources.md)
|
|
262
|
+
- the hosts of the app's [sources](remote-resources.md);
|
|
263
263
|
- the hosts `files.trustedHosts` adds, as lowercase host names without a scheme, port or path:
|
|
264
264
|
|
|
265
265
|
```ts
|
|
@@ -282,8 +282,8 @@ Files are deleted by schedule, never in the write itself, so a restore of the da
|
|
|
282
282
|
|
|
283
283
|
A run happens:
|
|
284
284
|
|
|
285
|
-
- in [`protobase serve`](cli.md#serve)
|
|
286
|
-
- with [`protobase files cleanup`](cli.md#files-cleanup)
|
|
285
|
+
- in [`protobase serve`](cli.md#serve), every 60 minutes (`PROTOBASE_FILES_CLEANUP_MINUTES`, 0 turns it off);
|
|
286
|
+
- with [`protobase files cleanup`](cli.md#files-cleanup), from cron or a timer;
|
|
287
287
|
- with `POST /api/files:cleanup` and an admin's token, for a hosting platform. It answers `{ deleted, kept }`.
|
|
288
288
|
|
|
289
289
|
The scheduled deletes sit behind a small interface (`CleanupSchedule`: `add`, `due`, `done`), so a table in a schema of its own, added by a migration at update time, can replace the folder later.
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
|
|
5
5
|
A form that creates a record for people without an account, such as a contact form, at its own address, behind a rate limit and a bot check.
|
|
6
6
|
|
|
7
|
-
A public form creates a record of a resource for anyone, signed in or not: a contact form, a quote request, a sign-up for an event. `.form(...)` on the resource's view declares it, and the [anonymous role](access.md#anonymous-access)
|
|
7
|
+
A public form creates a record of a resource for anyone, signed in or not: a contact form, a quote request, a sign-up for an event. `.form(...)` on the resource's view declares it, and the [anonymous role](access.md#anonymous-access) grants `create` of its fields. Visitors never read the records back, and the form is the only way an anonymous caller creates.
|
|
8
8
|
|
|
9
9
|
```ts title="config/contact_requests.ts"
|
|
10
10
|
import { f, resource, view } from '@protobase/schema'
|
|
@@ -77,15 +77,15 @@ A form takes plain fields: text, numbers, booleans, dates, enums, currencies and
|
|
|
77
77
|
- a field the resource does not have, or two forms of the same name;
|
|
78
78
|
- a field every create must give left out, since no submission could then be saved.
|
|
79
79
|
|
|
80
|
-
An [encrypted field](data-config.md#encrypted-fields)
|
|
80
|
+
An [encrypted field](data-config.md#encrypted-fields) is written blind, masked as the visitor types, and stored encrypted.
|
|
81
81
|
|
|
82
82
|
## The record
|
|
83
83
|
|
|
84
|
-
A submission is a create like any other, made by the anonymous role and limited to the form's fields: the resource's `.validate()` rules, record rules and write hooks run, and [events](events.md)
|
|
84
|
+
A submission is a create like any other, made by the anonymous role and limited to the form's fields: the resource's `.validate()` rules, record rules and write hooks run, and [events](events.md), [webhooks](webhooks.md) and [history](history.md) see it. History names its maker **Visitor**.
|
|
85
85
|
|
|
86
86
|
### Email me on a new submission
|
|
87
87
|
|
|
88
|
-
There is no mail built into forms: the record's `created` event enqueues a [job](jobs.md)
|
|
88
|
+
There is no mail built into forms: the record's `created` event enqueues a [job](jobs.md), and the job [sends the mail](jobs.md#mail).
|
|
89
89
|
|
|
90
90
|
```ts title="config/notify.ts"
|
|
91
91
|
import { defineJob, FatalJobError, subscription } from '@protobase/server'
|
|
@@ -114,7 +114,7 @@ export const newContactRequests = subscription('newContactRequests', {
|
|
|
114
114
|
|
|
115
115
|
## Rate limit
|
|
116
116
|
|
|
117
|
-
Each client may send a form `perMinute` times a minute, 5 by default, as many as it may try to sign in. Past that, the form answers `429` with `Retry-After`. A client is known by its address, as the sign-in limit knows it: see [Client addresses](auth.md#client-addresses)
|
|
117
|
+
Each client may send a form `perMinute` times a minute, 5 by default, as many as it may try to sign in. Past that, the form answers `429` with `Retry-After`. A client is known by its address, as the sign-in limit knows it: see [Client addresses](auth.md#client-addresses), and `PROTOBASE_TRUSTED_PROXIES` behind a proxy. The count is kept in each server process's memory, so with several instances each counts on its own, and a restart starts over.
|
|
118
118
|
|
|
119
119
|
## Bot check
|
|
120
120
|
|
|
@@ -132,4 +132,4 @@ The form's page reads and sends it through three routes beside the API, which ne
|
|
|
132
132
|
| `GET /api/forms/<name>:challenge` | A fresh bot check. |
|
|
133
133
|
| `POST /api/forms/<name>` | `{ values, proof: { challenge, solution } }` creates the record: `204`, or the `400` with field errors a create answers; `400` `bot-check-failed` for a missing, wrong, expired or used check; `429` `too-many-submissions`. |
|
|
134
134
|
|
|
135
|
-
From code, [`@protobase/client`](client.md)
|
|
135
|
+
From code, [`@protobase/client`](client.md) has `form(name)`, `formChallenge(name)` and `submitForm(name, values, proof)`; solve the challenge with `solveChallenge` from `altcha-lib`, as the page does.
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
|
|
5
5
|
Every change of a resource's records kept with who made it, through the API or not, shown on the record page and listed for admins as Activity.
|
|
6
6
|
|
|
7
|
-
A resource with [`.history()`](https://docs.protobase.net/api/schema/interfaces/resourcebuilder/#history)
|
|
7
|
+
A resource with [`.history()`](https://docs.protobase.net/api/schema/interfaces/resourcebuilder/#history) keeps every change of its records: what each field was before and after, who made it and when. The changes are captured by the same database triggers as [events](events.md), so writes from psql, other services and cron SQL are kept too, not only those through the API.
|
|
8
8
|
|
|
9
9
|
```ts title="config/orders.ts"
|
|
10
10
|
import { f, l, resource, view } from '@protobase/schema'
|
|
@@ -43,7 +43,7 @@ export default defineConfig({ config, history: { days: 90 } })
|
|
|
43
43
|
|
|
44
44
|
## What a change holds
|
|
45
45
|
|
|
46
|
-
A change is what an event sees: one per record and transaction, with the record as it was at the start of the transaction and as it is at commit (see [What one change is](events.md#what-one-change-is))
|
|
46
|
+
A change is what an event sees: one per record and transaction, with the record as it was at the start of the transaction and as it is at commit (see [What one change is](events.md#what-one-change-is)).
|
|
47
47
|
|
|
48
48
|
| | |
|
|
49
49
|
| --- | --- |
|
|
@@ -55,15 +55,15 @@ A change is what an event sees: one per record and transaction, with the record
|
|
|
55
55
|
|
|
56
56
|
## On the record page
|
|
57
57
|
|
|
58
|
-
[`l.history(title?, { limit, when })`](https://docs.protobase.net/api/schema/variables/l/#history)
|
|
58
|
+
[`l.history(title?, { limit, when })`](https://docs.protobase.net/api/schema/variables/l/#history) shows the record's changes newest first: who made each, how long ago, and each changed field before and after, formatted and labelled as the view shows the field. `limit` changes show at first (default 10), and Show more loads the next ones. A view without a layout shows History last by itself. A view that names `l.history()` for a resource without `.history()` stops startup.
|
|
59
59
|
|
|
60
|
-
The section reads `GET /api/v1/{resource}/{key}:history` (see [REST API](api.md#record-history))
|
|
60
|
+
The section reads `GET /api/v1/{resource}/{key}:history` (see [REST API](api.md#record-history)). It answers whoever may read the record, with the same tenant, row filter and record-level rule as a get, a soft-deleted record included, and each change carries only the fields that caller may read. An update of only fields they may not read is left out. A change made through the API names its user, with their name when the project signs people in with Better Auth; a change from elsewhere shows as Database with the database role.
|
|
61
61
|
|
|
62
62
|
A save shows up within a moment: the server keeps a change just after its write commits, and the section looks again shortly after each save.
|
|
63
63
|
|
|
64
64
|
## Activity
|
|
65
65
|
|
|
66
|
-
Once a resource has history, admins get **Activity** (the built-in resource `record_history`): every kept change of every resource with history, newest first, filterable by resource, record, operation, user and database role. It lists `before` and `after` whole, so only the `admin` role reads it, whatever the project's roles say, and nobody writes it. When a resource with history has a [tenant](data-config.md#resource)
|
|
66
|
+
Once a resource has history, admins get **Activity** (the built-in resource `record_history`): every kept change of every resource with history, newest first, filterable by resource, record, operation, user and database role. It lists `before` and `after` whole, so only the `admin` role reads it, whatever the project's roles say, and nobody writes it. When a resource with history has a [tenant](data-config.md#resource), Activity is scoped to the caller's tenant, like the app's own resources.
|
|
67
67
|
|
|
68
68
|
## Keeping it
|
|
69
69
|
|
|
@@ -71,7 +71,7 @@ Once a resource has history, admins get **Activity** (the built-in resource `rec
|
|
|
71
71
|
|
|
72
72
|
## The table
|
|
73
73
|
|
|
74
|
-
[`protobase migration`](cli.md#migration)
|
|
74
|
+
[`protobase migration`](cli.md#migration) adds the table `changes.history` to the `changes` schema of the event capture (renamed with `events: { schema }`), and the capture triggers of each resource with history; apply it with the project's other migrations. `protobase dev` and `protobase serve` do not start while the table or a trigger is missing. A resource that drops `.history()` loses its triggers, unless it has events, at the next migration; its changes stay in the table.
|
|
75
75
|
|
|
76
76
|
| Column | |
|
|
77
77
|
| --- | --- |
|
|
@@ -83,4 +83,4 @@ Once a resource has history, admins get **Activity** (the built-in resource `rec
|
|
|
83
83
|
| `database_role` | The role that wrote. |
|
|
84
84
|
| `transaction`, `at` | The writing transaction's id and when it started. |
|
|
85
85
|
|
|
86
|
-
The changes are written by the `changes.dispatch` job, in the transaction that removes them from the outbox, so each is kept exactly once; a resource with history therefore needs the [`jobs` schema](jobs.md#the-jobs-schema)
|
|
86
|
+
The changes are written by the `changes.dispatch` job, in the transaction that removes them from the outbox, so each is kept exactly once; a resource with history therefore needs the [`jobs` schema](jobs.md#the-jobs-schema) and the app's workers. Turning the capture off for a transaction (`set local changes.capture = 'off'`) keeps nothing of it.
|
|
@@ -6,7 +6,7 @@ The admin app installs as an app of its own on phones and computers, named for t
|
|
|
6
6
|
|
|
7
7
|
Every app built with `protobase build` can be installed from the browser, as a Progressive Web App: Chrome and Edge offer **Install** in the address bar, Android's browsers **Add to home screen**, and Safari on iPhone and iPad **Share > Add to Home Screen**. The installed app opens in a window of its own, without the browser's toolbars, at the app's home page.
|
|
8
8
|
|
|
9
|
-
There is nothing to set up. The app installs under its name, `PROTOBASE_APP_NAME` or `appName`, with its favicon, `protobase.favicon.svg`, as its icon, and the colours of `app` in `protobase.config.ts` (see [App identity](app-identity.md))
|
|
9
|
+
There is nothing to set up. The app installs under its name, `PROTOBASE_APP_NAME` or `appName`, with its favicon, `protobase.favicon.svg`, as its icon, and the colours of `app` in `protobase.config.ts` (see [App identity](app-identity.md)). Safari takes no SVG icon: give the project a `protobase.apple-touch-icon.png`, or it shows a picture of the page on the home screen instead.
|
|
10
10
|
|
|
11
11
|
## The manifest
|
|
12
12
|
|