@protobase/cli 0.122.1 → 0.123.1
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/auth/migration-file.js +3 -1
- package/dist/auth/register.js +1 -0
- package/dist/auth/write-migration.js +27 -10
- package/dist/cli/package.js +1 -1
- package/dist/dev/dev-relay.js +1 -1
- package/dist/doctor/startup.js +1 -1
- package/dist/main.js +1 -1
- package/dist/migration/load-project.js +6 -7
- package/dist/migration/migrate-project.js +5 -3
- package/dist/migration/parts.js +5 -8
- package/dist/migration/plan-migrations.js +25 -0
- package/dist/migration/register.js +75 -11
- package/dist/migration/snapshot/auth-difference.js +37 -0
- package/dist/migration/snapshot/auth-target.js +13 -0
- package/dist/migration/snapshot/check.js +26 -0
- package/dist/migration/snapshot/consistency.js +29 -0
- package/dist/migration/snapshot/custom.js +39 -0
- package/dist/migration/snapshot/drop.js +64 -0
- package/dist/migration/snapshot/header.js +34 -0
- package/dist/migration/snapshot/lint-custom.js +23 -0
- package/dist/migration/snapshot/migration-files.js +30 -0
- package/dist/migration/snapshot/next-snapshot.js +40 -0
- package/dist/migration/snapshot/preconditions.js +25 -0
- package/dist/migration/snapshot/run-migration.js +50 -0
- package/dist/migration/snapshot/snapshot-file.js +33 -0
- package/dist/migration/snapshot/up.js +24 -0
- package/dist/migration/snapshot/write-file.js +24 -0
- package/dist/migration/summary.js +6 -6
- package/dist/supabase/dump-phase.js +3 -2
- package/dist/supabase/load-phase.js +5 -0
- package/dist/supabase/project-users.js +4 -3
- package/dist/supabase/storage-copy.js +1 -1
- package/dist/supabase/store-migrations.js +42 -4
- package/dist/users/password.js +1 -1
- package/package.json +9 -8
- package/skills/protobase/references/features/build-and-run.md +2 -1
- package/skills/protobase/references/features/data-model.md +1 -0
- package/skills/protobase/references/features/email-and-public-forms.md +1 -1
- package/skills/protobase/references/features/record-pages.md +1 -1
- package/skills/protobase/references/reference/api.md +1 -1
- package/skills/protobase/references/reference/auth.md +1 -1
- package/skills/protobase/references/reference/cli.md +25 -6
- package/skills/protobase/references/reference/config/api.md +9 -1
- package/skills/protobase/references/reference/custom-components.md +1 -1
- package/skills/protobase/references/reference/data-config.md +36 -0
- package/skills/protobase/references/reference/email.md +4 -3
- package/skills/protobase/references/reference/realtime-relay.md +4 -4
- package/skills/protobase/references/reference/sidebar.md +1 -1
- package/skills/protobase/references/reference/ui-config.md +1 -1
- package/dist/migration/write-migrations.js +0 -32
|
@@ -493,7 +493,7 @@ In an app with the [realtime relay](realtime-relay.md#pages), a chat of your own
|
|
|
493
493
|
| | |
|
|
494
494
|
| --- | --- |
|
|
495
495
|
| `TypingLine` | The line above the box saying who else types on `channel`, by the names `useMembers()` gives; it keeps its height while nobody does, so the box does not move. |
|
|
496
|
-
| `useTypers(channel)` | The user ids of the others who type on the channel now, each until six seconds after the last they sent; empty without the relay or while it is down. |
|
|
496
|
+
| `useTypers(channel)` | The user ids of the others who type on the channel now, outside threads, each until six seconds after the last they sent; empty without the relay or while it is down. |
|
|
497
497
|
| `useTypingSender(channel)` | `onText`, to call with the box's text as it changes, and `onSent`, once a message went: it sends `typing` events at most every three seconds, and that the reader stopped when they empty the box, send or leave. Nothing goes before the channel is joined. |
|
|
498
498
|
| `useViewChannel(channel)` | Says the page shows the channel while it is drawn, so only its notices read the page again, and joins it when the connect ticket did not list it, such as a conversation the reader joined since. |
|
|
499
499
|
|
|
@@ -69,6 +69,7 @@ export const repairs = resource('repairs')
|
|
|
69
69
|
| `.checks({ tasks_parked_by_hand: { label: '{title} waits for a day.' } })` | The database checks whose refusal the API names, each by its constraint name, with the label the app shows. See [Checks the database makes](#checks-the-database-makes). |
|
|
70
70
|
| `.openExisting((r) => [r.organization_id, r.member_key])` | A unique key of the table whose conflict on create opens the record already holding those values, such as a direct message with the same members. See [Opening an existing record](#opening-an-existing-record). |
|
|
71
71
|
| `.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. |
|
|
72
|
+
| `.prepare((values, context) => values)` | Fills in or rewrites a create's or update's values on the server before they are written, inside the write's transaction, such as a generated key or a normalised name. See [Preparing a record](#preparing-a-record). |
|
|
72
73
|
| `.access({ ... })` | Rules per operation. See [Access](#access). |
|
|
73
74
|
| `.events((e, r) => [...])` | What changes of its records mean, such as `orders.created`, captured in the database for every write. See [Events](events.md). |
|
|
74
75
|
| `.explicitGrants()` | Leaves the resource out of role wildcards such as `*.read`: only `*` and capabilities that name it grant it. For resources whose records hold secrets. See [Resources that need named grants](access.md#resources-that-need-named-grants). |
|
|
@@ -506,6 +507,41 @@ What a move leaves behind: webhooks already sent carried the old values to their
|
|
|
506
507
|
|
|
507
508
|
It protects the values from database dumps, backups, replicas, and readers of raw SQL and realtime changes, who see only ciphertext. It does not protect them from whoever holds the key or the app's environment, or can run code in the server. Someone who can write to the database can change a row's tenant or roles, or put back an older value of the same record and field, from a backup say, which still opens. The ciphertext shows whether a value is empty, its length and when it changed. A key seals at most about 2³² values safely, as each gets a random IV; rotating starts a new key's count.
|
|
508
509
|
|
|
510
|
+
## Preparing a record
|
|
511
|
+
|
|
512
|
+
Some values only the server may make, such as a generated key, a random identifier or a name written the one way the table accepts. `.prepare()` fills them in or rewrites them before the record is written, on every create and update: through the REST API, a [batch write](api.md#batch-writes), the [MCP tools](mcp.md), a public form and the assistant alike. It gets the values as the field checks and `.validate()` rules accepted them, and returns the values to write:
|
|
513
|
+
|
|
514
|
+
```ts
|
|
515
|
+
import { randomBytes } from 'node:crypto'
|
|
516
|
+
import { domainToASCII } from 'node:url'
|
|
517
|
+
import { f, FieldIssue, resource } from '@protobase/schema'
|
|
518
|
+
|
|
519
|
+
export const api_tokens = resource('api_tokens')
|
|
520
|
+
.table('api_tokens')
|
|
521
|
+
.fields({
|
|
522
|
+
id: f.integer().readOnly().dbDefault(),
|
|
523
|
+
domain: f.text(),
|
|
524
|
+
// not null, and no request may write it
|
|
525
|
+
token: f.text().readOnly(),
|
|
526
|
+
})
|
|
527
|
+
.primaryKey((r) => r.id)
|
|
528
|
+
.prepare((values, { operation }) => {
|
|
529
|
+
// Bücher.de. is stored as xn--bcher-kva.de, as a check (domain = lower(domain)) wants
|
|
530
|
+
const domain = values.domain === undefined ? {} : { domain: domainToASCII(values.domain.replace(/\.$/, '')) }
|
|
531
|
+
if (domain.domain === '') throw new FieldIssue('domain', 'Not a domain name')
|
|
532
|
+
return operation === 'create' ? { ...values, ...domain, token: randomBytes(24).toString('base64url') } : { ...values, ...domain }
|
|
533
|
+
})
|
|
534
|
+
```
|
|
535
|
+
|
|
536
|
+
- A field it fills stays `.readOnly()`: a request that sends it is refused as read-only, and the hook sets it anyway, so a `not null` column the server makes can be created through the standard `POST`.
|
|
537
|
+
- Its second argument holds the `operation` (`'create'` or `'update'`), the `user`, the `session` and the `tenant` the write works in, `before`, the stored record an update changes, and `trx`, the write's transaction (Kysely): what the hook reads or writes there commits with the record, or not at all.
|
|
538
|
+
- It runs after the field checks and `.validate()` rules, which then check the record it returns again (`400` when one refuses it), and before the access rules decide on the record and the row is written; the [write hooks](config/api.md#writehooks) run after the row is written, in the same transaction.
|
|
539
|
+
- Throwing a `FieldIssue` refuses the write with `422` and `errors: [{ field, code: 'invalid-value', message }]`, the field named when the caller reads or writes it and `_record` otherwise; anything else it throws rolls the write back as a server error.
|
|
540
|
+
- It may not set the tenant column or the `.author()` field, which only the server sets from the caller, nor a name that is not one of the resource's fields: the write then fails as a server error that names it.
|
|
541
|
+
- Each call adds a hook, run in order, each with the values the one before returned.
|
|
542
|
+
- The [PostgREST-compatible API](supabase-rest.md) writes the tables under their own grants and policies rather than through the resources, so the hook does not run there: a table it writes takes such a value from a default or a trigger.
|
|
543
|
+
- It does database and CPU work only, such as generating a key pair. While it waits on anything else, such as an HTTP call, the transaction stays open; work that needs the network belongs in a [job](jobs.md) a write hook enqueues.
|
|
544
|
+
|
|
509
545
|
## Opening an existing record
|
|
510
546
|
|
|
511
547
|
A resource whose records are unique by some of their values, such as a direct message by its members, can have a create that repeats those values open the record that holds them instead of failing. `.openExisting()` names the unique key, as its fields:
|
|
@@ -317,7 +317,7 @@ await mail.send({ to: member.email, subject: 'News', text: '...', bulk: true, to
|
|
|
317
317
|
Amazon SES (through SNS) and Resend report to their own endpoints so far, beside the `protobase` one a platform reports to.
|
|
318
318
|
:::
|
|
319
319
|
|
|
320
|
-
A mail provider tells the app when a message bounced or its recipient marked it as spam. With `mailFeedback` on, each report becomes a `mail_feedback` record, one per recipient, with the events `mail_feedback.bounced` and `mail_feedback.complained`, which [workflows](workflows.md), [subscriptions](webhooks.md) and your own code act on, such as marking a contact's address undeliverable. Protobase records and notifies only: it keeps sending to the address, and the app decides what a bounce means.
|
|
320
|
+
A mail provider tells the app when a message bounced or its recipient marked it as spam. With `mailFeedback` on, each report becomes a `mail_feedback` record, one per recipient, or one for a complaint that hides who complained, with the events `mail_feedback.bounced` and `mail_feedback.complained`, which [workflows](workflows.md), [subscriptions](webhooks.md) and your own code act on, such as marking a contact's address undeliverable. Protobase records and notifies only: it keeps sending to the address, and the app decides what a bounce means.
|
|
321
321
|
|
|
322
322
|
Turn it on in the config, then run `protobase migration`, which writes its table, `mail.feedback`, and the capture of its events as migrations of the project's own; apply them with the project's migrations, as `protobase serve` refuses to start while the table is missing:
|
|
323
323
|
|
|
@@ -336,10 +336,11 @@ Each provider reports to `POST /api/mail/feedback/<provider>` with the default [
|
|
|
336
336
|
A value that is not a topic ARN or a signing secret stops startup, naming the variable.
|
|
337
337
|
|
|
338
338
|
- **Signatures:** a report writes nothing unless its signature checks out. `ses` takes SNS's signature, versions 1 and 2, with the signing certificate fetched from an SNS host over HTTPS and kept an hour, and only messages of the named topics, since anyone can sign with a topic of their own. `resend` and `protobase` take [Standard Webhooks](https://www.standardwebhooks.com) signatures, made at most five minutes before. A missing or wrong signature answers `401`, another topic `403`.
|
|
339
|
-
- **The record:** `type` is `bounce` or `complaint`; `bounce_type` `hard` (permanent) or `soft` (transient), and empty when the provider could not tell; `recipient`, lower-cased
|
|
339
|
+
- **The record:** `type` is `bounce` or `complaint`; `bounce_type` `hard` (permanent) or `soft` (transient), and empty when the provider could not tell; `recipient`, lower-cased, which a complaint leaves empty when the report hides who complained, as some mailbox providers' spam reports do, and then names its `message_id`; `message_id`, the `Message-ID` header of the message it is about, when the report states it; `provider_message_id`, the provider's own id of that message; `reason`, as the provider gives it, such as the receiving server's diagnostic, a bounce subtype or a complaint's feedback type; `occurred_at`, when the provider says it happened; `provider`, `event_id`, the provider's id of the report, and `created_at`. A value the report does not state is empty, never guessed. Match it to the mail it answers by the [message id](#message-id) you stored.
|
|
340
340
|
- **Once:** a provider sends a report again when it got no answer in time; the same report writes nothing new. A report of another kind, such as a delivery, answers `204` and writes nothing.
|
|
341
341
|
- **Who sees it:** admins see the reports under **Mail feedback** in the admin app, and the app's code reads them as the `mail_feedback` resource, `GET /api/v1/mail_feedback?filter=recipient = "ada@example.com"`, filtered by `recipient`, `type`, `message_id` or `provider_message_id`. Nobody changes them but the endpoints.
|
|
342
|
-
- **The `protobase` endpoint:** its body is one record as above, without `provider`, `event_id` and `created_at`, such as `{ "type": "bounce", "bounce_type": "hard", "recipient": "ada@example.com", "message_id": "<...>", "reason": "smtp; 550 5.1.1 user unknown" }`, signed as Standard Webhooks with a key derived from `PROTOBASE_SECRET`, so the secret itself never travels: `mailFeedbackHeaders(secret, id, body)` of `@protobase/server` makes the headers. `webhook-id` names the report, and a retry keeps it.
|
|
342
|
+
- **The `protobase` endpoint:** its body is one record as above, without `provider`, `event_id` and `created_at`, such as `{ "type": "bounce", "bounce_type": "hard", "recipient": "ada@example.com", "message_id": "<...>", "reason": "smtp; 550 5.1.1 user unknown" }`, a bounce with its `recipient` and a complaint with its `recipient`, its `message_id` or both, signed as Standard Webhooks with a key derived from `PROTOBASE_SECRET`, so the secret itself never travels: `mailFeedbackHeaders(secret, id, body)` of `@protobase/server` makes the headers. `webhook-id` names the report, and a retry keeps it.
|
|
343
|
+
- <gap>The generated type of a `mail_feedback` record cannot say that `message_id` is set when `recipient` is empty because field types that depend on another field's value are not implemented.</gap>
|
|
343
344
|
- <gap>Other mail providers cannot report bounces and complaints because only the `ses`, `resend` and `protobase` endpoints are implemented.</gap>
|
|
344
345
|
|
|
345
346
|
## Attachments
|
|
@@ -118,25 +118,25 @@ A person who shares no channel is published to no one. <gap>A page cannot show w
|
|
|
118
118
|
With `realtime.relay`, the admin app follows the relay for someone signed in, on one socket per page, and `/meta` names the resources whose changes the relay notices:
|
|
119
119
|
|
|
120
120
|
- **Live updates:** the lists, records and counts of those resources are read again as the relay notices their changes, new, edited and deleted records alike, instead of through [`/api/live`](live.md), which stays for the other `.live()` resources. While a [conversation page](ui-config.md#conversation-pages) is open, only its own channel's notices read it again; the notices of other channels only move the [unread marks](sidebar.md#typing-and-unread-from-the-relay). After the socket was down, everything shown is read again once.
|
|
121
|
-
- **Typing:** a conversation page with `typing` says who else types, above its box, and tells the others while the reader types, as `typing` events on the record's channel. A sidebar group with [`typing`](sidebar.md#typing-and-unread-from-the-relay) says "typing…" beside a record someone types in, wherever the reader is.
|
|
121
|
+
- **Typing:** a conversation page with `typing` says who else types, above its box and in a thread's box, and tells the others while the reader types, as `typing` events on the record's relay channel. Without the relay, it goes over the record's own [channel](channels.md) instead. A sidebar group with [`typing`](sidebar.md#typing-and-unread-from-the-relay) says "typing…" beside a record someone types in, wherever the reader is.
|
|
122
122
|
- **Unread:** a sidebar group with [`unread`](sidebar.md#typing-and-unread-from-the-relay) counts each conversation's unread messages once, then adds the relay's notices without reading.
|
|
123
123
|
- **Presence:** the page tells the relay the person is away while it is hidden and online once it shows; a page that shows again, or a device back online, connects again at once.
|
|
124
124
|
|
|
125
|
-
The relay refuses events the app did not declare, so declare typing in the relay's apps file, on each channel resource with typing:
|
|
125
|
+
The relay refuses events the app did not declare, so declare typing in the relay's apps file, on each channel resource with typing. A page sends `{ typing, thread }`, `thread` the key of the thread's first message or null, so the size leaves room for a key:
|
|
126
126
|
|
|
127
127
|
```json
|
|
128
128
|
"client_events": {
|
|
129
129
|
"channels": {
|
|
130
130
|
"conversations": {
|
|
131
131
|
"events": {
|
|
132
|
-
"typing": { "schema": { "type": "object", "properties": { "typing": { "type": "boolean" } }, "required": ["typing"] }, "max_bytes":
|
|
132
|
+
"typing": { "schema": { "type": "object", "properties": { "typing": { "type": "boolean" } }, "required": ["typing"] }, "max_bytes": 128, "per_second": 2, "burst": 4 }
|
|
133
133
|
}
|
|
134
134
|
}
|
|
135
135
|
}
|
|
136
136
|
}
|
|
137
137
|
```
|
|
138
138
|
|
|
139
|
-
|
|
139
|
+
With the relay, a conversation page's `typing` needs its resource among `channels`: the server refuses to start otherwise.
|
|
140
140
|
|
|
141
141
|
<gap>A page does not hear of a channel the person joined after it connected, such as a new direct message, until it opens that conversation or connects again, because telling a person's sockets of a new channel is not implemented.</gap> <gap>An unread count also counts the reader's own messages sent from another device or tab, until they open the conversation, because notices do not name who made the change.</gap>
|
|
142
142
|
|
|
@@ -155,7 +155,7 @@ view<typeof conversations>('conversations')
|
|
|
155
155
|
.record({ conversation: { resource: 'messages', field: 'conversation_id', sender: 'author_id', time: 'sent_at', text: 'body', readPosition: { resource: 'read_positions', field: 'conversation_id', message: 'last_read_id' }, typing: true } })
|
|
156
156
|
```
|
|
157
157
|
|
|
158
|
-
- `typing` says "typing…" after a record's title while someone else types in it, wherever the reader is in the app, for six seconds after the last they heard, and not while the relay is down. The open conversation's row leaves it out: the line above its box says who types.
|
|
158
|
+
- `typing` says "typing…" after a record's title while someone else types in it, wherever the reader is in the app, for six seconds after the last they heard, and not while the relay is down. The open conversation's row leaves it out: the line above its box says who types. <gap>A sidebar group's `typing` cannot say "typing…" in an app without the realtime relay because following every listed record's own channel is not implemented.</gap>
|
|
159
159
|
- `unread` draws a record bold, with the count of the messages others wrote after the reader's read position. It is counted as the group shows, in two requests, the reader's read positions and one aggregate of the messages grouped by conversation in the database, and again after the relay reconnects. In between, each new message the relay notices on a conversation adds one without reading anything; the open conversation counts none, and opening one clears its count.
|
|
160
160
|
|
|
161
161
|
Both mark the group's own records over a `marks` hook's, so a direct message's row can still name its person.
|
|
@@ -846,7 +846,7 @@ view<typeof channels>('channels').record({
|
|
|
846
846
|
| `thread` | Replies in a thread under a message, in a panel beside the conversation: `{ field, count, last }`. |
|
|
847
847
|
| `attachments` | [Files](#files-in-messages) a message carries, kept in a resource of their own: `{ resource, field, file, width, height }`. |
|
|
848
848
|
| `call` | The record's [call](calls.md), by the name of the [API function](../guides/api-functions.md) that admits people to it. |
|
|
849
|
-
| `typing` | `true`: a line above the box says who else types, by their names among the organization's members, and the others hear while the reader types, through the app's [realtime relay](realtime-relay.md#pages)
|
|
849
|
+
| `typing` | `true`: a line above the box says who else types, by their names among the organization's members, and the others hear while the reader types, each shown until six seconds after the last they sent or until they send or empty the box; a thread's panel says who types in that thread alone. It works with or without a relay: through the app's [realtime relay](realtime-relay.md#pages) when it has one, whose channel resources then include the record's resource, and otherwise over the record's own [channel](channels.md), which only those who may read the record join. |
|
|
850
850
|
|
|
851
851
|
The header shows the record's title, which the top bar repeats at mobile width and so shows there only with `members`, the subtitle, the call with `call`, and *Details*, a link to the record's standard page at `?standard=true`, with its sections, Edit and Delete. The latest 50 messages show oldest first, with *Load earlier messages* at the top for more. A day divider opens each day, and one person's messages sent within five minutes of each other show under their name once. A message resource with [`.live()`](live.md) shows new, changed and deleted messages as they happen. Every read goes through the reader's own access: a message they may not read never shows, and a reader who may not read the messages, a field each one needs or the fields of `thread` gets the standard page. The box shows for a reader who may create messages.
|
|
852
852
|
|
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
import { nextMigrationName } from "../auth/migration-file.js";
|
|
2
|
-
import path from "node:path";
|
|
3
|
-
import { mkdir, readdir, writeFile } from "node:fs/promises";
|
|
4
|
-
//#region src/migration/write-migrations.ts
|
|
5
|
-
/** A part's file: what wrote it and why, then its statements. */
|
|
6
|
-
const partText = (title, sql) => `-- ${title}, written by \`protobase migration\`.\n-- Applied with the project's other migrations; edit or remove statements the project handles differently.\n\n${sql}\n`;
|
|
7
|
-
/**
|
|
8
|
-
* Writes what each part still needs as the next migrations in `dir`, one file per part, and returns their paths; none
|
|
9
|
-
* when the database is current. Every part is compared with the database before any file is written, so the
|
|
10
|
-
* project's earlier migrations must be applied to it first.
|
|
11
|
-
*/
|
|
12
|
-
const writeMigrations = async (parts, dir) => {
|
|
13
|
-
const pending = [];
|
|
14
|
-
for (const part of parts) {
|
|
15
|
-
const sql = await part.sql();
|
|
16
|
-
if (sql !== void 0) pending.push({
|
|
17
|
-
part,
|
|
18
|
-
sql
|
|
19
|
-
});
|
|
20
|
-
}
|
|
21
|
-
if (pending.length === 0) return [];
|
|
22
|
-
await mkdir(dir, { recursive: true });
|
|
23
|
-
const files = [];
|
|
24
|
-
for (const { part, sql } of pending) {
|
|
25
|
-
const file = path.join(dir, nextMigrationName(await readdir(dir), part.name));
|
|
26
|
-
await writeFile(file, partText(part.title, sql), { flag: "wx" });
|
|
27
|
-
files.push(file);
|
|
28
|
-
}
|
|
29
|
-
return files;
|
|
30
|
-
};
|
|
31
|
-
//#endregion
|
|
32
|
-
export { partText, writeMigrations };
|