@protobase/cli 0.57.6 → 0.58.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.
@@ -1,4 +1,4 @@
1
1
  //#region package.json
2
- var version = "0.57.6";
2
+ var version = "0.58.0";
3
3
  //#endregion
4
4
  export { version };
@@ -3,16 +3,17 @@ import { checkJobsSchema, startJobs } from "@protobase/server";
3
3
  const store = globalThis;
4
4
  /**
5
5
  * Runs the project's jobs in the dev server: started on the first load, once the jobs schema checks out, with the app's
6
- * own mailer when it has one, and given the reloaded definitions on every load after it, so an edited job runs its new
7
- * code from the next job on. A mailer added or changed later is the jobs' from the next start.
6
+ * own mailer when it has one, and given the reloaded definitions and the rebuilt app's `decryptField` on every load
7
+ * after it, so an edited job runs its new code from the next job on. A mailer added or changed later is the jobs' from the next start.
8
8
  */
9
- const devJobs = async (db, definitions, mailer) => {
10
- if (store.__protobaseJobs) return store.__protobaseJobs.update(definitions);
9
+ const devJobs = async (db, definitions, decryptField, mailer) => {
10
+ if (store.__protobaseJobs) return store.__protobaseJobs.update(definitions, decryptField);
11
11
  if (definitions.length === 0) return;
12
12
  await checkJobsSchema(db);
13
13
  store.__protobaseJobs = await startJobs({
14
14
  db,
15
15
  jobs: definitions,
16
+ decryptField,
16
17
  ...mailer && { mailer },
17
18
  onError: (error) => console.error(error)
18
19
  });
package/dist/dev/entry.js CHANGED
@@ -49,7 +49,7 @@ const ui = {
49
49
  icons: projectIcons(projectDir)
50
50
  };
51
51
  const { admin, jobs } = projectAdmin(project, db, ui, reporter, upgradeWebSocket);
52
- await devJobs(db, jobs, project.mailer);
52
+ await devJobs(db, jobs, admin.decryptField, project.mailer);
53
53
  devRealtimePrune(admin);
54
54
  var entry_default = withDevServiceWorker(new Hono(), Boolean(project.notifications)).route("/", admin);
55
55
  /** Whether the auth store has a user yet, for the banner's sign-in line; undefined without one. */
@@ -48,6 +48,7 @@ const startServe = async ({ config, env, openDb = (url) => openAppDatabase(pg, u
48
48
  db,
49
49
  jobs: definitions,
50
50
  work: env.runJobs,
51
+ decryptField: admin.decryptField,
51
52
  ...project.mailer && { mailer: project.mailer },
52
53
  onError: (error) => console.error(error)
53
54
  }) : void 0;
@@ -29,13 +29,14 @@ const startWorker = async ({ config, databaseUrl, openDb = (url) => openAppDatab
29
29
  return isCaptured(model) || model.comments || Object.values(model.fields).some((field) => field.ai);
30
30
  });
31
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);
32
+ const { admin, jobs: definitions } = projectAdmin(project, db);
33
33
  await checkJobsSchema(db);
34
34
  return {
35
35
  definitions,
36
36
  runtime: await startJobs({
37
37
  db,
38
38
  jobs: definitions,
39
+ decryptField: admin.decryptField,
39
40
  ...project.mailer && { mailer: project.mailer },
40
41
  onError: (error) => console.error(error)
41
42
  })
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@protobase/cli",
3
- "version": "0.57.6",
3
+ "version": "0.58.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": {
@@ -42,12 +42,12 @@
42
42
  "dependencies": {
43
43
  "@better-auth/core": "^1.7.12",
44
44
  "@hono/node-server": "^2.1.3",
45
- "@protobase/config": "0.57.6",
46
- "@protobase/layout": "0.57.6",
47
- "@protobase/query": "0.57.6",
48
- "@protobase/schema": "0.57.6",
49
- "@protobase/server": "0.57.6",
50
- "@protobase/ui": "0.57.6",
45
+ "@protobase/config": "0.58.0",
46
+ "@protobase/layout": "0.58.0",
47
+ "@protobase/query": "0.58.0",
48
+ "@protobase/schema": "0.58.0",
49
+ "@protobase/server": "0.58.0",
50
+ "@protobase/ui": "0.58.0",
51
51
  "@react-email/render": "^2.1.0",
52
52
  "@scalar/hono-api-reference": "^0.12.10",
53
53
  "@tailwindcss/vite": "^4.3.3",
@@ -21,6 +21,7 @@ Describe the tables of an existing Postgres database as resources, with their fi
21
21
  - [Recent strong sign-in](../reference/data-config.md#recent-strong-sign-in): A sensitive field can ask for a passkey or a second step in the last minutes before it is revealed. The record page asks the person to confirm it's them, then shows it.
22
22
  - [Computed reveals](../reference/data-config.md#computed-reveals) (beta): A secret kept elsewhere, or built from several values, is revealed like a sensitive field, with the same access, audit and recent sign-in check, but made when it is asked for.
23
23
  - [Encrypted fields](../reference/data-config.md#encrypted-fields): A sensitive text field is encrypted with the app's own key before it is stored, so dumps, backups and SQL readers see only ciphertext. A command encrypts the values a column already holds.
24
+ - [Encrypted fields in jobs and functions](../reference/data-config.md#reading-in-jobs-and-functions) (beta): A job or an API function reads an encrypted value the app must use itself, such as a share's password, and each read is audited like a reveal.
24
25
  - [Encryption key rotation](../reference/data-config.md#the-key): The encryption key can be replaced without downtime. CLI commands re-encrypt stored values and report what is left.
25
26
  - [Schema API reference](../reference/data-config.md#schema-api-reference): Every builder, option and type of `@protobase/schema` has a doc comment, shown in your editor and in the generated API reference, and the types the builders return are exported for helpers of your own.
26
27
  - [Extending a config](../reference/cli.md#extending-a-config): A project can merge other configs and UI configs into its own. A name defined twice is an error, never a silent override.
@@ -50,6 +50,7 @@ The second argument of a handler (`c.env` in a [Hono app](#hono-apps)):
50
50
  | `records` | The app's resources, acting as the caller: `list`, `get`, `create`, `update`, `delete`, `softDeleteField`. |
51
51
  | `db` | The Kysely instance itself. It skips every access rule, row filter and tenant scope. |
52
52
  | `caller()` | Resolves to `{ session, records }`. Public functions use it to ask for a caller when they need one. |
53
+ | `decryptField(resource, key, field)` | An [encrypted field](../reference/data-config.md#reading-in-jobs-and-functions)'s value, skipping access rules like `db`; each read is audited, naming the function and the caller. |
53
54
  | `acceptWebSocket(events)` | Answers a WebSocket upgrade with a socket; return its response. See [WebSockets](#websockets). |
54
55
  | `name` | The function's name. |
55
56
 
@@ -123,8 +123,9 @@ Before answering, the server publishes an audit event to the app's audit queue,
123
123
  type AuditEvent = {
124
124
  type: 'field.revealed'
125
125
  at: string // ISO 8601
126
- actor: { id: string | number; roles: string[] }
127
- tenant?: string | number // the caller's organization, for tenant-scoped resources
126
+ actor?: { id: string | number; roles: string[] } // absent when a job read the value
127
+ reader?: { job: string } | { function: string } // who read it with decryptField; absent for a reveal
128
+ tenant?: string | number // the caller's organization (the record's for decryptField), for tenant-scoped resources
128
129
  resource: string
129
130
  recordKey: string // as in the record's URL
130
131
  field: string
@@ -132,6 +133,8 @@ type AuditEvent = {
132
133
  }
133
134
  ```
134
135
 
136
+ A job or API function that reads an encrypted field with [`decryptField`](data-config.md#reading-in-jobs-and-functions) publishes the same event, with `reader` set and, for a job, no `actor` or `origin` headers.
137
+
135
138
  With [organizations](auth.md#organizations), someone with a global role switching into an organization they are not a member of publishes `{ type: 'organization.entered', at, actor, organization, origin }` first, where `actor.roles` are their global roles; a failed publish refuses the switch. `AuditEvent` is the union of the two.
136
139
 
137
140
  ### Audit events
@@ -255,7 +255,7 @@ resource('apps')
255
255
 
256
256
  ## Encrypted fields
257
257
 
258
- An encrypted field is a [sensitive field](#sensitive-fields) whose value the server encrypts before it reaches the database, so a dump, a backup, a replica, a realtime subscriber or anyone running SQL sees only ciphertext. Only its reveal decrypts it, for a caller who may read the record and the field, and every reveal is audited. Every API write encrypts it: the record form, REST, batches, imports, [public forms](forms.md) and `records` in [API functions](../guides/api-functions.md). The [Supabase-compatible REST API](supabase-rest.md#encrypted-fields) refuses to write or filter it.
258
+ An encrypted field is a [sensitive field](#sensitive-fields) whose value the server encrypts before it reaches the database, so a dump, a backup, a replica, a realtime subscriber or anyone running SQL sees only ciphertext. Only its reveal decrypts it, for a caller who may read the record and the field, and your own [jobs and functions](#reading-in-jobs-and-functions) on the server; every read is audited. Every API write encrypts it: the record form, REST, batches, imports, [public forms](forms.md) and `records` in [API functions](../guides/api-functions.md). The [Supabase-compatible REST API](supabase-rest.md#encrypted-fields) refuses to write or filter it.
259
259
 
260
260
  ```ts
261
261
  api_key: f.text().encrypted().access({ read: roles.can('keys.read') }),
@@ -275,6 +275,18 @@ When you change a `.sensitive()` field to `.encrypted()`, change every rule, val
275
275
 
276
276
  No other field of the resource may be over the column, as its own or as a relation's column, and another resource over the same table must leave the column out, or declare it `.encrypted()` too with the same `.table()`, tenant and key; the server does not start otherwise. A resource over a view of the table cannot be checked, so leave the column out of the view itself, not only out of the resource.
277
277
 
278
+ ### Reading in jobs and functions
279
+
280
+ A value the app must use itself, such as the password of a backup share, is read on the server with `decryptField(resource, key, field)` of a [job's](jobs.md) context and of a protected [API function's](../guides/api-functions.md#the-context):
281
+
282
+ ```ts
283
+ const password = await context.decryptField('backup_targets', target.id, 'password')
284
+ ```
285
+
286
+ It returns the cleartext, or `null` when the field is empty. Like the context's `db`, it skips access rules, row filters and tenant scope, and reads only live records. `key` is the record's key, or its parts in the order of a composite key. A resource that is unknown, a field that does not exist or is not `.encrypted()`, and a record that does not exist are errors that name them, as is a value that does not decrypt.
287
+
288
+ Every read publishes a `field.revealed` [audit event](api.md#sensitive-fields-and-audit), whose `reader` names the job (`{ job }`, with no `actor`) or the function (`{ function }`, with its caller as `actor`), and the value is only returned once the event is published: when publishing fails, so does the read. A public function has no `decryptField`, since anyone may call it: enqueue a job that reads the value instead. No HTTP request reaches it.
289
+
278
290
  ### The key
279
291
 
280
292
  The app's key is `PROTOBASE_ENCRYPTION_KEY`: 32 random bytes in base64, such as the output of `openssl rand -base64 32`. It is its own: never the app's `secret` (`PROTOBASE_SECRET`) or an API key, nor derived from one, so rotating those loses nothing. Keep it apart from the database's backups, since whoever holds both reads every value. The server does not start with an encrypted field and no key, or a key that is not 32 bytes in base64.
@@ -53,7 +53,7 @@ export default (async ({ invoiceId }, { db }) => {
53
53
  | its name | Letters, digits, `_`, `-`, `.` and `/`; the file `jobs/<name>.ts` holds what it does. |
54
54
  | `description` | What the job does, for the people who read the config. |
55
55
  | `arguments` | Its arguments by name, each a [field type](data-config.md#field-types) such as `'integer'` or `'bigint'`, or the type with options: `{ type: 'enum', values: ['day', 'week'] }`, `{ type: 'text', min: 1 }`, `{ type: 'integer', optional: true }`, `{ type: 'decimal', precision: 8, scale: 2 }`. Each value is checked as a field of that type checks it, when the job is enqueued and again before it runs, and no other argument is taken. A payload refused at run time fails the job for good. The same is JSON in a `protobase.config.json`. |
56
- | the default export of `jobs/<name>.ts` | `(payload, { db, job, signal, mail, sendNotification })`, typed with `JobRun<Payload>` from `@protobase/server`, its payload `JobPayload<typeof config, '<name>'>` (see [Typed names and payloads](#typed-names-and-payloads)): the work. `db` is the app's pool: the enqueuer's transaction has committed by then. `job` holds its `id`, `name`, `attempt`, `maxAttempts` and `startedOn`, when this try took it. `signal` aborts when the try times out or the worker shuts down. `mail` sends mail, see [Mail](#mail). `sendNotification` sends a notification, see [Sending](notifications.md#sending); inside a transaction of `db`, a try that throws in it queues nothing. It is `undefined` in an app without [`notifications`](config/jobs-and-events.md#notifications). |
56
+ | the default export of `jobs/<name>.ts` | `(payload, { db, job, signal, decryptField, mail, sendNotification })`, typed with `JobRun<Payload>` from `@protobase/server`, its payload `JobPayload<typeof config, '<name>'>` (see [Typed names and payloads](#typed-names-and-payloads)): the work. `db` is the app's pool: the enqueuer's transaction has committed by then. `job` holds its `id`, `name`, `attempt`, `maxAttempts` and `startedOn`, when this try took it. `signal` aborts when the try times out or the worker shuts down. `decryptField(resource, key, field)` reads an encrypted field's value, audited, see [Reading in jobs and functions](data-config.md#reading-in-jobs-and-functions). `mail` sends mail, see [Mail](#mail). `sendNotification` sends a notification, see [Sending](notifications.md#sending); inside a transaction of `db`, a try that throws in it queues nothing. It is `undefined` in an app without [`notifications`](config/jobs-and-events.md#notifications). |
57
57
  | `maxAttempts` | Tries before the job is kept as failed; default 10. |
58
58
  | `retryDelay`, `maxRetryDelay` | The wait before the first retry, doubling with each one (with some jitter), and its ceiling; default `2s` and `1 hour`. Milliseconds or text such as `30 seconds`. |
59
59
  | `timeout` | How long one try may run before it counts as failed; default `15 minutes`. |