@protobase/server 0.133.1 → 0.133.2
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/api-keys/caller-settings.js +10 -5
- package/dist/batch/run-batch.js +2 -2
- package/dist/changes/actor.js +7 -6
- package/dist/changes/capture-schema-sql.d.ts +2 -1
- package/dist/changes/capture-schema-sql.js +2 -1
- package/dist/changes/events-options.d.ts +5 -0
- package/dist/changes/logical-message.js +17 -0
- package/dist/changes/stream/schema-messages-sql.js +42 -0
- package/dist/changes/stream/stream-module.js +48 -0
- package/dist/comments/post-comment.js +2 -1
- package/dist/package.js +1 -1
- package/dist/rest/rest-context.js +4 -2
- package/dist/schema-modules/modules.d.ts +2 -1
- package/dist/schema-modules/modules.js +2 -0
- package/dist/schema-modules/needed-modules.js +1 -0
- package/dist/storage/storage-db.js +6 -7
- package/dist/transactions.js +16 -6
- package/dist/write-pipeline.js +2 -2
- package/package.json +6 -6
|
@@ -1,4 +1,12 @@
|
|
|
1
1
|
//#region src/api-keys/caller-settings.ts
|
|
2
|
+
/** Who a request admitted by key writes as: its signed-in user, or none for the anonymous and the app's own key. */
|
|
3
|
+
const callerActor = (caller) => {
|
|
4
|
+
const sub = caller.role === "authenticated" ? caller.claims.sub : void 0;
|
|
5
|
+
return sub === void 0 ? void 0 : {
|
|
6
|
+
id: sub,
|
|
7
|
+
roles: []
|
|
8
|
+
};
|
|
9
|
+
};
|
|
2
10
|
/**
|
|
3
11
|
* The transaction settings a request admitted by key (`/api/sb/rest/v1`, `/api/sb/storage/v1`) runs with, as Supabase's PostgREST
|
|
4
12
|
* sets them: `role`, which is `SET LOCAL ROLE`, to `anon` or `authenticated`, and the caller's claims in `request.jwt.claims` and its user in
|
|
@@ -9,10 +17,7 @@
|
|
|
9
17
|
const callerSettings = (caller, actorSetting) => {
|
|
10
18
|
const role = caller.role === "app" ? "service_role" : caller.role;
|
|
11
19
|
const sub = caller.role === "authenticated" ? caller.claims.sub : void 0;
|
|
12
|
-
const actor =
|
|
13
|
-
id: sub,
|
|
14
|
-
roles: []
|
|
15
|
-
};
|
|
20
|
+
const actor = callerActor(caller);
|
|
16
21
|
return {
|
|
17
22
|
...caller.role !== "app" && { role: caller.role },
|
|
18
23
|
"request.jwt.claims": JSON.stringify({
|
|
@@ -24,4 +29,4 @@ const callerSettings = (caller, actorSetting) => {
|
|
|
24
29
|
};
|
|
25
30
|
};
|
|
26
31
|
//#endregion
|
|
27
|
-
export { callerSettings };
|
|
32
|
+
export { callerActor, callerSettings };
|
package/dist/batch/run-batch.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { HttpProblem, badRequest, notFound } from "../problem.js";
|
|
2
2
|
import { requestAccess } from "../request-access.js";
|
|
3
3
|
import { writeTransaction } from "../transactions.js";
|
|
4
|
-
import { actorSettings, wakeDispatch } from "../changes/actor.js";
|
|
4
|
+
import { actorSettings, sessionActor, wakeDispatch } from "../changes/actor.js";
|
|
5
5
|
import { problemOf } from "../error-handler.js";
|
|
6
6
|
import { once } from "../idempotency.js";
|
|
7
7
|
import { scheduleRemovedFiles } from "../files/attach.js";
|
|
@@ -151,7 +151,7 @@ const runBatch = async (deps, session, operations, idempotency) => {
|
|
|
151
151
|
throw atOperation(deps, error, index, operation);
|
|
152
152
|
}
|
|
153
153
|
return results;
|
|
154
|
-
}), actorSettings(deps, session)));
|
|
154
|
+
}), actorSettings(deps, session), sessionActor(session)));
|
|
155
155
|
await scheduleRemovedFiles(deps, removed);
|
|
156
156
|
wakeDispatch(deps);
|
|
157
157
|
return written;
|
package/dist/changes/actor.js
CHANGED
|
@@ -1,18 +1,19 @@
|
|
|
1
1
|
import { runningJobs } from "../jobs/current.js";
|
|
2
2
|
import { dispatchJobName } from "./capture-sql.js";
|
|
3
3
|
//#region src/changes/actor.ts
|
|
4
|
+
/** Who writes in a session's write transaction: the actor its `protobase_actor` message and the change capture name. */
|
|
5
|
+
const sessionActor = (session) => ({
|
|
6
|
+
id: session.user.id,
|
|
7
|
+
roles: session.user.roles
|
|
8
|
+
});
|
|
4
9
|
/** The setting a write transaction sets so the change capture records who wrote, when a resource has events or history. */
|
|
5
10
|
const actorSettings = (deps, session) => {
|
|
6
11
|
if (!deps.actorSetting) return {};
|
|
7
|
-
|
|
8
|
-
id: session.user.id,
|
|
9
|
-
roles: session.user.roles
|
|
10
|
-
};
|
|
11
|
-
return { [deps.actorSetting]: JSON.stringify(actor) };
|
|
12
|
+
return { [deps.actorSetting]: JSON.stringify(sessionActor(session)) };
|
|
12
13
|
};
|
|
13
14
|
/** After a write committed: has this process's dispatcher deliver its events now, rather than at its next poll. */
|
|
14
15
|
const wakeDispatch = (deps) => {
|
|
15
16
|
if (deps.actorSetting) runningJobs()?.wake(dispatchJobName);
|
|
16
17
|
};
|
|
17
18
|
//#endregion
|
|
18
|
-
export { actorSettings, wakeDispatch };
|
|
19
|
+
export { actorSettings, sessionActor, wakeDispatch };
|
|
@@ -3,7 +3,8 @@ import { ResourceModel } from "@protobase/schema";
|
|
|
3
3
|
//#region src/changes/capture-schema-sql.d.ts
|
|
4
4
|
/**
|
|
5
5
|
* The change capture `models` need, as a migration writes it on a database without it: the change capture module, and
|
|
6
|
-
* the history and notices tables when a resource has history or live updates
|
|
6
|
+
* the history and notices tables when a resource has history or live updates, and with `events.stream` the schema
|
|
7
|
+
* change messages. The jobs schema it enqueues into is not
|
|
7
8
|
* part of it (`jobsSchemaSql`), nor are the rollups and formulas of `models` (`computedSchemaSql`).
|
|
8
9
|
*/
|
|
9
10
|
export declare const captureSchemaSql: (models: readonly ResourceModel[], events?: EventsOptions) => string;
|
|
@@ -4,7 +4,8 @@ import { neededModules } from "../schema-modules/needed-modules.js";
|
|
|
4
4
|
//#region src/changes/capture-schema-sql.ts
|
|
5
5
|
/**
|
|
6
6
|
* The change capture `models` need, as a migration writes it on a database without it: the change capture module, and
|
|
7
|
-
* the history and notices tables when a resource has history or live updates
|
|
7
|
+
* the history and notices tables when a resource has history or live updates, and with `events.stream` the schema
|
|
8
|
+
* change messages. The jobs schema it enqueues into is not
|
|
8
9
|
* part of it (`jobsSchemaSql`), nor are the rollups and formulas of `models` (`computedSchemaSql`).
|
|
9
10
|
*/
|
|
10
11
|
const captureSchemaSql = (models, events) => {
|
|
@@ -6,5 +6,10 @@ export type EventsOptions = {
|
|
|
6
6
|
* (`changes.actor`, `changes.capture`); default `changes`. Rename it when the database already has a schema by that name.
|
|
7
7
|
*/
|
|
8
8
|
schema?: string;
|
|
9
|
+
/**
|
|
10
|
+
* Whether `protobase migration` writes the event triggers that put schema changes into the WAL as logical messages,
|
|
11
|
+
* which the change stream reads in order with the rows. Creating them takes a superuser.
|
|
12
|
+
*/
|
|
13
|
+
stream?: boolean;
|
|
9
14
|
};
|
|
10
15
|
//#endregion
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { sql } from "kysely";
|
|
2
|
+
//#region src/changes/logical-message.ts
|
|
3
|
+
/** The prefix of the message every API write transaction emits with who wrote, as the change stream reads it. */
|
|
4
|
+
const actorPrefix = "protobase_actor";
|
|
5
|
+
/** The prefix of the message emitted for each command a schema change runs (`ddl_command_end`). */
|
|
6
|
+
const ddlPrefix = "protobase_ddl";
|
|
7
|
+
/** The prefix of the message emitted for each object a schema change drops (`sql_drop`). */
|
|
8
|
+
const dropPrefix = "protobase_drop";
|
|
9
|
+
/**
|
|
10
|
+
* A transactional logical decoding message, as an expression of a select: it reaches the WAL with the transaction's
|
|
11
|
+
* rows, in their order, and only when the transaction commits. The `text` cast picks the text overload over bytea's.
|
|
12
|
+
*/
|
|
13
|
+
const emitMessage = (prefix, content) => sql`pg_logical_emit_message(true, ${prefix}, ${content}::text)`;
|
|
14
|
+
/** The same message in the SQL of a function body: `content` is an SQL expression of type text. */
|
|
15
|
+
const emitMessageSql = (prefix, content) => `pg_logical_emit_message(true, '${prefix}', ${content})`;
|
|
16
|
+
//#endregion
|
|
17
|
+
export { actorPrefix, ddlPrefix, dropPrefix, emitMessage, emitMessageSql };
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { ddlPrefix, dropPrefix, emitMessageSql } from "../logical-message.js";
|
|
2
|
+
import { quoteIdent } from "../capture-sql.js";
|
|
3
|
+
//#region src/changes/stream/schema-messages-sql.ts
|
|
4
|
+
/** The function the schema change event triggers call, in the changes schema. */
|
|
5
|
+
const schemaMessagesFunction = "emit_schema_messages";
|
|
6
|
+
/** The event triggers' names, one per event; they are the database's own, so the changes schema prefixes them. */
|
|
7
|
+
const schemaMessagesTriggers = (schema) => ({
|
|
8
|
+
ddl: `${schema}_ddl_messages`,
|
|
9
|
+
drop: `${schema}_drop_messages`
|
|
10
|
+
});
|
|
11
|
+
/**
|
|
12
|
+
* The function and the event triggers that write each schema change into the WAL as logical messages, in its
|
|
13
|
+
* transaction, so a reader of the change stream meets them in order with the rows: a `protobase_ddl` message for each
|
|
14
|
+
* command a statement such as ALTER TABLE ran (`ddl_command_end`), a `protobase_drop` message for each object a
|
|
15
|
+
* statement dropped (`sql_drop`), each a JSON object of Postgres's own fields. Creating an event trigger takes a
|
|
16
|
+
* superuser, so applying it as another role fails with Postgres's own error.
|
|
17
|
+
*/
|
|
18
|
+
const schemaMessagesSql = (schema) => {
|
|
19
|
+
const fn = `${quoteIdent(schema)}.${quoteIdent(schemaMessagesFunction)}`;
|
|
20
|
+
const { ddl, drop } = schemaMessagesTriggers(schema);
|
|
21
|
+
return `create or replace function ${fn}() returns event_trigger language plpgsql as $$
|
|
22
|
+
declare
|
|
23
|
+
command record;
|
|
24
|
+
begin
|
|
25
|
+
if tg_event = 'sql_drop' then
|
|
26
|
+
for command in select object_type, schema_name, object_identity from pg_event_trigger_dropped_objects() loop
|
|
27
|
+
perform ${emitMessageSql(dropPrefix, "json_build_object('object_type', command.object_type, 'schema_name', command.schema_name, 'object_identity', command.object_identity)::text")};
|
|
28
|
+
end loop;
|
|
29
|
+
return;
|
|
30
|
+
end if;
|
|
31
|
+
for command in select command_tag, object_type, schema_name, object_identity from pg_event_trigger_ddl_commands() loop
|
|
32
|
+
perform ${emitMessageSql(ddlPrefix, "json_build_object('command_tag', command.command_tag, 'object_type', command.object_type, 'schema_name', command.schema_name, 'object_identity', command.object_identity)::text")};
|
|
33
|
+
end loop;
|
|
34
|
+
end
|
|
35
|
+
$$;
|
|
36
|
+
drop event trigger if exists ${quoteIdent(ddl)};
|
|
37
|
+
create event trigger ${quoteIdent(ddl)} on ddl_command_end execute function ${fn}();
|
|
38
|
+
drop event trigger if exists ${quoteIdent(drop)};
|
|
39
|
+
create event trigger ${quoteIdent(drop)} on sql_drop execute function ${fn}();`;
|
|
40
|
+
};
|
|
41
|
+
//#endregion
|
|
42
|
+
export { schemaMessagesFunction, schemaMessagesSql, schemaMessagesTriggers };
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { quoteIdent } from "../capture-sql.js";
|
|
2
|
+
import { schemaMessagesFunction, schemaMessagesSql, schemaMessagesTriggers } from "./schema-messages-sql.js";
|
|
3
|
+
import { sql } from "kysely";
|
|
4
|
+
//#region src/changes/stream/stream-module.ts
|
|
5
|
+
/**
|
|
6
|
+
* The schema change messages as the database has them: step 1 once the function is there, and the triggers at version 1
|
|
7
|
+
* when both event triggers are there, enabled and calling it, else at none, so they are written again.
|
|
8
|
+
*/
|
|
9
|
+
const readStream = async (db, { names }) => {
|
|
10
|
+
const schema = names.schema;
|
|
11
|
+
const { ddl, drop } = schemaMessagesTriggers(schema);
|
|
12
|
+
const fn = `${quoteIdent(schema)}.${quoteIdent(schemaMessagesFunction)}`;
|
|
13
|
+
const { rows } = await sql`select to_regprocedure(${`${fn}()`}) is not null as found,
|
|
14
|
+
(select count(*)::int from pg_event_trigger where evtname = any(${[ddl, drop]}::text[]) and evtenabled in ('O', 'A') and evtfoid = to_regprocedure(${`${fn}()`})) as triggers`.execute(db);
|
|
15
|
+
const { found, triggers } = rows[0];
|
|
16
|
+
if (!found) return;
|
|
17
|
+
return {
|
|
18
|
+
lastStep: 1,
|
|
19
|
+
replaceables: triggers === 2 ? { schema_messages: 1 } : {},
|
|
20
|
+
names
|
|
21
|
+
};
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* What the change stream needs in the app's database besides the WAL itself, with `events.stream`: the event triggers
|
|
25
|
+
* that write schema changes as logical messages (`schemaMessagesSql`), in the schema `events` names (`names.schema`).
|
|
26
|
+
*/
|
|
27
|
+
const streamModule = {
|
|
28
|
+
label: "the schema change messages",
|
|
29
|
+
title: "The schema change messages the change stream reads",
|
|
30
|
+
steps: [{
|
|
31
|
+
step: 1,
|
|
32
|
+
name: "schema",
|
|
33
|
+
title: "The changes schema",
|
|
34
|
+
additive: true,
|
|
35
|
+
sql: (names) => `create schema if not exists ${quoteIdent(names.schema)};`
|
|
36
|
+
}],
|
|
37
|
+
replaceables: [{
|
|
38
|
+
name: "schema_messages",
|
|
39
|
+
title: "The event triggers that write schema changes as logical messages",
|
|
40
|
+
version: 1,
|
|
41
|
+
compatibleFrom: 1,
|
|
42
|
+
sql: (names) => schemaMessagesSql(names.schema)
|
|
43
|
+
}],
|
|
44
|
+
resourceReplaceables: [],
|
|
45
|
+
read: readStream
|
|
46
|
+
};
|
|
47
|
+
//#endregion
|
|
48
|
+
export { streamModule };
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { badRequest, notFound } from "../problem.js";
|
|
2
2
|
import { tenantScope } from "../tenant.js";
|
|
3
3
|
import { writeTransaction } from "../transactions.js";
|
|
4
|
+
import { sessionActor } from "../changes/actor.js";
|
|
4
5
|
import { commentAccess, mayRead, readableRecord, requireSignedIn, userSessions } from "./comment-access.js";
|
|
5
6
|
import { parseCommentInput } from "./comment-input.js";
|
|
6
7
|
import { commentColumns, commentItem, namedUsers } from "./comment-item.js";
|
|
@@ -51,7 +52,7 @@ const postComment = async (deps, entry, session, key, body, requestURL) => {
|
|
|
51
52
|
options: { key: `${inserted.id}:${user}` }
|
|
52
53
|
})));
|
|
53
54
|
return inserted;
|
|
54
|
-
});
|
|
55
|
+
}, {}, sessionActor(session));
|
|
55
56
|
if (!row) throw notFound(`No ${entry.name} with this key`);
|
|
56
57
|
const names = deps.comments?.users ? await deps.comments.users.names(namedUsers([row])) : {};
|
|
57
58
|
return commentItem(row, names);
|
package/dist/package.js
CHANGED
|
@@ -2,15 +2,17 @@ import { readTransaction, writeTransaction } from "../transactions.js";
|
|
|
2
2
|
import { wakeDispatch } from "../changes/actor.js";
|
|
3
3
|
import { callerSettings } from "./caller-settings.js";
|
|
4
4
|
import { restError } from "./rest-error.js";
|
|
5
|
+
import { callerActor } from "../api-keys/caller-settings.js";
|
|
5
6
|
//#region src/rest/rest-context.ts
|
|
6
7
|
/**
|
|
7
8
|
* Runs `work` in one transaction as the request's caller (see `callerSettings`), under the statement timeout;
|
|
8
9
|
* read-only for a read, so a function that writes fails as on PostgREST.
|
|
9
10
|
*/
|
|
10
11
|
const asCaller = async (deps, c, readOnly, work) => {
|
|
11
|
-
const
|
|
12
|
+
const caller = c.get("caller");
|
|
13
|
+
const settings = callerSettings(caller, deps.actorSetting);
|
|
12
14
|
if (readOnly) return readTransaction(deps.db, deps.statementTimeoutMs, work, settings);
|
|
13
|
-
const result = await writeTransaction(deps.db, deps.statementTimeoutMs, work, settings);
|
|
15
|
+
const result = await writeTransaction(deps.db, deps.statementTimeoutMs, work, settings, callerActor(caller));
|
|
14
16
|
wakeDispatch(deps);
|
|
15
17
|
return result;
|
|
16
18
|
};
|
|
@@ -5,6 +5,7 @@ export declare const schemaModules: {
|
|
|
5
5
|
mail_feedback: SchemaModule;
|
|
6
6
|
computed: SchemaModule;
|
|
7
7
|
changes: SchemaModule;
|
|
8
|
+
change_stream: SchemaModule;
|
|
8
9
|
history: SchemaModule;
|
|
9
10
|
derived: SchemaModule;
|
|
10
11
|
notices: SchemaModule;
|
|
@@ -31,7 +32,7 @@ export type ModulesModel = Partial<Record<SchemaModuleId, ModuleModel>>;
|
|
|
31
32
|
/** The schema modules an app needs, each with what it gives the module (`neededModules`), in their order. */
|
|
32
33
|
export type ModuleTargets = Partial<Record<SchemaModuleId, ModuleInput>>;
|
|
33
34
|
/** The modules of `targets`, in their order. */
|
|
34
|
-
export declare const targetIds: (targets: ModuleTargets) => ("status" | "query" | "user_tables" | "history" | "comments" | "derived" | "jobs" | "changes" | "mail_feedback" | "calls" | "webhooks" | "webhook_delivery_log" | "workflows" | "workflow_custom_steps" | "computed" | "notices" | "workflow_storage" | "notification_devices" | "notification_history" | "relay" | "realtime_relay" | "channel_sends" | "unsubscribes")[];
|
|
35
|
+
export declare const targetIds: (targets: ModuleTargets) => ("status" | "query" | "user_tables" | "history" | "comments" | "derived" | "jobs" | "changes" | "mail_feedback" | "calls" | "webhooks" | "webhook_delivery_log" | "workflows" | "workflow_custom_steps" | "computed" | "change_stream" | "notices" | "workflow_storage" | "notification_devices" | "notification_history" | "relay" | "realtime_relay" | "channel_sends" | "unsubscribes")[];
|
|
35
36
|
/** The model of each module of `targets` this version of Protobase reads. */
|
|
36
37
|
export declare const latestModules: (targets: ModuleTargets) => ModulesModel;
|
|
37
38
|
//#endregion
|
|
@@ -3,6 +3,7 @@ import { channelSendsModule } from "../channels/sends-module.js";
|
|
|
3
3
|
import { computedModule } from "../computed/computed-module.js";
|
|
4
4
|
import { derivedModule } from "../derived/derived-module.js";
|
|
5
5
|
import { changesModule } from "../changes/changes-module.js";
|
|
6
|
+
import { streamModule } from "../changes/stream/stream-module.js";
|
|
6
7
|
import { commentsModule } from "../comments/comments-module.js";
|
|
7
8
|
import { historyModule } from "../history/history-module.js";
|
|
8
9
|
import { latestModel } from "./module-difference.js";
|
|
@@ -28,6 +29,7 @@ const schemaModules = {
|
|
|
28
29
|
mail_feedback: mailFeedbackModule,
|
|
29
30
|
computed: computedModule,
|
|
30
31
|
changes: changesModule,
|
|
32
|
+
change_stream: streamModule,
|
|
31
33
|
history: historyModule,
|
|
32
34
|
derived: derivedModule,
|
|
33
35
|
notices: noticesModule,
|
|
@@ -40,6 +40,7 @@ const neededModules = (app) => {
|
|
|
40
40
|
...app.mailFeedback && { mail_feedback: noInput },
|
|
41
41
|
...app.models.some(computedOn) && { computed: computedInput(app.models) },
|
|
42
42
|
...changes && { changes: changesInput(captured, app.events) },
|
|
43
|
+
...app.events?.stream && { change_stream: events },
|
|
43
44
|
...app.models.some((model) => model.history) && { history: events },
|
|
44
45
|
...derivedNames(app.models).length > 0 && { derived: derivedInput(app.models) },
|
|
45
46
|
...(app.models.some((model) => model.live) || Boolean(app.calls)) && { notices: events },
|
|
@@ -1,21 +1,20 @@
|
|
|
1
|
-
import { readTransaction, writeTransaction } from "../transactions.js";
|
|
2
|
-
import { callerSettings } from "../api-keys/caller-settings.js";
|
|
3
|
-
import { sql } from "kysely";
|
|
1
|
+
import { applySettings, readTransaction, writeTransaction } from "../transactions.js";
|
|
2
|
+
import { callerActor, callerSettings } from "../api-keys/caller-settings.js";
|
|
4
3
|
//#region src/storage/storage-db.ts
|
|
5
4
|
/**
|
|
6
5
|
* Makes the rest of the transaction run as `caller`: its role and claims, as `/api/sb/rest/v1` sets them (see
|
|
7
6
|
* `callerSettings`), so the restored `storage.objects` and `storage.buckets` policies decide what it reads and writes.
|
|
8
|
-
* What runs before it runs as the app's own role, which owns the tables.
|
|
7
|
+
* What runs before it runs as the app's own role, which owns the tables. A signed-in caller's `protobase_actor`
|
|
8
|
+
* message goes with it, in the same statement.
|
|
9
9
|
*/
|
|
10
10
|
const becomeCaller = async (trx, deps, caller) => {
|
|
11
|
-
|
|
12
|
-
await sql`select ${sql.join(settings.map(([name, value]) => sql`set_config(${name}, ${value}, true)`))}`.execute(trx);
|
|
11
|
+
await applySettings(trx, callerSettings(caller, deps.actorSetting), callerActor(caller));
|
|
13
12
|
};
|
|
14
13
|
/** Runs `work` in one transaction as `caller`, read-only when `readOnly`. */
|
|
15
14
|
const asCaller = (deps, caller, readOnly, work) => {
|
|
16
15
|
const settings = callerSettings(caller, deps.actorSetting);
|
|
17
16
|
if (readOnly) return readTransaction(deps.db, deps.statementTimeoutMs, work, settings);
|
|
18
|
-
return writeTransaction(deps.db, deps.statementTimeoutMs, work, settings);
|
|
17
|
+
return writeTransaction(deps.db, deps.statementTimeoutMs, work, settings, callerActor(caller));
|
|
19
18
|
};
|
|
20
19
|
/** Runs `work` in one transaction as the app's own role, for what no caller decides: public objects, signed URLs. */
|
|
21
20
|
const asApp = (deps, work) => writeTransaction(deps.db, deps.statementTimeoutMs, work);
|
package/dist/transactions.js
CHANGED
|
@@ -1,15 +1,25 @@
|
|
|
1
|
+
import { actorPrefix, emitMessage } from "./changes/logical-message.js";
|
|
1
2
|
import { sql } from "kysely";
|
|
2
3
|
//#region src/transactions.ts
|
|
3
4
|
/**
|
|
5
|
+
* Sets `settings` for the rest of the open transaction and, given an `actor`, emits its `protobase_actor` message, so
|
|
6
|
+
* the change stream knows who wrote the rows that follow: one statement, one round trip.
|
|
7
|
+
*/
|
|
8
|
+
const applySettings = async (trx, settings, actor) => {
|
|
9
|
+
const parts = Object.entries(settings).map(([name, value]) => sql`set_config(${name}, ${value}, true)`);
|
|
10
|
+
if (actor) parts.push(sql`${emitMessage(actorPrefix, JSON.stringify(actor))} is not null`);
|
|
11
|
+
if (parts.length === 0) return;
|
|
12
|
+
await sql`select ${sql.join(parts)}`.execute(trx);
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
4
15
|
* One transaction whose statements Postgres cancels after `timeoutMs` (SET LOCAL statement_timeout), with `settings`
|
|
5
|
-
* set for it as well, all in one statement.
|
|
16
|
+
* set for it as well and `actor`'s message emitted (see `applySettings`), all in one statement.
|
|
6
17
|
*/
|
|
7
|
-
const writeTransaction = (db, timeoutMs, run, settings = {}) => db.transaction().execute(async (trx) => {
|
|
8
|
-
|
|
18
|
+
const writeTransaction = (db, timeoutMs, run, settings = {}, actor) => db.transaction().execute(async (trx) => {
|
|
19
|
+
await applySettings(trx, {
|
|
9
20
|
statement_timeout: String(timeoutMs),
|
|
10
21
|
...settings
|
|
11
|
-
});
|
|
12
|
-
await sql`select ${sql.join(all.map(([name, value]) => sql`set_config(${name}, ${value}, true)`))}`.execute(trx);
|
|
22
|
+
}, actor);
|
|
13
23
|
return run(trx);
|
|
14
24
|
});
|
|
15
25
|
/** Same, and Postgres refuses any write inside it. */
|
|
@@ -23,4 +33,4 @@ const snapshotReadTransaction = (db, timeoutMs, run) => db.transaction().setIsol
|
|
|
23
33
|
return run(trx);
|
|
24
34
|
});
|
|
25
35
|
//#endregion
|
|
26
|
-
export { readTransaction, snapshotReadTransaction, writeTransaction };
|
|
36
|
+
export { applySettings, readTransaction, snapshotReadTransaction, writeTransaction };
|
package/dist/write-pipeline.js
CHANGED
|
@@ -4,7 +4,7 @@ import { requestAccess, requireOperation, targetFilter, writableFields } from ".
|
|
|
4
4
|
import { writeTransaction } from "./transactions.js";
|
|
5
5
|
import { bindingOf, pendingBinding } from "./encryption/binding.js";
|
|
6
6
|
import { deleteRecord, fetchRecord, restoreRecord, updateRecord } from "./records.js";
|
|
7
|
-
import { actorSettings, wakeDispatch } from "./changes/actor.js";
|
|
7
|
+
import { actorSettings, sessionActor, wakeDispatch } from "./changes/actor.js";
|
|
8
8
|
import { isDatabaseError } from "./error-handler.js";
|
|
9
9
|
import { attachFiles, scheduleRemovedFiles } from "./files/attach.js";
|
|
10
10
|
import { assertWritten, checkParents, checkPrecondition, decide, denied, firstKeyField, keyOf, missing, notifier, shown } from "./write-shared.js";
|
|
@@ -150,7 +150,7 @@ const runWrite = async (deps, entry, session, request, form) => {
|
|
|
150
150
|
const result = await withDeclaredChecks(deps.registry, () => writeTransaction(deps.db, deps.statementTimeoutMs, async (trx) => {
|
|
151
151
|
await lockOrders(trx, locks);
|
|
152
152
|
return writeIn(trx, deps, access, request, removed);
|
|
153
|
-
}, actorSettings(deps, session)));
|
|
153
|
+
}, actorSettings(deps, session), sessionActor(session)));
|
|
154
154
|
await scheduleRemovedFiles(deps, removed);
|
|
155
155
|
wakeDispatch(deps);
|
|
156
156
|
return result;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@protobase/server",
|
|
3
|
-
"version": "0.133.
|
|
3
|
+
"version": "0.133.2",
|
|
4
4
|
"description": "The Protobase REST API as a Hono app, with Better Auth sign-in.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"repository": {
|
|
@@ -44,11 +44,11 @@
|
|
|
44
44
|
"@better-auth/sso": "^1.7.12",
|
|
45
45
|
"@livekit/protocol": "1.53.1",
|
|
46
46
|
"@modelcontextprotocol/server": "^2.3.1",
|
|
47
|
-
"@protobase/layout": "0.133.
|
|
48
|
-
"@protobase/query": "0.133.
|
|
49
|
-
"@protobase/schema": "0.133.
|
|
50
|
-
"@protobase/workflow-plugins": "0.133.
|
|
51
|
-
"@protobase/workflows": "0.133.
|
|
47
|
+
"@protobase/layout": "0.133.2",
|
|
48
|
+
"@protobase/query": "0.133.2",
|
|
49
|
+
"@protobase/schema": "0.133.2",
|
|
50
|
+
"@protobase/workflow-plugins": "0.133.2",
|
|
51
|
+
"@protobase/workflows": "0.133.2",
|
|
52
52
|
"@react-email/render": "^2.1.0",
|
|
53
53
|
"@react-pdf/primitives": "^4.4.0",
|
|
54
54
|
"@react-pdf/renderer": "^4.9.1",
|