@protobase/cli 0.48.0 → 0.49.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/build/config-bundle.js +6 -1
- package/dist/build/entry-code.js +30 -17
- package/dist/cli/package.js +1 -1
- package/dist/config/build-project.js +70 -0
- package/dist/config/called-config.js +11 -0
- package/dist/config/config-defaults.js +30 -0
- package/dist/config/env-values.js +20 -0
- package/dist/config/lazy-imports.js +18 -0
- package/dist/config/merge-config.js +72 -0
- package/dist/config/project-files.js +48 -0
- package/dist/config/project-parts.js +201 -0
- package/dist/config/read-config.js +31 -0
- package/dist/config/source-config.js +37 -0
- package/dist/dev/create-db.js +2 -2
- package/dist/dev/entry.js +9 -10
- package/dist/dev/project.js +23 -47
- package/dist/main.js +3 -3
- package/dist/migration/load-project.js +34 -34
- package/dist/migration/parts.js +2 -2
- package/dist/migration/summary.js +2 -2
- package/dist/mobile/api-origin.js +4 -4
- package/dist/mobile/load-project.js +13 -12
- package/dist/mobile/register.js +1 -1
- package/dist/mobile/run.js +1 -1
- package/dist/organizations/register.js +1 -1
- package/dist/project/error-reporter.js +3 -3
- package/dist/project/import-project-file.js +4 -2
- package/dist/project/load-bundle-project.js +16 -25
- package/dist/project/load-data-config.js +1 -1
- package/dist/project/pg-db.js +10 -4
- package/dist/project/placeholder-secret.js +2 -2
- package/dist/project/project-admin.js +12 -11
- package/dist/serve/create-admin.js +9 -4
- package/dist/serve/host-module-ids.js +1 -0
- package/dist/serve/host-modules.js +3 -1
- package/dist/serve/lifecycle.js +17 -13
- package/dist/serve/load-bundle.js +41 -11
- package/dist/serve/mobile-release.js +1 -1
- package/dist/serve/serve-bundle.js +3 -3
- package/dist/supabase/auth-config.js +33 -28
- package/dist/supabase/config-jobs.js +28 -27
- package/dist/supabase/config-object.js +25 -0
- package/dist/supabase/cron-job-file.js +13 -14
- package/dist/supabase/cron-jobs-write.js +6 -12
- package/dist/supabase/cron-plan.js +3 -51
- package/dist/supabase/project-files.js +4 -9
- package/dist/supabase/report.js +3 -3
- package/dist/users/load-auth.js +16 -15
- package/dist/worker/start-worker.js +18 -15
- package/package.json +8 -6
- package/skills/protobase/SKILL.md +2 -1
- package/skills/protobase/references/features/notifications.md +1 -1
- package/skills/protobase/references/features/start-a-project.md +3 -0
- package/skills/protobase/references/getting-started.md +2 -2
- package/skills/protobase/references/guides/api-functions.md +4 -4
- package/skills/protobase/references/guides/deploy.md +7 -7
- package/skills/protobase/references/guides/existing-database.md +11 -31
- package/skills/protobase/references/guides/first-plugin.md +1 -1
- package/skills/protobase/references/guides/mobile-app.md +5 -3
- package/skills/protobase/references/guides/plugin-build.md +1 -1
- package/skills/protobase/references/guides/push-notifications.md +6 -7
- package/skills/protobase/references/guides/remote-resources.md +22 -31
- package/skills/protobase/references/reference/access.md +3 -3
- package/skills/protobase/references/reference/ai.md +1 -9
- package/skills/protobase/references/reference/api.md +15 -15
- package/skills/protobase/references/reference/app-identity.md +9 -10
- package/skills/protobase/references/reference/assistant.md +6 -6
- package/skills/protobase/references/reference/auth.md +112 -86
- package/skills/protobase/references/reference/bottom-bar.md +1 -1
- package/skills/protobase/references/reference/cli.md +35 -85
- package/skills/protobase/references/reference/comments.md +0 -8
- package/skills/protobase/references/reference/config.md +152 -0
- package/skills/protobase/references/reference/data-config.md +2 -2
- package/skills/protobase/references/reference/email.md +36 -37
- package/skills/protobase/references/reference/events.md +1 -1
- package/skills/protobase/references/reference/files.md +18 -23
- package/skills/protobase/references/reference/forms.md +5 -17
- package/skills/protobase/references/reference/history.md +2 -3
- package/skills/protobase/references/reference/installable-app.md +1 -1
- package/skills/protobase/references/reference/jobs.md +53 -56
- package/skills/protobase/references/reference/languages.md +4 -2
- package/skills/protobase/references/reference/mcp.md +10 -10
- package/skills/protobase/references/reference/notifications.md +26 -24
- package/skills/protobase/references/reference/plugins.md +11 -14
- package/skills/protobase/references/reference/remote-resources.md +18 -24
- package/skills/protobase/references/reference/supabase-realtime.md +1 -1
- package/skills/protobase/references/reference/supabase-rest.md +1 -1
- package/skills/protobase/references/reference/supabase-storage.md +1 -1
- package/skills/protobase/references/reference/ui-config.md +15 -17
- package/skills/protobase/references/reference/versioning.md +1 -1
- package/skills/protobase/references/reference/webhooks.md +6 -7
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import { projectAdmin } from "../project/project-admin.js";
|
|
2
|
-
import {
|
|
2
|
+
import { buildProject } from "../config/build-project.js";
|
|
3
|
+
import { openAppDatabase } from "../project/pg-db.js";
|
|
3
4
|
import { readManifest } from "../bundle/manifest.js";
|
|
4
5
|
import { checkBundleManifest } from "../serve/bundle-manifest.js";
|
|
5
6
|
import { loadBundle } from "../serve/load-bundle.js";
|
|
6
7
|
import { statSync } from "node:fs";
|
|
7
8
|
import path from "node:path";
|
|
8
|
-
import { checkJobsSchema, configExports, isCaptured,
|
|
9
|
+
import { checkJobsSchema, configExports, isCaptured, startJobs } from "@protobase/server";
|
|
9
10
|
import pg from "pg";
|
|
10
11
|
//#region src/worker/start-worker.ts
|
|
11
12
|
const bundleConfigFile = async (bundle) => {
|
|
@@ -14,19 +15,21 @@ const bundleConfigFile = async (bundle) => {
|
|
|
14
15
|
};
|
|
15
16
|
/**
|
|
16
17
|
* Runs the project's jobs, and nothing else, until `stop`: the same handlers `protobase serve` runs, in a process of
|
|
17
|
-
* their own. It reads the jobs schema at start and refuses one that is missing or
|
|
18
|
+
* their own, built from the config the same way. It reads the jobs schema at start and refuses one that is missing or
|
|
19
|
+
* behind, like `serve`.
|
|
18
20
|
*/
|
|
19
|
-
const startWorker = async ({
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
});
|
|
24
|
-
if (jobDefinitions(project.jobs ?? {}).length === 0 && !builtIn) throw new Error("The bundle has no jobs: export them from protobase.config.ts as `jobs`, or give a resource events, history, comments or an AI field");
|
|
25
|
-
if (!project.db && !databaseUrl) throw new Error("No database: set DATABASE_URL or export `db` from protobase.config.ts");
|
|
26
|
-
const ownDb = project.db ? void 0 : createDb(databaseUrl);
|
|
27
|
-
const db = project.db ?? ownDb;
|
|
21
|
+
const startWorker = async ({ config, databaseUrl, openDb = (url) => openAppDatabase(pg, url) }) => {
|
|
22
|
+
if (!databaseUrl) throw new Error("No database: set DATABASE_URL");
|
|
23
|
+
const database = openDb(databaseUrl);
|
|
24
|
+
const { db } = database;
|
|
28
25
|
const start = async () => {
|
|
29
|
-
const
|
|
26
|
+
const project = await buildProject(config, database);
|
|
27
|
+
const builtIn = configExports(project.config).resources.some((resource) => {
|
|
28
|
+
const model = resource.toModel();
|
|
29
|
+
return isCaptured(model) || model.comments || Object.values(model.fields).some((field) => field.ai);
|
|
30
|
+
});
|
|
31
|
+
if (project.jobs.length === 0 && !builtIn) throw new Error("The bundle has no jobs: name their module in protobase.config.ts as `jobs`, or give a resource events, history, comments or an AI field");
|
|
32
|
+
const { jobs: definitions } = projectAdmin(project, db);
|
|
30
33
|
await checkJobsSchema(db);
|
|
31
34
|
return {
|
|
32
35
|
definitions,
|
|
@@ -38,14 +41,14 @@ const startWorker = async ({ project, databaseUrl, createDb = (url) => createPgD
|
|
|
38
41
|
};
|
|
39
42
|
};
|
|
40
43
|
const { definitions, runtime: jobs } = await start().catch(async (error) => {
|
|
41
|
-
await
|
|
44
|
+
await db.destroy();
|
|
42
45
|
throw error;
|
|
43
46
|
});
|
|
44
47
|
return {
|
|
45
48
|
jobs: definitions.length,
|
|
46
49
|
stop: async () => {
|
|
47
50
|
await jobs.stop();
|
|
48
|
-
await
|
|
51
|
+
await db.destroy();
|
|
49
52
|
}
|
|
50
53
|
};
|
|
51
54
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@protobase/cli",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.49.0",
|
|
4
4
|
"description": "The protobase command: scaffold, doctor, dev, build and serve a Protobase project.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"repository": {
|
|
@@ -39,11 +39,12 @@
|
|
|
39
39
|
"dependencies": {
|
|
40
40
|
"@better-auth/core": "^1.7.12",
|
|
41
41
|
"@hono/node-server": "^2.1.3",
|
|
42
|
-
"@protobase/
|
|
43
|
-
"@protobase/
|
|
44
|
-
"@protobase/
|
|
45
|
-
"@protobase/
|
|
46
|
-
"@protobase/
|
|
42
|
+
"@protobase/config": "0.49.0",
|
|
43
|
+
"@protobase/layout": "0.49.0",
|
|
44
|
+
"@protobase/query": "0.49.0",
|
|
45
|
+
"@protobase/schema": "0.49.0",
|
|
46
|
+
"@protobase/server": "0.49.0",
|
|
47
|
+
"@protobase/ui": "0.49.0",
|
|
47
48
|
"@react-email/render": "^2.1.0",
|
|
48
49
|
"@scalar/hono-api-reference": "^0.12.10",
|
|
49
50
|
"@tailwindcss/vite": "^4.3.3",
|
|
@@ -60,6 +61,7 @@
|
|
|
60
61
|
"postcss": "^8.5.29",
|
|
61
62
|
"postgres": "^3.4.9",
|
|
62
63
|
"react": "^19.3.0",
|
|
64
|
+
"rolldown": "~1.2.11",
|
|
63
65
|
"semver": "^7.8.5",
|
|
64
66
|
"ts-morph": "^28.0.0",
|
|
65
67
|
"vite": "^8.3.3",
|
|
@@ -5,7 +5,7 @@ description: Build, check and ship a Protobase admin app on Postgres (data.ts, u
|
|
|
5
5
|
|
|
6
6
|
# Protobase
|
|
7
7
|
|
|
8
|
-
Protobase is a config-driven admin on an existing Postgres database. `config/index.ts` exports the resources, `config/<resource>/data.ts` describes a table and `ui.ts` its views, `functions/` holds the app's API functions, and `protobase.config.ts` in the project root
|
|
8
|
+
Protobase is a config-driven admin on an existing Postgres database. `config/index.ts` exports the resources, `config/<resource>/data.ts` describes a table and `ui.ts` its views, `functions/` holds the app's API functions, and `protobase.config.ts` in the project root describes the rest of the app as one object of data (sign-in, jobs, files, the API's settings), whose code Protobase finds by its path, such as `jobs/<name>.ts`. The app's own React code (components, field types, actions) is a plugin in `protobase.ui.tsx`.
|
|
9
9
|
|
|
10
10
|
Run the CLI as the project does: `npx protobase <command>`, or `bun run protobase <command>`. These pages describe the Protobase version installed in the project.
|
|
11
11
|
|
|
@@ -63,6 +63,7 @@ Guides:
|
|
|
63
63
|
|
|
64
64
|
Reference:
|
|
65
65
|
|
|
66
|
+
- When writing `protobase.config.ts`, or looking for where a job's, hook's or other piece of the app's code goes → read [protobase.config.ts](references/reference/config.md)
|
|
66
67
|
- When defining resources, fields, keys, relations, rollups, formulas and validation → read [Data config](references/reference/data-config.md)
|
|
67
68
|
- When defining views, columns, filters, charts, related records and actions → read [UI config](references/reference/ui-config.md)
|
|
68
69
|
- When asking how the packages fit together → read [Architecture](references/reference/architecture.md)
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
|
|
5
5
|
A bell in the top bar and push to browsers and phones, for what your app tells its users.
|
|
6
6
|
|
|
7
|
-
- [Notifications](../reference/notifications.md#turning-them-on) (beta): `
|
|
7
|
+
- [Notifications](../reference/notifications.md#turning-them-on) (beta): `notifications: {}` in the config gives every signed-in user a bell in the top bar and push to their browsers and phones; `ui: false` keeps only the backend.
|
|
8
8
|
- [Devices](../reference/notifications.md#devices) (beta): Each signed-in user registers their own browsers and phones to receive push, and a device gets it only while the sign-in it was registered in lasts.
|
|
9
9
|
- [Your own parts](../reference/notifications.md#your-own-parts) (beta): Senders, the device store, the history store and the bell can each be replaced by your own, such as a store outside the database.
|
|
10
10
|
- [Table names](../reference/notifications.md#table-names) (beta): The built-in stores' schema, tables and columns can be renamed, as auth's users table can.
|
|
@@ -6,6 +6,9 @@ Start a new app, or an admin over an existing Postgres database, and keep the co
|
|
|
6
6
|
|
|
7
7
|
- [Project starter](../getting-started.md#start-an-app): `npm create protobase` (also `bun create` and `pnpm create`) starts a new app from the scratch, ERP or real estate preset, with its own Postgres in Docker for development, and prints the next steps. Each preset has an `AGENTS.md` with rules for coding agents.
|
|
8
8
|
- [Example apps](../getting-started.md#run-the-erp-example): Seeded example apps on Postgres: an ERP, real estate property management and a public property portal.
|
|
9
|
+
- [One config](../reference/config.md): The whole app is one object of data in `protobase.config.ts`, typed with `defineConfig`, with a sensible default for every key.
|
|
10
|
+
- [Code found by path](../reference/config.md#where-the-code-is): The config names jobs, hooks and providers, and Protobase finds their code by its path, such as `jobs/<name>.ts`.
|
|
11
|
+
- [JSON config](../reference/config.md#the-same-config-as-json): The same config works as `protobase.config.json`, with values from the environment written as `$env`.
|
|
9
12
|
- [Scaffold from a database](../reference/cli.md#scaffold): Reads your Postgres tables and writes the data config for them, with field types, keys, relations, enums, soft delete, tenants and search filled in, and with `--ui` a first view of each.
|
|
10
13
|
- [Safe re-scaffolding](../reference/cli.md#re-running): Running scaffold again adds only the columns that are not declared yet and leaves your own config code as it is.
|
|
11
14
|
- [Schema drift report](../reference/cli.md#drift-report): After each scaffold, lists declared fields whose column is gone, whose type changed or whose enum values differ, with a suggested fix.
|
|
@@ -17,7 +17,7 @@ npm create protobase my-app
|
|
|
17
17
|
npm create protobase my-app -- --preset real-estate
|
|
18
18
|
```
|
|
19
19
|
|
|
20
|
-
The project comes with its own Postgres for development: a `docker-compose.yml` with Postgres 18, started by `bun run db:up` and stopped by `bun run db:down`, on port 55432 or, when something listens there already, the next free one. Its `.env` points at that database and has a `
|
|
20
|
+
The project comes with its own Postgres for development: a `docker-compose.yml` with Postgres 18, started by `bun run db:up` and stopped by `bun run db:down`, on port 55432 or, when something listens there already, the next free one. Its `.env` points at that database and has a `PROTOBASE_SECRET` of its own. Without Docker, set `DATABASE_URL` in `.env` to a Postgres database of yours. The next steps install it with Bun, which runs the project's scripts, start the database, migrate it, load the preset's base data, if it has any, create a user and run `bun run dev`. For scratch:
|
|
21
21
|
|
|
22
22
|
```sh
|
|
23
23
|
cd my-app
|
|
@@ -56,7 +56,7 @@ bun run --cwd examples/erp protobase users create you@example.com --generate-pas
|
|
|
56
56
|
bun run --cwd examples/erp dev # http://localhost:5173
|
|
57
57
|
```
|
|
58
58
|
|
|
59
|
-
`
|
|
59
|
+
`PROTOBASE_SECRET` in `examples/erp/.env` signs the session cookies; see [Login with Better Auth](reference/auth.md). `protobase dev` serves the admin app and the API on one port and reloads both on every change ([CLI](reference/cli.md#dev)). Its startup message says how to sign in: while the database has no users, it prints the exact `users create` command for the first admin. It stops with a clear message when another server holds its port; pass `--port 5174` to run a second app beside it.
|
|
60
60
|
|
|
61
61
|
## Run the real estate example
|
|
62
62
|
|
|
@@ -38,7 +38,7 @@ The app's own `authenticate` checks the token, the same one that guards the REST
|
|
|
38
38
|
- **Not functions:** files and folders starting with `_` (shared code, such as `functions/_shared/`), dotfiles, `*.test.ts`, `*.spec.ts` and `*.d.ts`. A folder without an `index.ts` is an error that says so.
|
|
39
39
|
- **The default export** is `defineFunction(...)` from `@protobase/server`, a bare `(request, context)` handler, or a Hono app. The last two need a signed-in caller.
|
|
40
40
|
- **Any method** reaches the function, and so does any path below its name. The request's path is the part after `/api/functions/<name>`: `/` for the function itself, `/42/advance` for `/api/functions/workshop/42/advance`. The query string and the body are passed through unchanged.
|
|
41
|
-
- **
|
|
41
|
+
- **Only the folder:** `protobase.config.ts` names no functions; they are found in `functions/`. The functions of an [extension](../reference/config.md#extensions)'s `functions/` folder are kept, and a name used by two folders is an error that names both.
|
|
42
42
|
|
|
43
43
|
## The context
|
|
44
44
|
|
|
@@ -100,7 +100,7 @@ A refusal or a missing record rejects with the problem the REST API would answer
|
|
|
100
100
|
|
|
101
101
|
`defineFunction({ roles: ['mechanic', 'desk'] }, handler)` lets in callers with at least one of those roles. `defineFunction({ capability: 'vms.update' }, handler)` lets in callers whose roles hold all of that capability ([`roles.holds`](../reference/access.md#wildcards-in-has-can-and-holds)): `*`, `vms.*` or a plain `vms.update`, but not `vms.update.own` or a `vms.update` limited to fields or rows. A wildcard means all of it, so `vms.*` needs a role that holds `vms.*` or `*`. Anyone else gets a `403 function-forbidden` problem before the handler runs. Finer rules belong in the resources' [access rules](../reference/access.md), which `records` already applies.
|
|
102
102
|
|
|
103
|
-
In an app with [roles](../reference/access.md) (`
|
|
103
|
+
In an app with [roles](../reference/access.md) (`config/roles.ts`), a function with neither is closed like a resource without a rule: only callers whose roles hold `*`, such as `admin: ['*']`, may call it, unless `defaultAccess` is `allow`. A function for everyone signed in says so with its roles. In an app without roles, every signed-in caller may call it. A `capability` needs the app's roles, and a function takes `roles` or `capability`, not both: the server refuses to start otherwise.
|
|
104
104
|
|
|
105
105
|
## Public functions and webhooks
|
|
106
106
|
|
|
@@ -404,13 +404,13 @@ export default defineFunction({ longQueries: true }, async (request, { db }) =>
|
|
|
404
404
|
- **Up to 30 seconds** per query, the most any query of an app may run. Work longer than that belongs in steps, such as a [job](../reference/jobs.md) per batch, which takes `longQueries` too.
|
|
405
405
|
- **One connection kept for it.** Under `protobase dev` and `serve`, those queries run on a connection of their own, which asks for the longer timeout when it logs in, so long work never takes the connections pages need; a second long call waits for it. A PgBouncer of your own in front of the database must list `statement_timeout` in `track_extra_parameters` to pass that on; Protobase Cloud's does. With a `db` the project exports itself, the option changes nothing.
|
|
406
406
|
- **`records` keep the API's limit,** [`statementTimeoutMs`](../reference/api.md#safety).
|
|
407
|
-
- **A query past its limit** is stopped, and the call answers `504 statement-timeout` naming the call, its limit and the way out, such as `POST /api/functions/import ran a query longer than the database's statement timeout, so the database stopped it. Make the query faster, such as with an index or a narrower filter, or give it longQueries: true, whose queries may run up to 30 s.` It goes to `
|
|
407
|
+
- **A query past its limit** is stopped, and the call answers `504 statement-timeout` naming the call, its limit and the way out, such as `POST /api/functions/import ran a query longer than the database's statement timeout, so the database stopped it. Make the query faster, such as with an index or a narrower filter, or give it longQueries: true, whose queries may run up to 30 s.` It goes to the app's error hook, `hooks/error.ts`, and the [error tracker](../reference/api.md#error-reporting).
|
|
408
408
|
|
|
409
409
|
## Errors
|
|
410
410
|
|
|
411
411
|
- **Problems:** throw an `HttpProblem` (or `badRequest`, `unauthorized`, `forbidden`, `notFound` from `@protobase/server`) to answer with an [RFC 9457 problem](../reference/api.md#errors). Hono's `HTTPException` works too.
|
|
412
412
|
- **A query the database stopped** is a `504 statement-timeout` that names the call; see [Long queries](#long-queries).
|
|
413
|
-
- **Everything else:** any other error, or a handler that returns something other than a `Response`, becomes a `500 internal-error` problem without its message. The error goes to `
|
|
413
|
+
- **Everything else:** any other error, or a handler that returns something other than a `Response`, becomes a `500 internal-error` problem without its message. The error goes to the app's error hook, `hooks/error.ts`; without one, `protobase dev` and `serve` print it.
|
|
414
414
|
|
|
415
415
|
## Functions moved from Supabase
|
|
416
416
|
|
|
@@ -28,7 +28,7 @@ dist/
|
|
|
28
28
|
plugins and the app's version
|
|
29
29
|
```
|
|
30
30
|
|
|
31
|
-
The host serves `public/` itself and sends only the manifest's `api` paths to the runtime (`/api` and the two OAuth discovery documents of [MCP clients](../reference/auth.md#oauth-for-mcp-clients), and `/.well-known/webauthn`, for an app on [several origins](../reference/auth.md#several-origins)), so loading the UI never needs a running container. With the UI it sends `X-Frame-Options: DENY` and `Content-Security-Policy: frame-ancestors 'none'`, as `protobase serve` does, so no other site frames the app's pages. The runtime serves the API alone; `protobase serve dist` serves the whole folder the same way, to check a bundle locally. The host passes `DATABASE_URL`, `PORT` (default 8787) and optionally `PROTOBASE_DB_CONNECTIONS` (the connections the app may open, so its functions and jobs leave one to requests, see [`serve`](../reference/cli.md#serve)), `REQUEST_LOG`, `PROTOBASE_SMTP_URL` and `PROTOBASE_MAIL_FROM` (the mail server and sender for [password reset](../reference/auth.md#password-reset) and emailed sign-in codes) `PROTOBASE_APP_NAME` (the app's name in that mail, see [`appName`](../reference/auth.md#configuration)) `PROTOBASE_SIGN_IN_*` (a provider people with an account sign in with, the [platform sign-in provider](../reference/auth.md#platform-sign-in-provider)) `PROTOBASE_OPERATOR_*` (the provider its staff sign in with for [staff sign-in](../reference/auth.md#staff-sign-in)), `PROTOBASE_ORIGINS` and `PROTOBASE_PASSKEY_DOMAIN` (the app's [other origins](../reference/auth.md#several-origins) and its passkey domain) `PROTOBASE_CORS_ORIGINS` (the websites that may call its API, see [CORS](../reference/api.md#cors)), `PROTOBASE_TRUSTED_PROXIES` (the proxies in front of the runtime, whose `X-Forwarded-For` names the client the sign-in limits count, see [Client addresses](../reference/auth.md#client-addresses)) and `PROTOBASE_SENTRY_DSN` (the app's [error tracker](../reference/api.md#error-reporting)), plus the project's own settings
|
|
31
|
+
The host serves `public/` itself and sends only the manifest's `api` paths to the runtime (`/api` and the two OAuth discovery documents of [MCP clients](../reference/auth.md#oauth-for-mcp-clients), and `/.well-known/webauthn`, for an app on [several origins](../reference/auth.md#several-origins)), so loading the UI never needs a running container. With the UI it sends `X-Frame-Options: DENY` and `Content-Security-Policy: frame-ancestors 'none'`, as `protobase serve` does, so no other site frames the app's pages. The runtime serves the API alone; `protobase serve dist` serves the whole folder the same way, to check a bundle locally. The host passes `DATABASE_URL`, `PORT` (default 8787) and optionally `PROTOBASE_DB_CONNECTIONS` (the connections the app may open, so its functions and jobs leave one to requests, see [`serve`](../reference/cli.md#serve)), `REQUEST_LOG`, `PROTOBASE_SMTP_URL` and `PROTOBASE_MAIL_FROM` (the mail server and sender for [password reset](../reference/auth.md#password-reset) and emailed sign-in codes) `PROTOBASE_APP_NAME` (the app's name in that mail, see [`appName`](../reference/auth.md#configuration)) `PROTOBASE_SIGN_IN_*` (a provider people with an account sign in with, the [platform sign-in provider](../reference/auth.md#platform-sign-in-provider)) `PROTOBASE_OPERATOR_*` (the provider its staff sign in with for [staff sign-in](../reference/auth.md#staff-sign-in)), `PROTOBASE_ORIGINS` and `PROTOBASE_PASSKEY_DOMAIN` (the app's [other origins](../reference/auth.md#several-origins) and its passkey domain) `PROTOBASE_CORS_ORIGINS` (the websites that may call its API, see [CORS](../reference/api.md#cors)), `PROTOBASE_TRUSTED_PROXIES` (the proxies in front of the runtime, whose `X-Forwarded-For` names the client the sign-in limits count, see [Client addresses](../reference/auth.md#client-addresses)) and `PROTOBASE_SENTRY_DSN` (the app's [error tracker](../reference/api.md#error-reporting)), `PROTOBASE_URL` and `PROTOBASE_SECRET` (the app's address and secret, unless its config sets [`url` and `secret`](../reference/config.md#defaults)), plus the project's own settings, which its config reads where it serves. The runtime and the config module are plain JavaScript for Bun: no install, no TypeScript, no writes. The runtime supplies protobase and the dependencies it runs on the server (`pg`, `postgres`, `kysely`, `better-auth`, ...), so the config module carries only the project's code and the other packages it uses. The manifest's `protobase` is the version the project is pinned to, and the host serves the bundle on the runtime of that version (see [Versioning](../reference/versioning.md#bundles-and-runtimes)). The manifest format and the routing rules are in the CLI reference: [`build`](../reference/cli.md#build), [`build-serve`](../reference/cli.md#build-serve) and [`serve`](../reference/cli.md#serve).
|
|
32
32
|
|
|
33
33
|
## Migrations
|
|
34
34
|
|
|
@@ -44,7 +44,7 @@ Every release pushes two public images to GitHub's registry, for `linux/amd64` a
|
|
|
44
44
|
| `ghcr.io/prototeamhq/protobase-dev:<version>` | Bun and the presets at `/opt/protobase/presets/<name>` (`erp`, `real-estate`, `scratch`), with the dependencies they share installed in `/workspace` and Bun's cache in `/opt/bun-cache`. It has no Node: the presets' scripts and the `protobase` command run on Bun. It runs as `bun` (1000) |
|
|
45
45
|
|
|
46
46
|
```sh
|
|
47
|
-
docker run -v "$PWD/dist:/app:ro" -p 127.0.0.1:8787:8787 -e DATABASE_URL -e
|
|
47
|
+
docker run -v "$PWD/dist:/app:ro" -p 127.0.0.1:8787:8787 -e DATABASE_URL -e PROTOBASE_URL -e PROTOBASE_SECRET ghcr.io/prototeamhq/protobase:0.2.0
|
|
48
48
|
```
|
|
49
49
|
|
|
50
50
|
In `protobase-dev`, copy a preset into `/workspace` and `bun install --frozen-lockfile` has nothing to do; installed anywhere else, it comes from the cache, without the network.
|
|
@@ -87,12 +87,12 @@ APP_DB_PASSWORD=... # openssl rand -hex 24
|
|
|
87
87
|
`app.env`, the app's own settings:
|
|
88
88
|
|
|
89
89
|
```sh
|
|
90
|
-
|
|
91
|
-
|
|
90
|
+
PROTOBASE_SECRET=... # openssl rand -base64 32
|
|
91
|
+
PROTOBASE_URL=http://localhost:8787
|
|
92
92
|
PROTOBASE_TRUSTED_PROXIES=172.30.87.1
|
|
93
93
|
```
|
|
94
94
|
|
|
95
|
-
`
|
|
95
|
+
`PROTOBASE_URL` is the address the browser opens, exactly: sign-in from `http://127.0.0.1:8787` is refused as another origin. `PROTOBASE_TRUSTED_PROXIES` is the compose network's gateway, which every connection through the published port comes from, a reverse proxy's included; trusting it lets the sign-in limits count each client by the proxy's `X-Forwarded-For` (see [Client addresses](../reference/auth.md#client-addresses)). Add the app's other variables to `app.env` too.
|
|
96
96
|
|
|
97
97
|
```yaml
|
|
98
98
|
services:
|
|
@@ -165,7 +165,7 @@ app.example.com {
|
|
|
165
165
|
}
|
|
166
166
|
```
|
|
167
167
|
|
|
168
|
-
Then set `
|
|
168
|
+
Then set `PROTOBASE_URL=https://app.example.com` in `app.env` and run `docker compose up -d` to apply it. Only then are the session cookies `Secure` and passwords and codes encrypted on the way. Another proxy must also append the client's address to `X-Forwarded-For` (see [Client addresses](../reference/auth.md#client-addresses)), or every visitor shares one sign-in limit. Do not change the port to `8787:8787`: that publishes plain HTTP on every interface, ahead of host firewall rules such as ufw. On Docker Engine older than 28.0, hosts on the same local network can still reach a port published to `127.0.0.1`, so use 28.0 or newer.
|
|
169
169
|
|
|
170
170
|
### First admin
|
|
171
171
|
|
|
@@ -173,7 +173,7 @@ Then set `BETTER_AUTH_URL=https://app.example.com` in `app.env` and run `docker
|
|
|
173
173
|
docker compose run --rm --no-deps app create-admin /app you@example.com --generate-password
|
|
174
174
|
```
|
|
175
175
|
|
|
176
|
-
Then open the address in `
|
|
176
|
+
Then open the address in `PROTOBASE_URL` and sign in with that email and the printed password: `https://app.example.com` once the proxy is in front, `http://localhost:8787` (or the SSH tunnel) only while `PROTOBASE_URL` is still `http://localhost:8787`, since sign-in from any other address is refused as another origin. `--no-deps` keeps compose from running `migrate` first, which during an upgrade would migrate under the old version while it serves; use it with every `docker compose run … app`. Where container logs leave the host, pass the password on stdin instead: `docker compose run --rm --no-deps -T app create-admin /app you@example.com --password-stdin < password.txt`.
|
|
177
177
|
|
|
178
178
|
### Upgrade
|
|
179
179
|
|
|
@@ -13,16 +13,16 @@ Protobase never changes your tables. Sign-in is the one thing it adds to the dat
|
|
|
13
13
|
In a new folder, next to the database's own code or anywhere else:
|
|
14
14
|
|
|
15
15
|
```sh
|
|
16
|
-
npm install @protobase/cli @protobase/schema @protobase/layout @protobase/server @protobase/ui pg kysely
|
|
16
|
+
npm install @protobase/cli @protobase/config @protobase/schema @protobase/layout @protobase/server @protobase/ui pg kysely
|
|
17
17
|
```
|
|
18
18
|
|
|
19
19
|
Then a `.env` beside it, which every `protobase` command reads:
|
|
20
20
|
|
|
21
21
|
```sh
|
|
22
22
|
DATABASE_URL=postgres://user:password@localhost:5432/shop
|
|
23
|
-
#
|
|
24
|
-
|
|
25
|
-
|
|
23
|
+
# The app's secret, which signs session cookies: generate your own with openssl rand -base64 32
|
|
24
|
+
PROTOBASE_SECRET=
|
|
25
|
+
PROTOBASE_URL=http://localhost:5173
|
|
26
26
|
```
|
|
27
27
|
|
|
28
28
|
Keep `.env` out of version control.
|
|
@@ -45,39 +45,19 @@ It checks the config against the database: each table and column exists, each ty
|
|
|
45
45
|
|
|
46
46
|
## 4. Add sign-in
|
|
47
47
|
|
|
48
|
-
|
|
48
|
+
Sign-in is on by default: it takes the app's address and secret from `PROTOBASE_URL` and `PROTOBASE_SECRET` in `.env`, and keeps users, sessions and signing keys in an `auth` schema of their own, next to your tables. `protobase.config.ts` says the rest; the resources in `config/` are found by that path:
|
|
49
49
|
|
|
50
50
|
```ts
|
|
51
|
-
import
|
|
52
|
-
import { auth, authenticate } from './auth/auth'
|
|
51
|
+
import { defineConfig } from '@protobase/config'
|
|
53
52
|
|
|
54
|
-
export default {
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
```ts
|
|
60
|
-
import { PostgresDialect } from 'kysely'
|
|
61
|
-
import pg from 'pg'
|
|
62
|
-
import { appPool, betterAuthAuthenticator, createAuth } from '@protobase/server'
|
|
63
|
-
|
|
64
|
-
const secret = process.env.BETTER_AUTH_SECRET
|
|
65
|
-
if (!secret) throw new Error('BETTER_AUTH_SECRET is not set: put the output of `openssl rand -base64 32` in .env')
|
|
66
|
-
|
|
67
|
-
// The app's one pool on DATABASE_URL, which `protobase dev` and `serve` share.
|
|
68
|
-
const pool = appPool(pg, process.env.DATABASE_URL!)
|
|
69
|
-
|
|
70
|
-
export const auth = createAuth({
|
|
71
|
-
// Users, sessions and signing keys live in a schema of their own, next to your tables.
|
|
72
|
-
database: { dialect: new PostgresDialect({ pool }), type: 'postgres', schemaName: 'auth', transaction: true },
|
|
73
|
-
baseURL: process.env.BETTER_AUTH_URL ?? 'http://localhost:5173',
|
|
74
|
-
secret,
|
|
53
|
+
export default defineConfig({
|
|
54
|
+
auth: { roles: ['admin'] },
|
|
55
|
+
// Everyone works in tenant 1.
|
|
56
|
+
authenticate: { tenant: 1 },
|
|
75
57
|
})
|
|
76
|
-
|
|
77
|
-
export const authenticate = betterAuthAuthenticator({ auth, tenant: 1 })
|
|
78
58
|
```
|
|
79
59
|
|
|
80
|
-
The [
|
|
60
|
+
The [config reference](../reference/config.md) has every key, and the [auth reference](../reference/auth.md) the rest of sign-in: emailed codes, passkeys, two-factor, roles, and a users table the database already has.
|
|
81
61
|
|
|
82
62
|
## 5. Write and apply the auth tables
|
|
83
63
|
|
|
@@ -157,7 +157,7 @@ Reload the app. The card shows three orders, and the app was neither rebuilt nor
|
|
|
157
157
|
|
|
158
158
|
## 7. Ship it
|
|
159
159
|
|
|
160
|
-
Put `dist/` on a static host, and list its `plugin.json` in the deployed app's `PROTOBASE_PLUGINS`, or in `
|
|
160
|
+
Put `dist/` on a static host, and list its `plugin.json` in the deployed app's `PROTOBASE_PLUGINS`, or in the `plugins` of its `protobase.config.ts`, which can also give the plugin settings and limit it to some roles (see [loading](../reference/plugins.md#loading)). Serve `plugin.json` without caching, so a new build reaches the apps on their next load, and the files in `assets/`, whose names change with their content, as long as you like.
|
|
161
161
|
|
|
162
162
|
## When the app moves on
|
|
163
163
|
|
|
@@ -21,6 +21,8 @@ Set their variables, such as `ANDROID_HOME` and `JAVA_HOME`, in your shell, not
|
|
|
21
21
|
|
|
22
22
|
```ts
|
|
23
23
|
// protobase.config.ts
|
|
24
|
+
import { defineConfig } from '@protobase/config'
|
|
25
|
+
|
|
24
26
|
export default defineConfig({
|
|
25
27
|
// ...
|
|
26
28
|
mobile: {
|
|
@@ -33,7 +35,7 @@ export default defineConfig({
|
|
|
33
35
|
- `appId` is the app's id in reverse-DNS form, the same on iOS and Android: at least two parts separated by dots, each starting with a letter and holding only letters and digits. Underscores and hyphens are refused, as iOS rejects underscores and Android rejects hyphens; `protobase mobile` stops early on such an id and suggests one without them.
|
|
34
36
|
- `plugins`, optional, are extra native [Capacitor plugins](https://capacitorjs.com/docs/plugins), npm name to version. Capacitor itself, `@capacitor/app`, `@capacitor/keyboard`, `@capacitor/splash-screen` and `@capawesome/capacitor-live-update` come with Protobase at fixed versions and cannot be listed. A plugin runs native code with the app's rights, and one that writes files, such as `@capacitor/filesystem`, lets a script that got into the page point the iOS app at files it wrote (with Capacitor's own `WebView.setServerBasePath`), so list only plugins the app needs. On Android a plugin's JavaScript cannot call `CapacitorHttp`, `CapacitorCookies` or `WebView`, and `Capacitor.convertFileSrc` URLs do not load: call those from the plugin's native code.
|
|
35
37
|
|
|
36
|
-
The app is installed under the name it shows in its upper left, on iOS and Android alike: `protobase mobile` reads it from the server the app calls (its web manifest), so a `PROTOBASE_APP_NAME` the server runs with names the app on the phone too. When that server cannot be reached, `run` and `open` warn and take the project's own name, `
|
|
38
|
+
The app is installed under the name it shows in its upper left, on iOS and Android alike: `protobase mobile` reads it from the server the app calls (its web manifest), so a `PROTOBASE_APP_NAME` the server runs with names the app on the phone too. When that server cannot be reached, `run` and `open` warn and take the project's own name, the config's `name` or `PROTOBASE_APP_NAME` in its environment, and `build` stops.
|
|
37
39
|
|
|
38
40
|
On the home screen the app shows Protobase's icon, on iOS and Android alike. <gap>An app cannot show its own icon on the phone because setting the mobile app's icon is not implemented.</gap>
|
|
39
41
|
|
|
@@ -59,7 +61,7 @@ The first run installs Capacitor into `.protobase/mobile/`, which ignores itself
|
|
|
59
61
|
|
|
60
62
|
In the app, the admin keeps clear of the status bar, the notch, the home indicator and Android's system bars, and it does not zoom: a focused input leaves the page at its scale, and so does a pinch. <gap>The status bar's text cannot follow the theme chosen in the app, only the phone's light or dark mode, because setting the status bar's style is not implemented.</gap>
|
|
61
63
|
|
|
62
|
-
The app calls the server at the
|
|
64
|
+
The app calls the server at the app's `url` (`PROTOBASE_URL` by default); pass `--api https://shop.example.com` for another one. Plain http is only for a server on this machine (`localhost`) or, on the Android emulator, its host `10.0.2.2`; any other server needs https. An Android phone on USB reaches a server on `localhost` through a forwarded port; an iPhone needs an https server. The app's UI plugins load from the device only when the server serves a built bundle (`protobase serve dist`): against `protobase dev`, use `--live`.
|
|
63
65
|
|
|
64
66
|
`--target <id>` picks the simulator, emulator or device; without it Capacitor asks.
|
|
65
67
|
|
|
@@ -98,7 +100,7 @@ protobase mobile build ios --app-version 1.2.3 --build-number 7 --api https://sh
|
|
|
98
100
|
makes a signed release binary: an `.aab` to upload to Google Play, an `.ipa` to upload to App Store Connect. It goes to `mobile-release/`, which ignores itself in git, named `<appId>-<version>-<build number>`; `--out <dir>` picks another folder.
|
|
99
101
|
|
|
100
102
|
- The binary carries the app's files, so it opens without a network and never downloads anything on its first launch; offline it says it cannot reach the server, as the web app does.
|
|
101
|
-
- A release calls its server over https, at `--api` or the
|
|
103
|
+
- A release calls its server over https, at `--api` or the app's `url`: plain http, `localhost` and `10.0.2.2` are refused.
|
|
102
104
|
- `--app-version` is the version the stores show, one to three numbers such as `1.2.3`. `--build-number` is a whole number that must rise with every upload, such as your CI's run number.
|
|
103
105
|
- Extra plugins run at build time with access to your signing key, so add only plugins you trust.
|
|
104
106
|
|
|
@@ -47,7 +47,7 @@ The project's Tailwind classes are built into the plugin's own stylesheet, `plug
|
|
|
47
47
|
|
|
48
48
|
## 4. Extensions
|
|
49
49
|
|
|
50
|
-
Each `--extend` folder's `protobase.ui.tsx` becomes a plugin of its own, `plugins/<name>/`, named by its `package.json`, else its folder, and merged before the project's, as before. Its
|
|
50
|
+
Each `--extend` folder's `protobase.ui.tsx` becomes a plugin of its own, `plugins/<name>/`, named by its `package.json`, else its folder, and merged before the project's, as before. Its config, with the code its paths find, goes into the config module and is merged before the project's when the bundle is served (see [Extensions](../reference/config.md#extensions)). Two extensions that would be plugins of one name fail the build: give one's `package.json` another `name`.
|
|
51
51
|
|
|
52
52
|
## 5. Hosts
|
|
53
53
|
|
|
@@ -8,15 +8,14 @@ You turn notifications on, send one when something happens in your app, and see
|
|
|
8
8
|
|
|
9
9
|
## 1. Turn notifications on
|
|
10
10
|
|
|
11
|
-
Add `notifications
|
|
11
|
+
Add `notifications` to `protobase.config.ts`:
|
|
12
12
|
|
|
13
13
|
```ts
|
|
14
|
-
import { defineConfig
|
|
14
|
+
import { defineConfig } from '@protobase/config'
|
|
15
15
|
|
|
16
16
|
export default defineConfig({
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
// auth, authenticate, jobs, ...
|
|
17
|
+
notifications: {},
|
|
18
|
+
// auth, jobs, ...
|
|
20
19
|
})
|
|
21
20
|
```
|
|
22
21
|
|
|
@@ -62,7 +61,7 @@ import { appPath } from '@protobase/schema'
|
|
|
62
61
|
import { sendNotification, subscription } from '@protobase/server'
|
|
63
62
|
|
|
64
63
|
// The app's public origin: the address its sign-in runs on, from its own configuration.
|
|
65
|
-
const appOrigin = process.env.
|
|
64
|
+
const appOrigin = process.env.PROTOBASE_URL ?? 'http://localhost:5173'
|
|
66
65
|
|
|
67
66
|
/** Tells an order's owner when its status changes, in their inbox and as push, with a link to the order. */
|
|
68
67
|
export const orderStatusToOwner = subscription('orderStatusToOwner', {
|
|
@@ -107,7 +106,7 @@ On an iPhone or iPad, push reaches the app once it is added to the Home Screen,
|
|
|
107
106
|
|
|
108
107
|
## 6. Turn the bell off or replace a part
|
|
109
108
|
|
|
110
|
-
- `notifications
|
|
109
|
+
- `notifications: { ui: false }` keeps sending, the history and devices, and draws no bell: your own UI reads the history and turns push on with [the browser helper](../reference/notifications.md#in-the-browser) ([without the bell](../reference/notifications.md#without-the-bell)).
|
|
111
110
|
- `shell.bell` in `protobase.ui.tsx` draws your own bell in its place ([the bell's slot](../reference/notifications.md#the-bells-slot)).
|
|
112
111
|
- Your own device store, history store or senders replace the built-in ones, such as a store outside the database ([your own parts](../reference/notifications.md#your-own-parts)).
|
|
113
112
|
- The tables' schema, names and columns can be renamed ([table names](../reference/notifications.md#table-names)).
|
|
@@ -12,12 +12,12 @@ This guide makes the desk's tickets a [remote resource](../reference/remote-reso
|
|
|
12
12
|
|
|
13
13
|
You need two Protobase projects, each with its own database, its own [admin store](../reference/auth.md#admin-store) and a user: the from-scratch preset (`examples/scratch` in the repository) is enough for both. Each runs on its own origin; this guide runs them side by side on one machine:
|
|
14
14
|
|
|
15
|
-
| | `
|
|
15
|
+
| | `PROTOBASE_URL` in its `.env` | Started with |
|
|
16
16
|
| --- | --- | --- |
|
|
17
17
|
| The service desk | `http://localhost:4720` | `protobase dev --port 4720` |
|
|
18
18
|
| The shop | `http://localhost:4710` | `protobase dev --port 4710` |
|
|
19
19
|
|
|
20
|
-
`
|
|
20
|
+
`PROTOBASE_URL` matters here: it is the issuer of each app's tokens, and the desk will recognise the shop's tokens by it.
|
|
21
21
|
|
|
22
22
|
The shop has its orders. The desk has tickets and their notes:
|
|
23
23
|
|
|
@@ -83,31 +83,26 @@ That is the desk's admin app as its agents use it. Nothing in it knows about the
|
|
|
83
83
|
|
|
84
84
|
The shop's admin app will call the desk's API from the browser, with the token the shop gave the signed-in person. The desk needs to accept two things: that token, and calls from the shop's origin.
|
|
85
85
|
|
|
86
|
-
The token: list the shop as a trusted app in the desk's
|
|
86
|
+
The token: list the shop as a trusted app in the desk's `authenticate`, and say what the shop's roles are at the desk. The origin: the shop's admin app runs on `http://localhost:4710`, so the desk answers [CORS](../reference/api.md#cors) for it:
|
|
87
87
|
|
|
88
88
|
```ts
|
|
89
|
-
// desk:
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
89
|
+
// desk: protobase.config.ts
|
|
90
|
+
import { defineConfig } from '@protobase/config'
|
|
91
|
+
|
|
92
|
+
export default defineConfig({
|
|
93
|
+
auth: { roles: ['admin'] },
|
|
94
|
+
authenticate: {
|
|
95
|
+
tenant: 1,
|
|
96
|
+
// The shop's admins sign in at the shop; the desk takes the shop's tokens and calls them `shop` here.
|
|
97
|
+
apps: [{ issuer: 'http://localhost:4710', roles: { admin: 'shop' }, tenant: 1 }],
|
|
98
|
+
},
|
|
99
|
+
// The shop's admin app, on its own origin, calls this API from the browser.
|
|
100
|
+
cors: ['http://localhost:4710'],
|
|
95
101
|
})
|
|
96
102
|
```
|
|
97
103
|
|
|
98
104
|
The desk now checks a token from the shop against the shop's keys, at `http://localhost:4710/api/auth/jwks`. Its holder is `shop` at the desk, never the desk's `admin`, so the `deskOnly` rule keeps tickets from being deleted from the shop; a shop user with any other role is refused. The [reference](../reference/remote-resources.md#signing-in-at-the-source) has the details, such as the ids those users get.
|
|
99
105
|
|
|
100
|
-
The origin: the shop's admin app runs on `http://localhost:4710`, so the desk answers [CORS](../reference/api.md#cors) for it:
|
|
101
|
-
|
|
102
|
-
```ts
|
|
103
|
-
// desk: protobase.config.ts
|
|
104
|
-
import * as config from './config'
|
|
105
|
-
import { auth, authenticate } from './auth/auth'
|
|
106
|
-
|
|
107
|
-
// The shop's admin app, on its own origin, calls this API from the browser.
|
|
108
|
-
export default { config, auth, authenticate, options: { cors: ['http://localhost:4710'] } }
|
|
109
|
-
```
|
|
110
|
-
|
|
111
106
|
`protobase dev` reloads the desk.
|
|
112
107
|
|
|
113
108
|
## 3. The shop draws the tickets
|
|
@@ -116,17 +111,13 @@ The shop lists the desk as a source, with the resources it draws from it:
|
|
|
116
111
|
|
|
117
112
|
```ts
|
|
118
113
|
// shop: protobase.config.ts
|
|
119
|
-
import
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
options: {
|
|
127
|
-
sources: [{ url: 'http://localhost:4720/api/v1', label: 'Service desk', resources: ['tickets', 'ticket_notes'] }],
|
|
128
|
-
},
|
|
129
|
-
}
|
|
114
|
+
import { defineConfig } from '@protobase/config'
|
|
115
|
+
|
|
116
|
+
export default defineConfig({
|
|
117
|
+
auth: { roles: ['admin'] },
|
|
118
|
+
authenticate: { tenant: 1 },
|
|
119
|
+
remoteResources: [{ url: 'http://localhost:4720/api/v1', label: 'Service desk', resources: ['tickets', 'ticket_notes'] }],
|
|
120
|
+
})
|
|
130
121
|
```
|
|
131
122
|
|
|
132
123
|
`url` is the desk's resources API, and `label` its name when the shop's people cannot reach it. The notes are there because a ticket's page lists them: what the shop does not draw, it does not show. A platform that runs the shop can set the same list in `PROTOBASE_SOURCES`, without touching the shop's code:
|
|
@@ -31,7 +31,7 @@ defineRoles<'orders' | 'products'>({
|
|
|
31
31
|
- **Filter** is AIP-160 text, like a filter an access rule returns. It narrows the rows of the capability, together with `.own` or `.team` when the capability has one.
|
|
32
32
|
- A limit names one resource, never `*`, and fields go with `read`, `create` or `update` only; `defineRoles` refuses anything else. At startup the server refuses a field the resource does not have, a filter that does not fit it, and a limit on a name that is not a resource.
|
|
33
33
|
|
|
34
|
-
A field is readable when the role's list allows it and the field's own [rule](#field-rules-or-field-lists) passes. Field lists take effect through the roles the server gets (`
|
|
34
|
+
A field is readable when the role's list allows it and the field's own [rule](#field-rules-or-field-lists) passes. Field lists take effect through the roles the server gets (the `roles` export of `config/roles.ts`, `resolveAccess(source, ctx, { roles })`), for every resource whatever its rules. A filter is the capability's row filter, so like `.own` it applies where the capability decides: an operation without a rule, or a rule built with `roles.can(...)`.
|
|
35
35
|
|
|
36
36
|
**Several roles add up.** A user gets every field one of their roles lists (all of them when a role holds the plain capability) and the rows any of their roles allows. Fields and rows add up separately: someone holding both `erp_sync` and `reseller` above reads `name` on unpublished products too. With Better Auth a session acts with one [active role](auth.md#active-role) at a time, so this only comes up with an authenticator that hands out several.
|
|
37
37
|
|
|
@@ -51,7 +51,7 @@ anonymous: [{ 'products.read': { fields: ['name', 'slug', 'price', 'image'], fil
|
|
|
51
51
|
```
|
|
52
52
|
|
|
53
53
|
- **It reads listed fields, and creates them through a public form only.** Each entry names one resource, `read` or `create` without a scope, and its fields; `defineRoles` refuses other capabilities, wildcards, `.own` and `.team`, and a capability without a field list, so nothing becomes public by accident, a new column included. A `create` is used by the resource's [public forms](forms.md) alone, behind their rate limit and bot check: everywhere else it does not exist.
|
|
54
|
-
- **No token, not a bad one.** A request without an `Authorization` header is served as a caller holding only this role, in no organization. A header with anything but a valid, unexpired token is still a `401`. `jwtAuthenticator` and
|
|
54
|
+
- **No token, not a bad one.** A request without an `Authorization` header is served as a caller holding only this role, in no organization. A header with anything but a valid, unexpired token is still a `401`. `jwtAuthenticator` and the authenticator of the app's sign-in tell the two apart; an authenticator of your own throws `MissingCredentials` (from `@protobase/server`) for a request without a credential to do the same.
|
|
55
55
|
- **Only its capabilities open anything.** The capability is ANDed into a resource's access rule, so a rule that allows everyone (`read: () => true`) or `defaultAccess: 'allow'` never lets an anonymous caller in on its own.
|
|
56
56
|
- **The resources API and `/api/meta` only.** Lists, records, `:search`, `:seek` and the aggregates of what the role reads, with filters, sorts and `fields` checked as for everyone: an unlisted field is refused like an unknown one. A resource it cannot read is a `404`; a write, or another operation it may not do on a resource it reads, is a `401` that asks for a token, a `create` it holds for a form included. `/api/meta` describes what it reads, for the [admin app](#browsing-the-admin-app). `/api/openapi.json`, `/api/docs`, batch writes, [API functions](../guides/api-functions.md), the assistant and the file cleanup still need a token. A [file field](files.md#downloads) it reads comes with its link, as for anyone.
|
|
57
57
|
- A tenant-scoped resource (`.tenant`) cannot be in it, since an anonymous caller works in no organization; the server refuses to start.
|
|
@@ -188,7 +188,7 @@ export const apps = resource('apps')
|
|
|
188
188
|
- **Members only.** An organization someone enters with a global role, without being a member, is not read across; the session's own organization always is. An outside app's token, such as an MCP client's, reads its own organization alone.
|
|
189
189
|
- **The tenant field is a filter.** The list has it as any other field, so `organization_id = "..."`, a facet or a column picks organizations. A caller who cannot read the tenant field lists their own organization alone. To open the sidebar entry on the current organization, which the person can then clear, see [The current organization](sidebar.md#the-current-organization).
|
|
190
190
|
- **Rows of another organization are read-only in the list.** Their `permissions` say `update: false` and `delete: false`. The admin app opens one by switching the session to its organization first: a record page the session's organization does not have looks the record up across the lists, for a resource with a key of one filterable field, so the page and everything on it are read in the record's own organization.
|
|
191
|
-
- **Where the memberships come from.**
|
|
191
|
+
- **Where the memberships come from.** The authenticator of the app's sign-in (`auth`) reads them from the store, once per request that lists across organizations. An [authenticator](auth.md#tokens-and-roles) of your own gives them as the session's `memberships`, a function answering `[{ tenant, roles }]`, and must answer only organizations the person is a member of. Without it, as with `authenticate: { jwksUrl }` instead of `auth`, a list reads the session's organization alone.
|
|
192
192
|
|
|
193
193
|
## Restricted model and row filters
|
|
194
194
|
|
|
@@ -39,14 +39,6 @@ export const ticketsView = view<typeof tickets>('tickets')
|
|
|
39
39
|
])
|
|
40
40
|
```
|
|
41
41
|
|
|
42
|
-
```ts title="protobase.config.ts"
|
|
43
|
-
import { defineConfig } from '@protobase/server'
|
|
44
|
-
import * as config from './tickets'
|
|
45
|
-
|
|
46
|
-
// The model comes from PROTOBASE_ASSISTANT_API_KEY; the summaries are filled by the app's workers.
|
|
47
|
-
export default defineConfig({ config })
|
|
48
|
-
```
|
|
49
|
-
|
|
50
42
|
## Prompts
|
|
51
43
|
|
|
52
44
|
A prompt names fields of the record as `{field}`, at least one. The model gets a fixed instruction to answer with the value only, and the prompt with each `{field}` replaced by the record's value as text: text as it is, numbers and booleans written out, json as JSON, an empty value as nothing. For an enum field, the prompt ends with the values the answer must be one of. Nothing else of the record or the app is sent. The answer is trimmed, and validated as any write is, so an enum value the field does not have, or text over its `.max()`, is refused.
|
|
@@ -95,7 +87,7 @@ Every check is made before the model is asked.
|
|
|
95
87
|
|
|
96
88
|
## The model
|
|
97
89
|
|
|
98
|
-
AI fields and actions use the [assistant's model settings](assistant.md#which-backend): `PROTOBASE_ASSISTANT_API_KEY`, with `PROTOBASE_ASSISTANT_BASE_URL` and `PROTOBASE_ASSISTANT_MODEL`, or the same keys of `
|
|
90
|
+
AI fields and actions use the [assistant's model settings](assistant.md#which-backend): `PROTOBASE_ASSISTANT_API_KEY`, with `PROTOBASE_ASSISTANT_BASE_URL` and `PROTOBASE_ASSISTANT_MODEL`, or the same keys of the config's `assistant`. They use them also when `PROTOBASE_ASSISTANT_URL` names a chat backend elsewhere, which replaces the chat, not the model, and when `assistant` is `false`, which turns off the chat only. On OpenRouter they ask for little reasoning.
|
|
99
91
|
|
|
100
92
|
`protobase dev` and `protobase serve` do not start when a resource has an AI field or a view an AI action and no key is set; `PROTOBASE_STARTUP_CHECKS=false` skips that check, as it does the others. Other commands, such as `protobase migration`, run without a key.
|
|
101
93
|
|