@protobase/cli 0.129.0 → 0.130.0
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/config-organizations.js +15 -0
- package/dist/auth/register.js +8 -2
- package/dist/auth/write-migration.js +34 -12
- package/dist/cli/package.js +1 -1
- package/dist/config/build-project.js +1 -0
- package/dist/config/project-modules.js +64 -0
- package/dist/config/read-only-queries.js +9 -0
- package/dist/config/user-tables-config.js +1 -1
- package/dist/dev/dev-jobs.js +12 -6
- package/dist/dev/entry.js +12 -19
- package/dist/doctor/capture-files.js +3 -3
- package/dist/doctor/startup.js +2 -4
- package/dist/migration/apply-migrations.js +9 -6
- package/dist/migration/baseline/read-live-modules.js +31 -0
- package/dist/migration/baseline/read-live-store.js +50 -0
- package/dist/migration/baseline/write-baseline.js +80 -0
- package/dist/migration/migrate-project.js +3 -3
- package/dist/migration/migration-project.js +29 -15
- package/dist/migration/older-code/catalog.js +34 -0
- package/dist/migration/older-code/contractions.js +86 -0
- package/dist/migration/older-code/lint-files.js +55 -0
- package/dist/migration/older-code/refusal.js +9 -0
- package/dist/migration/older-code/trial.js +58 -0
- package/dist/migration/parts.js +8 -104
- package/dist/migration/record/after-file.js +20 -2
- package/dist/migration/register.js +56 -15
- package/dist/migration/release/release-record.js +9 -7
- package/dist/migration/release/release-steps.js +15 -3
- package/dist/migration/snapshot/auth-difference.js +111 -12
- package/dist/migration/snapshot/auth-target.js +9 -3
- package/dist/migration/snapshot/check.js +26 -9
- package/dist/migration/snapshot/custom.js +6 -3
- package/dist/migration/snapshot/drop.js +5 -1
- package/dist/migration/snapshot/header.js +11 -4
- package/dist/migration/snapshot/module-changes.js +22 -0
- package/dist/migration/snapshot/module-prompts.js +28 -0
- package/dist/migration/snapshot/module-sections.js +38 -0
- package/dist/migration/snapshot/next-snapshot.js +24 -8
- package/dist/migration/snapshot/off-choice.js +11 -0
- package/dist/migration/snapshot/organizations-off-facts.js +49 -0
- package/dist/migration/snapshot/revival-rows.js +34 -0
- package/dist/migration/snapshot/run-migration.js +82 -16
- package/dist/migration/snapshot/snapshot-file.js +7 -0
- package/dist/migration/snapshot/terminal-prompts.js +21 -0
- package/dist/migration/snapshot/write-file.js +1 -1
- package/dist/migration/summary.js +3 -2
- package/dist/organizations/api.js +4 -2
- package/dist/organizations/commands.js +27 -10
- package/dist/organizations/pin-source.js +20 -0
- package/dist/organizations/pin.js +40 -0
- package/dist/organizations/register.js +20 -12
- package/dist/scaffold/protobase-tables.js +2 -2
- package/dist/serve/create-admin.js +6 -3
- package/dist/serve/lifecycle.js +2 -2
- package/dist/serve/migrate-release.js +1 -1
- package/dist/serve/relay-check.js +7 -8
- package/dist/serve/startup-checks.js +12 -22
- package/dist/supabase/load-phase.js +3 -1
- package/dist/supabase/project-users.js +4 -1
- package/dist/users/commands.js +44 -6
- package/dist/users/load-auth.js +9 -3
- package/dist/users/manage.js +16 -1
- package/dist/users/register.js +28 -25
- package/dist/users/set-disabled.js +3 -2
- package/dist/users/users-api.js +9 -3
- package/dist/worker/start-worker.js +7 -4
- package/package.json +8 -8
- package/skills/protobase/references/features/api-functions.md +1 -0
- package/skills/protobase/references/features/build-and-run.md +3 -2
- package/skills/protobase/references/features/sign-in-and-accounts.md +2 -1
- package/skills/protobase/references/guides/api-functions.md +27 -2
- package/skills/protobase/references/guides/deploy.md +2 -2
- package/skills/protobase/references/reference/assistant.md +1 -1
- package/skills/protobase/references/reference/auth.md +52 -17
- package/skills/protobase/references/reference/calls.md +1 -1
- package/skills/protobase/references/reference/cli.md +41 -27
- package/skills/protobase/references/reference/comments.md +1 -1
- package/skills/protobase/references/reference/config/api.md +1 -1
- package/skills/protobase/references/reference/config/jobs-and-events.md +1 -1
- package/skills/protobase/references/reference/config/sign-in.md +22 -4
- package/skills/protobase/references/reference/config.md +1 -1
- package/skills/protobase/references/reference/data-config.md +9 -9
- package/skills/protobase/references/reference/derived.md +1 -1
- package/skills/protobase/references/reference/email.md +2 -2
- package/skills/protobase/references/reference/events.md +2 -2
- package/skills/protobase/references/reference/history.md +1 -1
- package/skills/protobase/references/reference/jobs.md +5 -5
- package/skills/protobase/references/reference/live.md +2 -2
- package/skills/protobase/references/reference/notifications.md +4 -4
- package/skills/protobase/references/reference/personal-access-tokens.md +2 -2
- package/skills/protobase/references/reference/realtime-relay.md +1 -1
- package/skills/protobase/references/reference/status.md +2 -2
- package/skills/protobase/references/reference/supabase-rest.md +1 -1
- package/skills/protobase/references/reference/user-tables.md +1 -1
- package/skills/protobase/references/reference/webhooks.md +1 -1
- package/dist/migration/snapshot/lint-custom.js +0 -23
|
@@ -26,31 +26,37 @@ Users, sessions and signing keys stay apart from the application's tables, in a
|
|
|
26
26
|
|
|
27
27
|
### Migrations
|
|
28
28
|
|
|
29
|
-
The store's tables come from steps that ship with Protobase, and are part of the project's migrations, committed in the repository like the application's own: the examples carry them as `db/migrations/*_auth.sql`, and their `db:migrate` applies both. Each module of sign-in has its own numbered chain of steps, the only source of its tables: `auth` (users, sessions, accounts, codes and signing keys), `two_factor`, `passkeys`, `sign_in_policy`, `staff_sign_in`, `organizations`, `mcp`, `personal_access_tokens`, `mail_senders` and `platform_devices`. A step is written against the store's [names](#table-and-column-names), so it makes the tables and columns under the names the app configures, and a step that adds columns to another module's table, such as `organizations` adding the organization of each user, session and personal access token, adds them only while that module is on. The store records
|
|
29
|
+
The store's tables come from steps that ship with Protobase, and are part of the project's migrations, committed in the repository like the application's own: the examples carry them as `db/migrations/*_auth.sql`, and their `db:migrate` applies both. Each module of sign-in has its own numbered chain of steps, the only source of its tables: `auth` (users, sessions, accounts, codes and signing keys), `two_factor`, `passkeys`, `sign_in_policy`, `staff_sign_in`, `organizations`, `mcp`, `personal_access_tokens`, `mail_senders` and `platform_devices`. A step is written against the store's [names](#table-and-column-names), so it makes the tables and columns under the names the app configures, and a step that adds columns to another module's table, such as `organizations` adding the organization of each user, session and personal access token, adds them only while that module is on. The store records each step it applied in its `auth_steps` table, one row per module, step and the module whose tables it changes, so the columns one module adds to another's tables are recorded apart from the step's own tables. A store an earlier Protobase made recorded versions instead, in `auth_migrations`, the numbers of the single chain the store had before its modules had chains of their own: the next file makes `auth_steps` from them, each version as the steps that shared it, such as version 13 as `auth` 4, `passkeys` 2, `staff_sign_in` 2 and `organizations` 2, and `auth_migrations` is read no more. When an upgrade of Protobase brings new steps, write them as the project's next migration:
|
|
30
30
|
|
|
31
31
|
```sh
|
|
32
32
|
bun run db:migrate # the database at the project's current schema
|
|
33
|
-
bun run protobase auth migration # reads
|
|
33
|
+
bun run protobase auth migration # reads auth_steps; writes the steps not applied as db/migrations/NNN_auth.sql
|
|
34
34
|
bun run db:migrate # applies them
|
|
35
35
|
```
|
|
36
36
|
|
|
37
|
-
Each step's statements come under a comment naming its module, number and name, such as `-- auth 2 account_keys: ...`, and
|
|
37
|
+
Each step's statements come under a comment naming its module, number and name, such as `-- auth 2 account_keys: ...`, and each is followed by its row in `auth_steps`. The file only adds tables, nullable columns, columns with a default, and indexes, and the `pg_trgm` extension that the index of the users' names needs, never drops, retypes or tightens anything, so it is safe to apply unattended; Protobase's tests lint every step it ships for this. A step that assumes something of the rows already there starts with a check that refuses before it changes anything: `auth` step 2, which makes one account per provider and account id, stops when two accounts share both, naming them, so you keep one of each in a migration of your own written before it, then apply again. The file is plain SQL to review, edit or leave out where the project handles the auth schema itself. `--dir` and `--name` choose the folder and the name after the number. [`protobase migration`](cli.md#migration) writes the same steps from the project's `protobase.snapshot.json` instead of the database, so it needs no database, along with what the config's other features need, such as [jobs](jobs.md). The file `auth migration` writes starts with Protobase's lines and brings the store to every step of this version, so it also starts the project's snapshot, or moves it on, and `protobase migration` goes on from there.
|
|
38
38
|
|
|
39
|
-
|
|
39
|
+
A store has the tables of the modules the config turns on, and no table, column or index of the others. A module is on while its feature is: `organizations` with [`auth.organizations`](#organizations), `mcp` with [`auth.mcp`](#oauth-for-mcp-clients) or `openIdProvider`, which share its tables, `personal_access_tokens` with [`auth.personalAccessTokens`](personal-access-tokens.md), and `mail_senders` with [`auth.personalMail`](config/sign-in.md#authpersonalmail), and `platform_devices` with [device sign-in](#device-accounts): a provider of `auth.signInProviders` with `devices: true`, or [`auth.signInDevices`](config/sign-in.md#authsignindevices) for the platform's providers from the environment, as a migration is written from the config alone. `auth`, `two_factor`, `passkeys`, `sign_in_policy` and `staff_sign_in` are always on: those features have no switch. Turning a feature on is a migration: the next `protobase migration` or `protobase auth migration` writes its module's steps, and the statements the modules already on add to its tables, and the server refuses to start with the feature on until that file is applied. The columns a module adds to another's table, such as `user.last_organization_id`, are made with that module. SAML providers and open sign-up keep nothing in the store.
|
|
40
|
+
|
|
41
|
+
#### Turning a module off
|
|
42
|
+
|
|
43
|
+
Taking a feature out of the config turns its module off, and the next [`protobase migration`](cli.md#migration) asks what becomes of its tables and rows; without a terminal, `--off keep` answers. The answer is recorded in `protobase.snapshot.json`, under `decisions.modules.kept`, with the step the module had and its tables' names. `keep`, the default, applies at once: the tables and rows stay as they are, the module stays at its step, and each column it adds to the tables of the modules still on loses `not null`, so the code that no longer writes it still inserts rows, and is watched for the code that still does, such as the version before once rolled back: a trigger on each counts the rows written with it set, by hour, in `retired.writes` (`table_name`, `column_name`, `hour`, `writes`), the schema `retired` made by the first file that retires a column. Turning the module on again stops the watch, and the column is in use again under its name with the values it kept. A personal access token's `organization_id` stays required, as tokens made while organizations are off are made in [the default organization](#the-default-organization). For personal access tokens the question says they stay valid unless deleted. <gap>A module turned off cannot have its tables archived or deleted because waiting for an update to count as successful, by its manual sign-off, is not implemented.</gap> `--off archive` and `--off delete` are refused, naming keep. `protobase auth migration` refuses a config that turns a module off or on again since the snapshot, and says to run `protobase migration`.
|
|
44
|
+
|
|
45
|
+
Turning a kept module on again writes the rest of its steps after the one it was kept at, and refuses a table or column name the config changed meanwhile, as for a module that stays on; and again what the modules on add to its tables, which their guard skips where the database has it. Its rows come back into use, such as personal access tokens, which act for their users again, so the file starts with a check that refuses while its tables hold rows, naming each table with its count, unless it is applied with [`protobase migrate --force`](cli.md#migrate). With `DATABASE_URL` set, `protobase migration` counts them first and refuses the same way, asking on a terminal whether to bring them back, or taking `--force`; with empty tables it neither asks nor refuses.
|
|
40
46
|
|
|
41
47
|
The names the app configures, the store's schema ([`auth.schema`](config/sign-in.md#authschema)), its [tables and columns](#table-and-column-names), and uuid ids (`generateUserId: 'uuid'`), are the ones in effect when the file is written. Changing one later is a migration of the project's own, such as a rename. <gap>`protobase auth migration` cannot rename a table or column the store has already because writing a rename from a changed name is not implemented.</gap>
|
|
42
48
|
|
|
43
|
-
The server never changes the schema, and never compares it with what Better Auth needs. `protobase serve` and `protobase dev` read the database's [record of migrations](../guides/deploy.md#the-record-of-migrations), `migrations.history`, at startup and refuse to start while it lacks a step of this Protobase, naming the module and step, or while the database is ahead of this Protobase by a migration that is not additive. Each migration records itself there; `
|
|
49
|
+
The server never changes the schema, and never compares it with what Better Auth needs. `protobase serve` and `protobase dev` read the database's [record of migrations](../guides/deploy.md#the-record-of-migrations), `migrations.history`, at startup and refuse to start while it lacks a step of this Protobase, naming the module and step, or while the database is ahead of this Protobase by a migration that is not additive. Each migration records itself there; `auth_steps` stays the steps' own record, which each step's file reads to skip a step the database has. The examples' `db:migrate` applies all pending files in one transaction, so a migration that fails leaves the database as the running version expects it. The host must guarantee the rest: nothing but the migrations writes while they run, and the new version serves only after they succeeded. Only [`protobase dev`](cli.md#dev), on the developer's machine, compares the store's tables with what the app's Better Auth needs, and warns at startup about each table, column or index that drifted, such as one changed by hand.
|
|
44
50
|
|
|
45
51
|
No resource may be over a table of the store, which holds password hashes, sessions, tokens and signing keys: `protobase serve` and `protobase dev` refuse to start with one, naming it, whatever the table is called, and [`protobase scaffold`](cli.md#scaffold) never writes one. The API, the Supabase-compatible endpoints, live updates and MCP read the app's resources only, so none of them reads the store.
|
|
46
52
|
|
|
47
|
-
A store made before 0.48, when `protobase auth migration` compared the database with Better Auth's tables, has no
|
|
53
|
+
A store made before 0.48, when `protobase auth migration` compared the database with Better Auth's tables, has no record of its steps. For it, `protobase auth migration` writes the step that adopts it, once: it runs every step in a scratch schema, in a transaction it rolls back, to find the tables and columns each version makes, records the steps of each version whose tables and columns the store has, with their types, as applied, and writes the steps of the versions whose tables it has none of, such as MCP's for an app that never turned MCP on. The file checks again, when it is applied, that the store has every table and column it adopts, and stops otherwise, naming them. A store that has only some of a version's tables and columns, or has them with other types, is refused, naming what differs: bring it to the schema of Protobase 0.47 first, with that version's `protobase auth migration`. A store set up by Protobase 0.6, with Better Auth's camelCase names, or by `auth:migrate` before it, goes through 0.47 the same way.
|
|
48
54
|
|
|
49
55
|
### Table and column names
|
|
50
56
|
|
|
51
57
|
Each module of the store declares its tables and columns once, under built-in names, snake_case like the app's (`sign_in_policy.platform_sign_in`, `session.user_id`); the code and the API keep Better Auth's camelCase names. The modules and their tables:
|
|
52
58
|
|
|
53
|
-
- `auth`: `user`, `session`, `account`, `verification`, `jwks` and `auth_migrations`.
|
|
59
|
+
- `auth`: `user`, `session`, `account`, `verification`, `jwks`, `auth_steps` and `auth_migrations`.
|
|
54
60
|
- `two_factor`: `two_factor`, and the user's `two_factor_enabled`.
|
|
55
61
|
- `passkeys`: `passkey`.
|
|
56
62
|
- `sign_in_policy`: `sign_in_policy`.
|
|
@@ -136,7 +142,7 @@ The store's migrations then make nothing of the user table, which a migration of
|
|
|
136
142
|
|
|
137
143
|
### Hooks
|
|
138
144
|
|
|
139
|
-
Sign-in runs the project's own code from `auth/hooks.ts`, which Protobase finds by that path and loads when it builds sign-in. Each hook is a named export, typed with `AuthHooks` from `@protobase/server`, and gets the app's database, `{ db }` (Kysely), as its last argument
|
|
145
|
+
Sign-in runs the project's own code from `auth/hooks.ts`, which Protobase finds by that path and loads when it builds sign-in. Each hook is a named export, typed with `AuthHooks` from `@protobase/server`, and gets the app's database, `{ db }` (Kysely), as its last argument, which keeps the [organization's tables](../guides/api-functions.md#the-organizations-tables) to the organization the hook is about, and to none for a hook about none, such as `onUserCreated`:
|
|
140
146
|
|
|
141
147
|
```ts
|
|
142
148
|
// auth/hooks.ts
|
|
@@ -154,9 +160,9 @@ export const generateUserId: AuthHooks['generateUserId'] = async ({ db }) => {
|
|
|
154
160
|
| `generateUserId` | the id of a new user, by any route; see [User ids](#roles-and-access-in-the-api) |
|
|
155
161
|
| `onUserCreated` | `(user, context, { db })`, after a user is created by any route: `protobase users create`, an admin, [open sign-up](#open-sign-up) or a [sign-in provider](#sign-in-with-github) |
|
|
156
162
|
| `generateOrganizationId` | the id of a new [organization](#organizations); default a random UUID |
|
|
157
|
-
| `onOrganizationCreated` | `({ organization, member }, { db })`, after an organization is created, with its owner's membership |
|
|
158
|
-
| `beforeOrganizationDeleted` | `(organization, { db })`, before an organization is deleted; throwing refuses it |
|
|
159
|
-
| `organizationHooks` | Better Auth's organization hooks by name, each with `{ db }` last, run after Protobase's own |
|
|
163
|
+
| `onOrganizationCreated` | `({ organization, member }, { db })`, after an organization is created, with its owner's membership; `db` works in the new organization |
|
|
164
|
+
| `beforeOrganizationDeleted` | `(organization, { db })`, before an organization is deleted; throwing refuses it; `db` works in that organization |
|
|
165
|
+
| `organizationHooks` | Better Auth's organization hooks by name, each with `{ db }` last, run after Protobase's own; `db` works in the organization the hook's input names |
|
|
160
166
|
| `senderVerifiers` | `(add_verifier)`, once as the app starts: adds the app's ways to prove a From address; see [Verifying a From address](email.md#verifying-a-from-address) |
|
|
161
167
|
|
|
162
168
|
A mailer of the project's own, for sign-in's mail, is the default export of `auth/mailer.ts` (see [Password reset](#password-reset)). An authenticator of its own, in place of the one `authenticate` describes, is the default export of `auth/authenticate.ts` (see [Tokens and roles](#tokens-and-roles)).
|
|
@@ -668,6 +674,14 @@ PROTOBASE_SIGN_IN_PHONE_TOKENS=true
|
|
|
668
674
|
|
|
669
675
|
A provider that gives each device a subject of its own, such as one that signs in a phone app, can have every device be an account of its own: `PROTOBASE_SIGN_IN_DEVICES=true` (`devices: true`), with [token sign-in](#token-sign-in) alone, without a client secret.
|
|
670
676
|
|
|
677
|
+
Device sign-in keeps its own tables, `refused_account` and `device_session`, which a migration makes. A provider of `auth.signInProviders` with `devices: true` turns them on. For the platform's providers from the environment, turn on `auth.signInDevices` in the config, then write the migration with `protobase migration` and apply it:
|
|
678
|
+
|
|
679
|
+
```ts
|
|
680
|
+
auth: { signInDevices: true }
|
|
681
|
+
```
|
|
682
|
+
|
|
683
|
+
Without it, a provider of the environment with `PROTOBASE_SIGN_IN_DEVICES=true`, or `PROTOBASE_SIGN_IN_<ID>_DEVICES=true`, stops the server at startup with an error naming `auth.signInDevices`.
|
|
684
|
+
|
|
671
685
|
- **Linked by address:** a device's first sign-in links it to the account with the address the provider vouches for, as for any provider, both addresses verified. A person holds as many of the provider's accounts as they have devices.
|
|
672
686
|
- **Its sessions:** each session a device's token sign-in starts is kept as the device's in the store's `device_session` table (the `platform_devices` module's step 1), which goes with the session, so it lasts as long as the session however often it is renewed. A sign-in that waits for the [second step](#two-factor-authentication) is the device's once the step is taken.
|
|
673
687
|
- **On the account page:** the **Devices** tab lists each device as the provider's name with the device's own name, which the device's ID token carries in the `device_name` claim (a string), with when it was linked, and the sessions it began under it rather than among the others. The connected accounts do not list them. `GET /api/auth/account/device-links` answers `{ links: [{ id, provider, name, device, linkedAt, sessions }] }`, `sessions` being the tokens of the sessions it began; `GET /api/auth/status` says `deviceLinks: true`.
|
|
@@ -750,11 +764,11 @@ auth: {
|
|
|
750
764
|
sales: 'Sales',
|
|
751
765
|
accountant: 'Accountant',
|
|
752
766
|
},
|
|
753
|
-
organizations: { create: 'everyone' },
|
|
767
|
+
organizations: { enabled: true, default: { id: 'main', name: 'Main', slug: 'main' }, existingUsersRole: 'sales', create: 'everyone' },
|
|
754
768
|
}
|
|
755
769
|
```
|
|
756
770
|
|
|
757
|
-
It is Better Auth's organization plugin, with its tables named in snake_case (`organization`, `member`, `invitation`, `session.active_organization_id`)
|
|
771
|
+
It is Better Auth's organization plugin, with its tables named in snake_case (`organization`, `member`, `invitation`, `session.active_organization_id`, `user.last_organization_id`), which the store has while organizations are on, and keeps once they are turned off (see [migrations](#migrations)).
|
|
758
772
|
|
|
759
773
|
**Two kinds of role.** Every membership has an organization role, Better Auth's `owner`, `admin` or `member`, which decides who manages the organization: owners and admins invite, remove and change members and edit its details; only owners hand it over, delete it or make owners. App roles, the project's `roles`, decide what the app shows and allows. A user holds them **globally** (the admin plugin's `role` column, as without organizations), which applies in every organization, or **per membership** (`member.app_roles`), which applies there only. A request's role, `ctx.user.roles`, is the session's [active role](#active-role), of either kind; access rules see the organization role as `ctx.organization.role`, read from the membership on each request, absent for someone who is no member there, such as a superuser who entered it. `admin` is the superuser: global only, it holds every role. With organizations every role needs a label; `owner` and `member` cannot be app role names, and `user` stands for an account without a global role.
|
|
760
774
|
|
|
@@ -770,21 +784,42 @@ It is Better Auth's organization plugin, with its tables named in snake_case (`o
|
|
|
770
784
|
|
|
771
785
|
**Ids.** The `generateOrganizationId` [hook](#hooks) makes a new organization's id, by default a UUID; `.tenant` columns hold it, so `text` or `uuid`. An app whose tenant columns are integers takes the next value of its own organizations sequence instead, and adds its own row in `onOrganizationCreated`, as the ERP example does (`tests/apps/erp/auth/hooks.ts`). An app that ran as one organization keeps its tenant value: `protobase organizations create "Acme" --owner you@example.com --id 1 --members all` makes that organization, with every user in it: superusers as owners, everyone else as a member, their global roles moved onto the membership. The first user `createUser` makes also becomes the owner of every organization without members, such as those a seed made before anyone could own them.
|
|
772
786
|
|
|
773
|
-
**On the host.** The [`protobase organizations`](cli.md#organizations) commands call these functions of `@protobase/server`, which a script of the project's own, such as a seed, calls with the sign-in of [`loadProjectAuth`](#first-run). The host acts as a superuser, so it may give any role; `organization` is a slug or an id:
|
|
787
|
+
**On the host.** The [`protobase organizations`](cli.md#organizations) commands call these functions of `@protobase/server`, which a script of the project's own, such as a seed, calls with the sign-in of [`loadProjectAuth`](#first-run). The host acts as a superuser, so it may give any role; `organization` is a slug or an id. `scope` is the organization a call works in, by its id, or every organization with `everyOrganization('<why>')` from `@protobase/query`, whose reason says why the call reads every organization's rows:
|
|
774
788
|
|
|
775
789
|
```ts
|
|
776
|
-
import { addAppRoleToOwners, addMember, createOrganization, listOrganizations, removeMember, setMemberRoles } from '@protobase/server'
|
|
790
|
+
import { addAppRoleToOwners, addMember, createOrganization, isMemberByEmail, listOrganizations, listUsers, organizationByKey, removeMember, setMemberRoles } from '@protobase/server'
|
|
777
791
|
|
|
778
792
|
await createOrganization(auth, { name, slug, owner: email, id? }) // the owner holds every app role
|
|
779
793
|
await addMember(auth, { organization, email, role?, appRoles? }) // role: owner, admin or member (default)
|
|
780
794
|
await setMemberRoles(auth, { organization, email, role?, appRoles? })
|
|
781
795
|
await removeMember(auth, { organization, email }) // refuses the last owner
|
|
782
|
-
await listOrganizations(auth, { after?, size? })
|
|
783
|
-
await addAppRoleToOwners(auth, appRole)
|
|
796
|
+
await listOrganizations(auth, scope, { after?, size? }) // a page, oldest first: { organizations: [{ id, name, slug, members }], next? }
|
|
797
|
+
await addAppRoleToOwners(auth, scope, appRole) // a role the project added later, to the owners in scope
|
|
798
|
+
await listUsers(auth, { scope?, after?, size? }) // the members of scope; every user without it
|
|
799
|
+
await organizationByKey(auth, organization) // the organization, or null
|
|
800
|
+
await isMemberByEmail(auth, organizationId, email)
|
|
784
801
|
```
|
|
785
802
|
|
|
786
803
|
The app's own pages are under the profile menu: the switcher, "Manage organizations" (theirs, a new one, and for global roles any one), "Organization" (details, leaving, handing over, deleting), and for owners and admins "Members" and "Invitations". The client has them as `session.organizations` (`list`, `current`, `switchTo`, `members`, `invite`, ...). `session.organizations.create({ name })` makes one from its name alone: the server makes the slug from the name, and when another organization has that slug, adds a dash and six random letters and digits, such as `acme-k3x9q2`. The organization page changes the slug afterwards.
|
|
787
804
|
|
|
805
|
+
### The default organization
|
|
806
|
+
|
|
807
|
+
Every table of the store is either the whole app's or an organization's. `member`, `invitation` and `personal_access_token` hold an organization's rows, each with its `organization_id`; the others, users, sessions, accounts, verifications, two-factor secrets, passkeys, the sign-in policy and the signing keys among them, are the app's. Each index of an organization's table serves one organization, leading with `organization_id`, or the whole app, such as a lookup by a person or an address; a key looked up from outside, such as a token's hash, stays unique across the app. The `db` the app's functions, jobs and hooks get shows them one organization's rows only, unless `db.unscoped('<reason>')` (see [The organization's tables](../guides/api-functions.md#the-organizations-tables)). Sign-in's own queries keep to one organization too, work Better Auth finishes after the response included: an `/api/auth/organization/*` request works in the organization it names as `organizationId`, in a GET's query or a POST's body, else in the session's active one, and a POST naming another in its query is refused with `400` (`CONFLICTING_ORGANIZATIONS`); a membership or token is read in the organization it belongs to; and what is a person's own in every organization, such as their organizations, the invitations to their address, their personal access tokens and a token looked up by its hash, is read across them. A request elsewhere in `/api/auth` works in none. <gap>An `/api/auth/organization/*` request that names its organization by slug alone cannot reach one other than the session's active organization because finding the organization of a slug before the request runs is not implemented.</gap> While organizations are off after being on, the `db` keeps the tables they left to [the default organization](#the-default-organization): a function's for a session of it, a job's enqueued in it, and a hook's, which is about it; a public function and `/api/sb/functions/v1` see none of their rows.
|
|
808
|
+
|
|
809
|
+
The default organization, `organizations.default`, is the one that stands in for every organization while they are off. [`protobase migration`](cli.md#migration) refuses without it once organizations are on or were ever on, and records it in `protobase.snapshot.json`. Turning organizations off sets `enabled: false` and keeps `default`; the store keeps their tables and columns, as [for any module](#turning-a-module-off).
|
|
810
|
+
|
|
811
|
+
Turning organizations on for the first time makes the default organization under its id, and each user there already becomes its member, with the organization role `member` and the app role `existingUsersRole`; `protobase migration` refuses without that role, unless the store is new and holds no users. Each personal access token made before then works in the default organization: the migration's `.after.sql` fills them in batches, then makes their `organization_id` required, checked first without a long lock, and builds their index per organization concurrently. Turning organizations on again later fills nothing: a token made while they were off was made in the default organization. One made while they were off on an earlier version has no organization, so the migration refuses, naming it, as below. On a store whose organizations were on before, such as one [`migration baseline`](cli.md#migration) started, the default organization is made when the store lacks it, no one becomes its member, and a token without an organization is not given the default one, which would guess where it works: the migration refuses, naming each such token, so you delete them, or set the organization each works in, in a migration of your own written before it. With organizations on, a personal access token is made in the session's active organization, and refused without one.
|
|
812
|
+
|
|
813
|
+
When organizations are off while the store still holds organizations, as after they were on, the default organization stands in for all of them, and only its members have access:
|
|
814
|
+
|
|
815
|
+
- At startup, the server refuses to start unless `authenticate.tenant` is the default organization's id, and its error message shows both values. A store with no organization, such as one whose organization tables were made while organizations were off, starts as before.
|
|
816
|
+
- A non-member who signs in gets the same answer as for a wrong password, and no session is created. This applies to every sign-in method, including staff sign-in and impersonation.
|
|
817
|
+
- Anyone added while organizations are off, by open sign-up, an admin or a sign-in provider, becomes a member of the default organization. They get the organization role `member` and the app role set in `existingUsersRole`, or no app role if that is not set.
|
|
818
|
+
- A non-member's session, API token or OAuth access token is refused with `401`. A non-member's session gets no token from `/api/auth/token` and no personal access token, and an admin who is not a member cannot manage people, the sign-in policy or registered clients, so they cannot add someone to the default organization. <gap>A non-member's session from before organizations were turned off still answers `/api/auth/get-session` because refusing it in sign-in's own session read is not implemented.</gap>
|
|
819
|
+
- New personal access tokens are created in the default organization, and only tokens created there work. A token created in another organization, or in none, is refused like an unknown token.
|
|
820
|
+
|
|
821
|
+
When `DATABASE_URL` is set, the question `protobase migration` asks as organizations are turned off says how many people are not members of the default organization. It also says how many personal access tokens were created outside it. If the store's only organization is not the default, `protobase migration` offers to make it the default. If you answer yes, it prints the `default` entry for your config and stops without writing anything. Add the entry and run it again.
|
|
822
|
+
|
|
788
823
|
### First organization and start address
|
|
789
824
|
|
|
790
825
|
:::caution[Beta]
|
|
@@ -31,7 +31,7 @@ Or, with no media server, `backend: { kind: 'peer' }`: see [Peer to peer](#peer-
|
|
|
31
31
|
- `maxMinutes`: how long a call lasts at most, 240 by default. Then it ends, whoever is still in it.
|
|
32
32
|
- `processes`: `'many'` when several processes serve the app behind one address, so members that different processes serve meet in one call; `'one'`, the default, otherwise.
|
|
33
33
|
|
|
34
|
-
Without `calls`, an app has none of it: the server loads no calls code, and only a page that imports `@protobase/client/calls` bundles the browser side. With `calls`, [`protobase migration`](cli.md#migration) adds the `calls` schema, where calls and their members are kept; apply it with the project's other migrations. `protobase dev` and `protobase serve` do not start while
|
|
34
|
+
Without `calls`, an app has none of it: the server loads no calls code, and only a page that imports `@protobase/client/calls` bundles the browser side. With `calls`, [`protobase migration`](cli.md#migration) adds the `calls` schema, where calls and their members are kept, and the notices table rings reach open pages through; apply it with the project's other migrations. `protobase dev` and `protobase serve` do not start while the [record of migrations](../guides/deploy.md#the-record-of-migrations) lacks them.
|
|
35
35
|
|
|
36
36
|
Running LiveKit on your own machine: `bun run calls:up` in the Protobase repository starts one from `docker-compose.yml`, at `ws://127.0.0.1:7880` with the key `devkey` and the secret `secretsecretsecretsecretsecretsecret`.
|
|
37
37
|
|
|
@@ -51,7 +51,7 @@ protobase scaffold [connection string | --env VAR] [target] [options]
|
|
|
51
51
|
|
|
52
52
|
Without a connection string or `--env`, the URL is `DATABASE_URL`, from the environment or the nearest `.env` at or above the current directory, as `dev` reads it. `target` is `all` (default), `schema`, `schema.table` or `schema.table.column`. Tables are introspected from `pg_catalog`; `config/<resource>/data.ts` is created or extended. A table that a written one points to with a foreign key is written too, outside the target or excluded, unless it has a resource already, since the app refuses a relation to a resource it does not have; a line names each.
|
|
53
53
|
|
|
54
|
-
Protobase's own tables are left out, since they hold password hashes, OAuth tokens and signing keys: the auth store, which is the `auth` schema or another schema with the store's `auth_migrations` table, or with Better Auth's `session`, `account` and `verification` tables whose sessions point at a user table (in `public`, only the tables of the store's [migrations](auth.md#migrations) and that user table, whatever it is called), pg-boss's `jobs` schema, and the `migrations` schema whose `applied` table records the migrations applied. A line names each one skipped. The auth store is left out whatever the target names; a target naming the `jobs` or `migrations` schema scaffolds it anyway. A resource an earlier run made over one of them gets a warning to remove it, and `dev` and `serve` refuse to start with a resource over the auth store.
|
|
54
|
+
Protobase's own tables are left out, since they hold password hashes, OAuth tokens and signing keys: the auth store, which is the `auth` schema or another schema with the store's `auth_steps` or `auth_migrations` table, or with Better Auth's `session`, `account` and `verification` tables whose sessions point at a user table (in `public`, only the tables of the store's [migrations](auth.md#migrations) and that user table, whatever it is called), pg-boss's `jobs` schema, and the `migrations` schema whose `applied` table records the migrations applied. A line names each one skipped. The auth store is left out whatever the target names; a target naming the `jobs` or `migrations` schema scaffolds it anyway. A resource an earlier run made over one of them gets a warning to remove it, and `dev` and `serve` refuse to start with a resource over the auth store.
|
|
55
55
|
|
|
56
56
|
| Option | Effect |
|
|
57
57
|
| --- | --- |
|
|
@@ -145,11 +145,11 @@ Without a connection string, `--env` or `DATABASE_URL`, doctor stops and names `
|
|
|
145
145
|
| Check | Without a database | With one |
|
|
146
146
|
| --- | --- | --- |
|
|
147
147
|
| Relations to a resource the config does not declare, sorts by a field that is not `.sortable()`, page components without reads | yes | yes |
|
|
148
|
-
| The triggers, `capture()`, outbox, history and notices tables of resources with events, history or live updates | from `db/migrations/*.sql`, for each resource whose table a migration creates | from the
|
|
148
|
+
| The triggers, `capture()`, outbox, history and notices tables of resources with events, history or live updates | from `db/migrations/*.sql`, for each resource whose table a migration creates | from the record of migrations |
|
|
149
149
|
| The server's build of the app, such as an owner field that must hold sign-in's user ids | yes | yes |
|
|
150
150
|
| Tables, columns, types and keys; indexes for the reads; [user tables](user-tables.md); the other startup checks of the database | skipped | yes |
|
|
151
151
|
|
|
152
|
-
From the migrations, a resource passes when they create its table, `capture()` in the changes schema and the outbox, the history table when it keeps history and the notices table when it is live, and a trigger of each name the
|
|
152
|
+
From the migrations, a resource passes when they create its table, `capture()` in the changes schema and the outbox, the history table when it keeps history and the notices table when it is live, and a trigger of each name the change capture writes on its table; a missing one fails, naming the resource. Statements are matched by name in the files, in any order, so a trigger a later migration drops or one created in dynamic SQL is judged only with a database. A resource whose table no migration creates, as in an app on a database of its own, is left to the database. <gap>Doctor cannot judge the jobs, comments, webhooks, workflows, notifications, status, rollup and other feature schemas without a database because reading their versions from the migration files is not implemented.</gap> Doctor ends with the checks it skipped:
|
|
153
153
|
|
|
154
154
|
```
|
|
155
155
|
Skipped without a database:
|
|
@@ -460,11 +460,11 @@ bun --no-install protobase-serve.js migrate-release <bundle> --plan # print wh
|
|
|
460
460
|
bun --no-install protobase-serve.js migrate-release <bundle> # apply it
|
|
461
461
|
```
|
|
462
462
|
|
|
463
|
-
It loads the bundle's config with the app's settings, as `create-admin` does, and compares every feature the config turns on with the database itself, as [`migration`](#migration) does with `DATABASE_URL` set: the sign-in tables by the
|
|
463
|
+
It loads the bundle's config with the app's settings, as `create-admin` does, and compares every feature the config turns on with the database itself, as [`migration`](#migration) does with `DATABASE_URL` set: the sign-in tables by the steps their `auth_steps` table records, the jobs schema by the step pg-boss's version is at and the version Protobase's functions carry, the change capture, workflows, notifications and the other built-in schemas by what the database has. It applies what differs in one transaction, then the row that records the release in `migrations.history`, with or without sign-in, which the server reads at startup, behind the lock `migrate` takes, stopping at once while another run holds it, prints each part it applies, such as `applying auth`, or that the database is up to date, so a second run applies nothing. A step whose check refuses the rows already there, such as two From addresses one person entered with different case, stops it with the step's message and exit code 1, and nothing is applied. It refuses a database that lacks any of the bundle's own migrations, since the steps are compared with what they made.
|
|
464
464
|
|
|
465
465
|
`--plan` applies nothing and prints the same statements as one SQL script, each part under a comment, ending with `-- N steps to apply; nothing was applied`. The script records the sign-in versions it applies itself, so a host can run it in a transaction of its own, such as one it rolls back to try it first, or one it commits while nothing else writes.
|
|
466
466
|
|
|
467
|
-
A project that gets the same steps later from its own [`migration`](#migration) applies them without doing any twice: each sign-in step in its files runs only while `
|
|
467
|
+
A project that gets the same steps later from its own [`migration`](#migration) applies them without doing any twice: each sign-in step in its files runs only while `auth_steps` lacks it, and the other parts are compared with its database.
|
|
468
468
|
|
|
469
469
|
## `serve`
|
|
470
470
|
|
|
@@ -617,12 +617,12 @@ Prints a fresh key pair for [Web Push](notifications.md#web-push) as `PROTOBASE_
|
|
|
617
617
|
|
|
618
618
|
```sh
|
|
619
619
|
protobase users create <email> [--name <name>] [--role <role>] [--password-stdin | --generate-password | --no-password] [--github-id <id>]
|
|
620
|
-
[--organization <organization> [--org-role <role>] [--app-roles <a,b>]
|
|
621
|
-
protobase users list
|
|
620
|
+
[--organization <organization> | --all-organizations] [--org-role <role>] [--app-roles <a,b>]
|
|
621
|
+
protobase users list [--organization <organization> | --all-organizations]
|
|
622
622
|
protobase users roles
|
|
623
|
-
protobase users set-role <email> <role>
|
|
624
|
-
protobase users disable <email> # and: enable <email>
|
|
625
|
-
protobase users delete <email> [--yes]
|
|
623
|
+
protobase users set-role <email> <role> [--organization <organization> | --all-organizations]
|
|
624
|
+
protobase users disable <email> [--organization <organization> | --all-organizations] # and: enable <email>
|
|
625
|
+
protobase users delete <email> [--yes] [--organization <organization> | --all-organizations]
|
|
626
626
|
```
|
|
627
627
|
|
|
628
628
|
Run inside a project with sign-in on (its config's `auth`, on unless `false`, or unless the config leaves `auth` out and sets `authenticate.jwksUrl`). Like `organizations`, `token` and `auth migration`, it reads the `.env` nearest the project and needs `DATABASE_URL`. There is no web setup page, and [open sign-up](auth.md#open-sign-up), when a project turns it on, waits for the first admin: the first admin is created here. The first user is always an admin, whatever `--role` says; later users get `--role`, or the project's default role (`auth: { roles, defaultRole }` in its config). `protobase users roles` lists the roles of the project; anything else is refused with that list. `set-role` also takes comma separated roles (`sales,accountant`). `--github-id` links the new user to a GitHub user id (the number, not the login), so signing in with GitHub, the app's own provider or the [platform's](auth.md#platform-sign-in-providers), finds them whatever address GitHub reports; an id that signs in another user already is refused. `createUser(auth, { ..., accounts: [{ providerId, accountId }] })` does the same from code.
|
|
@@ -631,51 +631,65 @@ The password is typed at a hidden prompt with confirmation, read from stdin with
|
|
|
631
631
|
|
|
632
632
|
`delete` asks you to type the email back (or pass `--yes`), and needs a terminal otherwise. `delete`, `set-role` and `disable` refuse to touch the last admin, so the project cannot lock itself out. `disable` bans the user and revokes their sessions; tokens already issued stop working on their next request, and `protobase token` refuses disabled users. With [MCP](auth.md#oauth-for-mcp-clients) on, it also disconnects their MCP clients. `enable` lifts the ban; the clients have to be allowed again. `protobase dev` prints the command that creates the first admin at startup while the user table is empty (see [`dev`](#dev)).
|
|
633
633
|
|
|
634
|
-
With [organizations](auth.md#organizations), `--role` is the user's global role (none by default) and
|
|
634
|
+
With [organizations](auth.md#organizations), `--role` is the user's global role (none by default), and each command acts on [the pinned organization](#the-pinned-organization): `create` adds the new user to it with `--org-role` (`owner`, `admin`, or `member`, the default) and `--app-roles` there, or to no organization with `--all-organizations`; the first user owns it already when it had no members. `list` lists its members, or every user with `--all-organizations`. `set-role`, `disable`, `enable` and `delete` reach only its members, or any user with `--all-organizations`; they change the whole account, in every organization the user belongs to.
|
|
635
635
|
|
|
636
636
|
## `organizations`
|
|
637
637
|
|
|
638
638
|
```sh
|
|
639
|
-
protobase organizations list
|
|
639
|
+
protobase organizations list [--organization <organization> | --all-organizations]
|
|
640
640
|
protobase organizations create <name> --owner <email> [--slug <slug>] [--id <id>] [--members all]
|
|
641
|
-
protobase organizations add-member <
|
|
642
|
-
protobase organizations set-roles <
|
|
643
|
-
protobase organizations remove-member <
|
|
644
|
-
protobase organizations add-app-role <role> --to owners
|
|
641
|
+
protobase organizations add-member <email> [--organization <organization>] [--org-role <role>] [--app-roles <a,b>]
|
|
642
|
+
protobase organizations set-roles <email> [--organization <organization>] [--org-role <role>] [--app-roles <a,b>]
|
|
643
|
+
protobase organizations remove-member <email> [--organization <organization>]
|
|
644
|
+
protobase organizations add-app-role <role> --to owners [--organization <organization> | --all-organizations]
|
|
645
645
|
```
|
|
646
646
|
|
|
647
|
-
For a project with [organizations](auth.md#organizations)
|
|
647
|
+
For a project with [organizations](auth.md#organizations). The host acts as a superuser, so it may give any role. Every command but `create` acts on [the pinned organization](#the-pinned-organization): `list` lists it with its members, or every organization with `--all-organizations`; `add-member`, `set-roles` and `remove-member` change a membership in it, and refuse `--all-organizations`; `add-app-role` gives a role the project added later to its owners, or to every organization's owners with `--all-organizations`. `create` makes `--owner` the owner with every app role; `--id` keeps an existing tenant value, and `--members all` adds every other user, superusers as owners and everyone else as a member with their global roles moved onto the membership, for an app that ran as one organization. `remove-member` and `set-roles` refuse to leave an organization without an owner.
|
|
648
|
+
|
|
649
|
+
### The pinned organization
|
|
650
|
+
|
|
651
|
+
The `users` and `organizations` commands act on one organization: the config's [`auth.organizations.default`](auth.md#the-default-organization), or the one `--organization` names by its slug or id. They print it first, or `Every organization` with `--all-organizations`, which reaches every organization's people. An organization the database does not have is refused, naming it, and so is a project without `auth.organizations.default` unless the command gets `--organization` or `--all-organizations`.
|
|
652
|
+
|
|
653
|
+
The pin is a filter, not a boundary: whoever runs a host command holds the database's credentials, so it keeps a command from reaching another organization's people by accident, not on purpose.
|
|
648
654
|
|
|
649
655
|
## `migrate`
|
|
650
656
|
|
|
651
657
|
```sh
|
|
652
|
-
protobase migrate [connection string | --env VAR] [--plan]
|
|
658
|
+
protobase migrate [connection string | --env VAR] [--plan] [--force]
|
|
653
659
|
```
|
|
654
660
|
|
|
655
|
-
Applies the project's `db/migrations/*.sql` that the database has not applied yet, in file name order, as the runtime's [`migrate`](#build-serve) applies a bundle's: behind the same lock, which it only tries, stopping at once while another run holds it, recorded in the same `migrations.applied` and, by each file's own last statement, in `migrations.history` (see [The record of migrations](../guides/deploy.md#the-record-of-migrations)), so a database either one migrated is current for the other. The files apply in batches, each in one transaction, cut after a file with an `.after.sql`, whose statements run outside a transaction before the next batch; a file a stopped run left unfinished is finished first. It prints each file it applies and each `.after.sql` it runs, or that the database is up to date; a project without `db/migrations` applies nothing and says so. It runs none of the project's code, so a project with only `protobase.config.ts`, no `package.json` and nothing installed, migrates as well. The URL comes as for [`scaffold`](#scaffold), and is needed only when there are migrations. A project that still has a `db/migrate.ts` recording in `public.schema_migrations` first copies in the presets' current `db/migrate.ts` and `drop.ts`, so the two keep one record. `--plan` is a dry run: it prints each `.after.sql` left unfinished, then each file not applied yet under its name, with its SQL and its `.after.sql`, from a read-only transaction, and applies and records nothing. <gap>`migrate` cannot report where the tables it touched differ from the record's model, nor refuse a Postgres older than the supported one, because comparing them after applying, and `migration check --database`, are not implemented.</gap>
|
|
661
|
+
Applies the project's `db/migrations/*.sql` that the database has not applied yet, in file name order, as the runtime's [`migrate`](#build-serve) applies a bundle's: behind the same lock, which it only tries, stopping at once while another run holds it, recorded in the same `migrations.applied` and, by each file's own last statement, in `migrations.history` (see [The record of migrations](../guides/deploy.md#the-record-of-migrations)), so a database either one migrated is current for the other. The files apply in batches, each in one transaction, cut after a file with an `.after.sql`, whose statements run outside a transaction before the next batch; a file a stopped run left unfinished is finished first. It prints each file it applies and each `.after.sql` it runs, or that the database is up to date; a project without `db/migrations` applies nothing and says so. It runs none of the project's code, so a project with only `protobase.config.ts`, no `package.json` and nothing installed, migrates as well. The URL comes as for [`scaffold`](#scaffold), and is needed only when there are migrations. A project that still has a `db/migrate.ts` recording in `public.schema_migrations` first copies in the presets' current `db/migrate.ts` and `drop.ts`, so the two keep one record. `--plan` is a dry run: it prints each `.after.sql` left unfinished, then each file not applied yet under its name, with its SQL and its `.after.sql`, from a read-only transaction, and applies and records nothing. `--force` lets a file that turns a [kept sign-in module](auth.md#turning-a-module-off) on again apply while its tables hold rows, which come back into use; without it the file stops, naming each table with its count. <gap>`migrate` cannot report where the tables it touched differ from the record's model, nor refuse a Postgres older than the supported one, because comparing them after applying, and `migration check --database`, are not implemented.</gap>
|
|
656
662
|
|
|
657
663
|
## `migration`
|
|
658
664
|
|
|
659
665
|
:::caution[Beta]
|
|
660
|
-
A project with auth migrations written before the snapshot starts it with
|
|
666
|
+
A project with auth migrations written before the snapshot starts it with `migration baseline`, against its database with those migrations applied; `migration` and `check` refuse such a project until then.
|
|
661
667
|
:::
|
|
662
668
|
|
|
663
669
|
```sh
|
|
664
|
-
protobase migration [--dir db/migrations] [--extend <dir>]
|
|
670
|
+
protobase migration [--dir db/migrations] [--extend <dir>] [--off keep] [--force]
|
|
665
671
|
protobase migration --custom <name> [--record-config] [--after]
|
|
666
672
|
protobase migration check
|
|
673
|
+
protobase migration baseline [--dir db/migrations] [--extend <dir>]
|
|
667
674
|
protobase migration drop
|
|
668
675
|
protobase migration up
|
|
669
676
|
```
|
|
670
677
|
|
|
671
|
-
Writes what the config and this version of Protobase need beyond the project's `protobase.snapshot.json` as the project's next migration in `--dir`: one numbered file each run, named after what it holds, such as `004_auth.sql` or `005_changes_webhooks.sql`, after the folder's highest number, and rewrites the snapshot to end at it, so both show in the pull request. The file starts with lines Protobase reads back: `-- protobase:id`, a random id of its own, then the version that wrote it, each sign-in module's last step, and the hash of the snapshot's model once it is applied. The file ends with the statement that records it in `migrations.history` with the snapshot's model, which the server reads at startup (see [The record of migrations](../guides/deploy.md#the-record-of-migrations))
|
|
678
|
+
Writes what the config and this version of Protobase need beyond the project's `protobase.snapshot.json` as the project's next migration in `--dir`: one numbered file each run, named after what it holds, such as `004_auth.sql` or `005_changes_webhooks.sql`, after the folder's highest number, and rewrites the snapshot to end at it, so both show in the pull request. The file starts with lines Protobase reads back: `-- protobase:id`, a random id of its own, then the version that wrote it, each sign-in module's last step, the schema modules' part of the model as JSON (`-- protobase:modules`), and the hash of the snapshot's model once it is applied. The file ends with the statement that records it in `migrations.history` with the snapshot's model, which the server reads at startup (see [The record of migrations](../guides/deploy.md#the-record-of-migrations)), with whether it is additive (see [Changes that break older code](#changes-that-break-older-code)). It writes nothing, the snapshot included, when nothing differs. Apply the file with the project's other migrations.
|
|
679
|
+
|
|
680
|
+
A sign-in module the config turns off is [kept](auth.md#turning-a-module-off): on a terminal it asks what becomes of its tables, keep being the default and for now the only choice, and without one it needs `--off keep`. A kept module turned on again goes on from its step; with `DATABASE_URL` set and rows in its tables, it asks on a terminal whether to bring them back, and needs `--force` without one, and the file it writes then applies with `protobase migrate --force`.
|
|
681
|
+
|
|
682
|
+
The snapshot describes the schema the project's migrations make: the sign-in store as the runtime reads it, each module that is on with its tables, columns and indexes under the names the config gives them, its last step, and whether every table whose rows belong to an organization has the column that names it, with `modelHash`, the SHA-256 of exactly that; what the migrations decided, the file it ends at with its id, its `format` and the version of Protobase that wrote it, the modules kept or archived, whether organizations were ever on and the default organization, and in `external` the store's relations Protobase did not make, such as the user view of [`users.keyTable`](auth.md#a-view-over-another-systems-users), which no step alters; and `physical`, every schema, table, column and index name the store has. Its `model.modules` holds each built-in schema, each a module with numbered steps, such as the [jobs schema](jobs.md#the-jobs-schema) (`jobs`), the [change capture](events.md#migrations-and-startup) (`changes`) and its [history](history.md#the-table) and [notices](live.md#the-table) tables (`history`, `notices`), the [webhook subscriptions](webhooks.md#subscriptions-in-the-admin-app) of `webhooks` (`webhooks`) and their delivery log with `deliveryLog` (`webhook_delivery_log`), the [workflows, versions and runs](workflows.md#turning-them-on) of `workflows` (`workflows`) with their storage configurations and custom steps while those are on (`workflow_storage`, `workflow_custom_steps`), the [devices and history tables](notifications.md#table-names) of `notifications` (`notification_devices`, `notification_history`), each unless a store of the app's own replaces it, the [comments table](comments.md#the-table) (`comments`), the table of the bounces and complaints of [`mailFeedback`](email.md#bounces-and-complaints) (`mail_feedback`), written before the change capture whose triggers capture its events, and the table of who unsubscribed from bulk mail of [`unsubscribe`](email.md#unsubscribe) (`unsubscribes`), and the [query role](assistant.md#the-built-in-backend) of the assistant's read-only queries (`query`), each at its last step and the version of the functions over it, with the names it takes from the config, such as the schema `events`, `webhooks.schema` or `workflows.schema` names, the notifications tables' names and columns and the users table they point at, and the triggers of each captured resource at a digest of their statements; it keeps one the config no longer needs as the migrations left it. That part of the file comes from the snapshot alone too, after the store's: the module's steps after its last one, then its functions where the snapshot has another version, then the triggers of each resource whose statements differ, such as one added or one whose field became sensitive; a file that replaces them with a version the code before does not run on says `-- protobase:breaks-older-code`. A name the config changes, such as `events.schema`, `webhooks.schema` or a notifications table's column, is refused, since Protobase does not write renames; so is turning sign-in on under notifications tables made without it, whose user column then points at no users table. With `DATABASE_URL` set, it refuses to write a module from its first step onto a database that has it already, as made by an earlier version's migration, and names `baseline`. The sign-in store's part of the file comes from the snapshot alone, without a database: the [steps](auth.md#migrations) after each module's last one, those a newer Protobase ships included, each followed by its row in `auth_steps`, and for a snapshot whose store has no `auth_steps` yet, that table first, made from `auth_migrations`. Each runs only while `auth_steps` lacks it, so a database that has it already, such as one [`migrate-release`](#release-steps) brought to a newer release, skips it. With `DATABASE_URL` set, the checks of those steps run first against the database's rows, in a read-only transaction, and refuse as the step would, such as on two accounts with one provider and account id. A name the store has that the config changes, such as a table renamed in [`auth.tables`](auth.md#table-and-column-names), is refused with what changed, since Protobase does not write renames: write it by hand with `--custom` and `--record-config`. A store a newer Protobase describes otherwise with no step to apply, such as personal access tokens now holding an organization's rows, gets a file that records it anew and changes nothing in the database. A step whose work cannot run in a transaction leaves it to the file's `.after.sql`, such as the batches that give each personal access token the [default organization](auth.md#the-default-organization) when organizations turn on, and the index it builds concurrently. With organizations on or ever on, the file needs `organizations.default` in the config, and turning them on the first time over a store that may hold users needs `organizations.existingUsersRole`; it refuses without them, as `migration check` reports.
|
|
683
|
+
|
|
684
|
+
The rest of the file is what the config still needs of the database, compared with it, under its own comment and named in the file name as in brackets: with [`userTables`](user-tables.md#the-switch), the delete of the definitions of tables that resources in code [own now](user-tables.md#taking-a-table-over-as-code) (`owned_definitions`). It is compared only with `DATABASE_URL` set, against the database with the project's migrations applied, and the command says so without it. The jobs schema is written when the config has jobs, `webhooks`, `workflows`, `notifications` or `mailFeedback`, or uses the relay, or a resource has events, history, comments or an [AI field](ai.md) or is a [time rollup](data-config.md#keeping-it-up-to-date). The rollups and formulas module is written when a resource has [rollup or formula fields](data-config.md#rollups-and-formulas) or is a [time rollup](data-config.md#time-rollups) (`computed`): the `rollups` schema, then per field and rollup the columns, the functions and triggers that keep the rollups, and the tables, indexes and refresh functions of time rollups; a changed one is dropped and made again, and what sets a rollup's values or fills a time rollup goes in the file's `.after.sql`, which runs once its transaction commits. The `derived` schema is written when a resource has [derived values](derived.md), with each value declared and its fill enqueued, and a value no longer declared loses its state. The schemas of user tables (`user_tables`) are written with [`userTables`](user-tables.md#the-switch); the tables users add inside them are data, made while the app serves. The change capture is written when a resource has events, history, live updates or derived values, another's derived values or live updates read it, or `mailFeedback` is on; its history table with history, and its notices table with live updates. The webhooks, workflows, notifications, [status](status.md), [calls](calls.md), comments, mail feedback and unsubscribe modules, the `realtime_relay` schema of [`realtime: { processes: 'many' }`](supabase-realtime.md#several-processes) and the `channels` schema of [`realtime: { sqlSends: true }`](channels.md#from-sql) are written while the config turns their feature on, and none of them while it is off; turning a feature off later leaves its tables as they are. Calls write the notices table too, which rings reach open pages through. The [relay](realtime-relay.md#sessions) module, the trigger that ends a deleted session's sockets and `realtime.members` with the triggers that keep it from channel records that list their members, is written with `realtime.relay`, and for an app with sign-in and a live resource whose conversation page has `members`, after the auth store's steps, since its trigger is on the sessions table. The query module, the role the [assistant's read-only queries](assistant.md#the-built-in-backend) run as with the function they run through, is written last when the command finds the built-in assistant's key (`query`), with the role's grants on each resource's table but those of the auth store: a resource added writes only its grants, and one removed takes them back. It reads the config merged with those of `--extend` as `dev` merges them, and the `.env` nearest the current directory.
|
|
672
685
|
|
|
673
|
-
|
|
686
|
+
#### Changes that break older code
|
|
674
687
|
|
|
675
|
-
|
|
688
|
+
Every file is additive, so the code still running while it deploys, and the version before it once rolled back, still runs on the database, or it breaks older code, and that code refuses to start rather than fail on a request, when the file changes the model the record holds (see [The record of migrations](../guides/deploy.md#the-record-of-migrations)). With `DATABASE_URL` set, `protobase migration` applies the file to the database in a transaction it rolls back, reads the schema before and after, and names what older code loses: a table, column, sequence, function or enum value dropped, renamed or moved, a trigger dropped, disabled or firing on fewer events, a table or column made again with its rows or values lost, a column whose type changes or that becomes `not null`, a required column without a default or one losing its default, and a new constraint or unique index on a table there already, which rows older code writes may break. A view, function, trigger or generated column made again is not lost, and taking back a privilege, which only narrows access, is always allowed. What the sign-in modules' steps take, such as the organization every personal access token needs once organizations turn on, is Protobase's own path: the file then says `-- protobase:breaks-older-code` in its header, with what the steps take under it, and is recorded as not additive. What another part would take is refused before anything is written, naming it, with the way through: write the change by hand with `--custom` and mark the file `-- protobase:breaks-older-code`. Protobase's own parts keep what older code still reads, such as the triggers of a resource that no longer has events, history or live updates while another resource is still captured, so the dispatch job drains what they capture; they follow the resource's fields, so a field made sensitive is no longer copied. Once no resource is captured, the change capture drops them, in a file that says `-- protobase:breaks-older-code`; so does the function and trigger of a rollup field the config no longer has, which the next file drops. A rename or a move is always one file that breaks older code, with no view or trigger in between. A column Protobase adds is retired rather than dropped once the config no longer needs it: the column of a rollup or formula field the config no longer has stays as it is, and putting the field back finds it, with its values; the columns a [sign-in module turned off](auth.md#turning-a-module-off) adds to the tables still on stay under their names, watched for the writes older code still makes, and are in use again, with their values, once it is on again. A resource field's column is the project's own, as is its table: removing the field writes nothing, and dropping the column is a hand-written file marked `-- protobase:breaks-older-code`. <gap>`protobase migration` cannot write a statement that takes from older code under `-- protobase:contract <id>`, naming the migration that stopped older code from using it, because the sign-off and delete paths are not implemented.</gap> <gap>A sign-in step that takes from older code cannot carry `-- protobase:contract` because steps naming the migration that stopped older code from using what they take is not implemented.</gap> A [time rollup](data-config.md#time-rollups) that changes has its table made again and filled from its source, which loses no rows, as its comment marks it as Protobase's: only the columns older code reads that it no longer has are named.
|
|
676
689
|
|
|
677
|
-
- `--custom <name>` writes an empty numbered file, `NNN_<name>.sql`, with Protobase's lines and `-- protobase:custom`, for statements you write by hand, and makes the snapshot end at it.
|
|
678
|
-
- `check`, for CI, writes nothing and exits 1 with a line for each problem: a config or Protobase version that needs steps the migrations lack, a name the config changed, two files that share a number when Protobase wrote one of them, a snapshot that does not end at the newest file Protobase wrote or whose model does not match its `modelHash`,
|
|
690
|
+
- `--custom <name>` writes an empty numbered file, `NNN_<name>.sql`, with Protobase's lines and `-- protobase:custom`, for statements you write by hand, and makes the snapshot end at it. Say what it is among its `-- protobase:` lines, which `check` requires: `-- protobase:additive` once it only adds, which `check` verifies with Postgres's own parser, statement by statement, passing only what adds, such as a new table (not `if not exists`, which may find one with rows), a nullable or defaulted column, a plain index, a constraint or unique index on a table the file makes, a new enum value, a trigger dropped and made again on its table, and `create or replace` of a view or function, which Postgres refuses when its columns, arguments or result change; a dropped default, a renamed enum value and a `delete` inside an `insert` or `update` count as taking from older code; or `-- protobase:breaks-older-code` when it takes from older code what it reads, such as a rename or a dropped column. Either way `migrate` records it as not additive unless it says `-- protobase:additive`. Write the statements above the statement that records it, which stays last; `migrate` fills in its hash and whether it is marked additive from the finished file. With `--record-config`, the snapshot takes the sign-in store's names as the config has them now, each module at the step it was: the file is where you make the store match, such as a rename, and it says `-- protobase:breaks-older-code`. With `--after`, it also writes an empty `NNN_<name>.after.sql` beside it, for statements that cannot run in a transaction, such as `create index concurrently if not exists`, which `migrate` runs once the file commits (see [The record of migrations](../guides/deploy.md#the-record-of-migrations)).
|
|
691
|
+
- `check`, for CI, writes nothing and exits 1 with a line for each problem: a config or Protobase version that needs steps the migrations lack, of the sign-in store or a schema module, or a schema module the snapshot does not record at all, a name the config changed, two files that share a number when Protobase wrote one of them, a snapshot that does not end at the newest file Protobase wrote or whose model does not match its `modelHash`, a hand-written file that says neither `-- protobase:additive` nor `-- protobase:breaks-older-code`, says both, has a statement marked `-- protobase:contract`, which only Protobase writes, or is marked additive while a statement does not only add, and a `-- protobase:contract <id>` that names no file before its own. It also prints a `warning:` line, which leaves the exit code alone, for each statement the `squawk-cli` linter finds that holds a lock blocking reads while it scans or rewrites a table, such as `set not null` or a column type change, and for each statement of a file Protobase wrote that takes from older code without `-- protobase:contract`, such as a formula's column made again.
|
|
692
|
+
- `baseline` describes, once, what the project's earlier migrations made that the snapshot does not record: each schema module the config needs, such as a jobs schema an earlier version's `protobase migration` wrote, read from its catalogue as the step pg-boss's version is at and the version Protobase's functions carry, and refused at a pg-boss version no step records, or a change capture, read as its first step once the outbox is there, `capture()` at its version when its body is the one this version writes, and each resource whose table has triggers calling it, which the next `protobase migration` writes again when they are not the ones the config makes, and refused for an outbox without its `database_role` column or a history table without its indexes, or the rollups and formulas, read as their first step once the `rollups` schema is there, and each rollup's column, formula's column, rollup's function and trigger, and time rollup's table and refresh function found, current when it is what this or, for a rollup or formula, an earlier version made of the config, else made again by the next `protobase migration`, as a time rollup an earlier version made is, and refused where the column or table is the project's own or a rollup's column is of another type, or the `derived` schema, read as its first step once its tables, sequence and indexes are there, with each value `derived.declared` marks, and refused without the sequence, the pending table or their indexes, or the schemas of user tables, read as their first step once every part is there, and refused for a definitions table made before organizations, or the tables of webhooks, workflows and notifications, each read as its first step once its tables are there with every index and, for a devices or history table of the app's own, each column under its configured name and the foreign key to the users, the webhooks' functions at the version their comments carry and the workflows' at version 28, the statuses table read as its first step once it has its columns and the foreign key to the users, the calls schema as its first step once its tables are there and its second once its guest tables are too, the `realtime_relay` and `channels` schemas as their first step once their tables are there, `channels.send()` too, the comments, mail feedback and opt-outs tables as their first step once each is there with every index, the query role as its first step once it and the `query` schema are there, with its function at version 1 when it is the one this version writes, owned by the role and not executable by everyone, each resource whose table it reads, and each table it reads that no resource has, which the next `protobase migration` takes back, and the relay's trigger and `realtime.members` functions at their versions with each listed channel whose triggers are the ones this version writes, enabled on its table and its partitions, else written again by the next `protobase migration`, and refused at an older version or shape no step records, such as webhooks without the indexes their version 2 added, a devices table without its session column, a feedback table whose recipient a complaint cannot leave out, or `realtime.members` functions before their version 3; in a project without a snapshot, the sign-in store too. It writes `NNN_baseline.sql`, which changes nothing and records itself, and the snapshot with them, after which `protobase migration` writes only what they lack. For the sign-in store, it starts the snapshot of a project whose store came from auth migrations written before it, such as an app's existing `db/migrations/*_auth.sql`, once: run it with `DATABASE_URL` pointing at the database with those migrations applied. In one read-only transaction it reads the steps the store records in `auth_steps`, or for a store an earlier Protobase made, the versions it records in `auth_migrations`, and the tables of the store's schema, and writes `NNN_baseline.sql`, which drops nothing, changes nothing but making `auth_steps` from `auth_migrations` where the store lacks it, and records itself in `migrations.history`, and `protobase.snapshot.json` describing the store as it is: each module the config has on at the step its recorded versions reach, so the former chain's versions 1 to 8 put each module at its first step; a module that is off but has tables as kept; organizations as ever on when a table whose rows belong to an organization, such as `member`, has the column that names it; and the user table as external when the config reads it through [`users.keyTable`](auth.md#a-view-over-another-systems-users), so no step writes to it; a user table the database has as a view needs `users.keyTable`, naming the table that holds the user ids, and is refused without it. `auth_migrations` stays as it is, read no more. Apply the file with the project's other migrations, then run `protobase migration`, which writes only the steps the store lacks, and nothing for a current store. It refuses a project whose snapshot records every schema module the database has, a store with neither record, one made before 0.48, which [`auth migration`](#auth-migration) adopts first, a version this Protobase does not ship, and a table a recorded step made that the schema lacks under the name the config gives it, such as one renamed since, naming each. <gap>`baseline` cannot refuse a column renamed since a step made it because recording which columns each step adds is not implemented.</gap> The first `protobase migration` after it keeps the organization of each personal access token that has one, and refuses one without, naming it, rather than give it the [default organization](auth.md#the-default-organization): no other column says where a token works.
|
|
679
693
|
- `drop` deletes the newest file Protobase wrote and takes the snapshot back to the one before it, from that file's lines, or removes the snapshot with the first file. It refuses while two files share a number or the snapshot does not end at the newest file, so it never deletes another branch's file, and after a file that recorded a changed config, whose snapshot before it only version control has.
|
|
680
694
|
- `up` rewrites the snapshot in this version's format. Every other command refuses a snapshot of an older format, and one of a newer format, which only a newer Protobase reads.
|
|
681
695
|
|
|
@@ -710,7 +724,7 @@ Prints the app's entry for the apps file of [Realtime](realtime-relay.md), as JS
|
|
|
710
724
|
protobase auth migration [--dir db/migrations] [--name auth]
|
|
711
725
|
```
|
|
712
726
|
|
|
713
|
-
Run inside a project, against its database with the project's migrations applied. It reads the
|
|
727
|
+
Run inside a project, against its database with the project's migrations applied. It reads the steps the store applied from its `auth_steps` table, or for a store an earlier Protobase made, the versions it applied from `auth_migrations`, which the file then records by step in `auth_steps`, and writes the steps of the modules the config has on that it has not, each followed by its row there, as the next migration in `--dir`: `009_auth.sql` after `008_...`, with the store's schema, user table, columns and id type as the config's `auth` names them. Nothing is written when the store is current and the project has a snapshot, nor with sign-in off, when it says so and exits 0. The file only adds, never drops; apply it with the project's other migrations. It starts with Protobase's lines, ends with the statement that records it in `migrations.history`, and starts the project's `protobase.snapshot.json`, or moves it on, with the store at every step, so [`migration`](#migration) goes on from it. A project without a snapshot whose store is current, such as one whose auth migrations an earlier Protobase wrote, gets a file without statements that starts it, and one whose store is current but not in `migrations.history`, such as one migrated before that record, a file that only records it. For a store made before 0.48, with neither record, it writes the step that adopts it once, and refuses one that matches no version, naming what differs. See [migrations](auth.md#migrations).
|
|
714
728
|
|
|
715
729
|
## `import supabase`
|
|
716
730
|
|
|
@@ -70,7 +70,7 @@ Once a resource takes comments, admins get **Comments** (the built-in resource `
|
|
|
70
70
|
|
|
71
71
|
## The table
|
|
72
72
|
|
|
73
|
-
[`protobase migration`](cli.md#migration) adds the schema `comments` with the table `comments.comments`; apply it with the project's other migrations. It has an index for each column the Comments list filters on
|
|
73
|
+
[`protobase migration`](cli.md#migration) adds the schema `comments` with the table `comments.comments`; apply it with the project's other migrations. It has an index for each column the Comments list filters on. `protobase dev` and `protobase serve` do not start while a resource has comments and the [record of migrations](../guides/deploy.md#the-record-of-migrations) lacks the table. [`protobase migration baseline`](cli.md#migration) records once a table an earlier version's migration made, and refuses one without every index. A deleted record's comments stay in the table.
|
|
74
74
|
|
|
75
75
|
| Column | |
|
|
76
76
|
| --- | --- |
|
|
@@ -162,7 +162,7 @@ export default defineConfig({ runtimeUpdates: false })
|
|
|
162
162
|
- **Type:** `{ processes?: 'one' | 'many', sqlSends?: boolean, relay?: { url, app, apiKey, ticketKey, channels, presence? } }`
|
|
163
163
|
- **Default:** `{ processes: 'one', sqlSends: false }`
|
|
164
164
|
|
|
165
|
-
How [channels](../channels.md) and [Supabase Realtime](../supabase-realtime.md) are served. With `'one'`, one process serves the app, and broadcasts and presence go between its channels in memory. Set `'many'` when several processes serve the app behind one address: broadcasts and presence then go between the processes through the `realtime_relay` schema of the app's database, which [`protobase migration`](../cli.md#migration) writes and the server
|
|
165
|
+
How [channels](../channels.md) and [Supabase Realtime](../supabase-realtime.md) are served. With `'one'`, one process serves the app, and broadcasts and presence go between its channels in memory. Set `'many'` when several processes serve the app behind one address: broadcasts and presence then go between the processes through the `realtime_relay` schema of the app's database, which [`protobase migration`](../cli.md#migration) writes and the server finds in the [record of migrations](../../guides/deploy.md#the-record-of-migrations) at start. See [Several processes](../channels.md#several-processes). Set `sqlSends: true` to let SQL in the app's database broadcast to channels with `channels.send()`, from the `channels` schema that `protobase migration` writes and the server finds in the record of migrations at start. See [From SQL](../channels.md#from-sql). With `relay`, the app also hands its realtime connections to a relay, so it can sleep while people stay connected; see [Realtime relay](../realtime-relay.md).
|
|
166
166
|
|
|
167
167
|
```ts
|
|
168
168
|
export default defineConfig({ realtime: { processes: 'many', sqlSends: true } })
|
|
@@ -362,7 +362,7 @@ export default defineConfig({ notifications: { history: { table: 'inbox_items' }
|
|
|
362
362
|
People set their own status, busy, do not disturb or a text of their own, which the people they share an organization with see, and do not disturb holds their pushes. It needs `auth` with organizations, and its table is written by `protobase migration`. See [Status](../status.md).
|
|
363
363
|
|
|
364
364
|
```ts
|
|
365
|
-
export default defineConfig({ auth: { organizations: true }, status: {} })
|
|
365
|
+
export default defineConfig({ auth: { organizations: { enabled: true, default: { id: 'main', name: 'Main', slug: 'main' } } }, status: {} })
|
|
366
366
|
```
|
|
367
367
|
|
|
368
368
|
## status.schema
|
|
@@ -33,7 +33,7 @@ The roles users can hold. `admin` is always included. With [`organizations`](#au
|
|
|
33
33
|
|
|
34
34
|
```ts
|
|
35
35
|
export default defineConfig({
|
|
36
|
-
auth: { roles: { manager: { label: 'Manager', grants: ['sales'] }, sales: 'Sales' }, organizations: true },
|
|
36
|
+
auth: { roles: { manager: { label: 'Manager', grants: ['sales'] }, sales: 'Sales' }, organizations: { enabled: true, default: { id: 'main', name: 'Main', slug: 'main' } } },
|
|
37
37
|
})
|
|
38
38
|
```
|
|
39
39
|
|
|
@@ -50,13 +50,20 @@ export default defineConfig({ auth: { roles: ['admin', 'sales', 'warehouse'], de
|
|
|
50
50
|
|
|
51
51
|
## auth.organizations
|
|
52
52
|
|
|
53
|
-
- **Type:** `
|
|
53
|
+
- **Type:** `{ enabled: boolean; default?: { id: string; name: string; slug: string }; existingUsersRole?: string; create?: 'admins' | 'everyone'; names?: { singular: string; plural: string }; createDescription?: string; creatorAppRoles?: 'all' | string[]; invitationDays?: number; membershipLimit?: number; deleteRecords?: 'refuse' | 'cascade'; invitationUrl?: string }`
|
|
54
54
|
- **Default:** off: the app works as one organization
|
|
55
55
|
|
|
56
|
-
|
|
56
|
+
`enabled: true` turns on organizations: people work in one at a time, records of `.tenant` resources are scoped to it, and roles are held globally or per membership. `create` is who may create one (default `admins`), `names` what the screens call an organization, as a view's names are written (default Organization), `createDescription` a line under the title of the screen where someone names their first, `creatorAppRoles` the roles its creator holds there (default `all`), `invitationDays` how long an invitation works (default 7), `membershipLimit` the most members (default 1000), `deleteRecords` whether deleting one is refused while it has records (`refuse`, the default) or left to the foreign keys (`cascade`), and `invitationUrl` the page an invitation opens (default `/-/invitation` on the origin the invitation was made on). Its hooks are in `auth/hooks.ts`. See [Organizations](../auth.md#organizations) and [First organization](../auth.md#first-organization-and-start-address).
|
|
57
|
+
|
|
58
|
+
`default` is the default organization: the one that stands in for every organization while they are off, which [`protobase migration`](../cli.md#migration) makes when they turn on and records in `protobase.snapshot.json`. It refuses without it once organizations are on or were ever on, so turning them off is `enabled: false` with `default` kept. `existingUsersRole` is the app role, one a member can hold, that each user there already gets in the default organization when organizations turn on for the first time; it is required then, unless the store is new and holds no users. See [The default organization](../auth.md#the-default-organization).
|
|
57
59
|
|
|
58
60
|
```ts
|
|
59
|
-
export default defineConfig({
|
|
61
|
+
export default defineConfig({
|
|
62
|
+
auth: {
|
|
63
|
+
roles: { sales: 'Sales' },
|
|
64
|
+
organizations: { enabled: true, default: { id: 'main', name: 'Main', slug: 'main' }, existingUsersRole: 'sales', create: 'everyone', invitationDays: 14 },
|
|
65
|
+
},
|
|
66
|
+
})
|
|
60
67
|
```
|
|
61
68
|
|
|
62
69
|
## auth.signUp
|
|
@@ -177,6 +184,17 @@ The OpenID Connect providers the platform adds for people who already have an ac
|
|
|
177
184
|
export default defineConfig({ auth: { signInProviders: [] } })
|
|
178
185
|
```
|
|
179
186
|
|
|
187
|
+
## auth.signInDevices
|
|
188
|
+
|
|
189
|
+
- **Type:** `boolean`
|
|
190
|
+
- **Default:** `false`
|
|
191
|
+
|
|
192
|
+
Lets the platform's sign-in providers from the environment sign in [devices](../auth.md#device-accounts) (`PROTOBASE_SIGN_IN_DEVICES=true`), and makes their tables with the next `protobase migration`. Without it, such a provider stops the server at startup. A provider of `auth.signInProviders` with `devices: true` needs no switch.
|
|
193
|
+
|
|
194
|
+
```ts
|
|
195
|
+
export default defineConfig({ auth: { signInDevices: true } })
|
|
196
|
+
```
|
|
197
|
+
|
|
180
198
|
## auth.operator
|
|
181
199
|
|
|
182
200
|
- **Type:** `{ issuer: string; clientId: string; clientSecret: string; name?: string; group?: string; scopes?: string[]; acrValues?: string[]; sessionMinutes?: number } | false`
|
|
@@ -12,7 +12,7 @@ import { defineConfig } from '@protobase/config'
|
|
|
12
12
|
|
|
13
13
|
export default defineConfig({
|
|
14
14
|
timezone: 'Europe/Amsterdam',
|
|
15
|
-
auth: { roles:
|
|
15
|
+
auth: { roles: { sales: 'Sales' }, organizations: { enabled: true, default: { id: 'main', name: 'Main', slug: 'main' } } },
|
|
16
16
|
jobs: {
|
|
17
17
|
'send-invoice': { description: 'Mails an invoice to its customer.', arguments: { invoiceId: 'bigint' }, maxAttempts: 5 },
|
|
18
18
|
},
|