@supacloud/db 0.6.0 → 0.7.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,5 +1,31 @@
1
1
  # @supacloud/db
2
2
 
3
+ ## Durable Commands
4
+
5
+ `createPostgresCommandStore(database)` implements the protocol's storage ports.
6
+ Use `createBunCommandDatabase` from `@supacloud/db/bun` for a native Bun SQL pool.
7
+ This package contains transaction/receipt SQL, Workflow binding, row validation
8
+ and input redaction. It does not own a recovery polling queue.
9
+
10
+ Execution factories moved to `@supacloud/commands`; pass `store` instead of
11
+ `database`. Errors moved to `CommandError` in `@supacloud/contracts`. Neither
12
+ remote sending nor command orchestration is re-exported here. The DB package
13
+ does not depend on the command runtime, including through its tests.
14
+
15
+ Install the existing PGMQ/Workflow runtime and updated commands-public SQL module,
16
+ then `COMMAND_PERSISTENCE_SQL` through a privileged application migration.
17
+ Existing unreleased v1 tables require `COMMAND_PERSISTENCE_UPGRADE_SQL`, with old
18
+ writers stopped; preserve operation identifiers during cutover.
19
+ The upgrade enqueues pending recovery using existing Workflow and removes prototype
20
+ lease/backoff columns. Use `createPostgresCommandStore(database, { submission })`
21
+ inside a submitted command's execute step to preserve its command ID and atomically
22
+ advance to reconciliation (external) or complete its Workflow (transactional).
23
+ `submission` carries commandId, stepId, messageId, attempt and workerId. The adapter
24
+ checks the current attempt, recorded tenant/actor/command, and original input.
25
+ Keep its schema private and supply domain authorization, JSON-stable input/result
26
+ decoders and a single-connection transaction adapter. Metadata cannot provide a
27
+ distributed transaction. See [migration, deployment and recovery](../../docs/command-migration.md).
28
+
3
29
  For reviewed deployment-time public SQL parameters, see
4
30
  [Controlled Migration Bindings](./MIGRATION_BINDINGS.md). This opt-in API preserves
5
31
  Drizzle v1 source/snapshot ownership and the existing executor's migration ledger.
@@ -0,0 +1,18 @@
1
+ import type { CommandStore, CommandRetentionStore } from "@supacloud/contracts";
2
+ export interface CommandTransaction {
3
+ query(sql: string, parameters?: readonly (string | number | boolean | null)[]): Promise<unknown>;
4
+ }
5
+ export interface CommandDatabase {
6
+ transaction<T>(run: (transaction: CommandTransaction) => Promise<T>): Promise<T>;
7
+ }
8
+ export interface CommandSubmissionBinding {
9
+ commandId: string;
10
+ stepId: string;
11
+ messageId: string;
12
+ attempt: number;
13
+ workerId: string;
14
+ }
15
+ /** PostgreSQL only: no remote sending, domain matching or recovery policy lives here. */
16
+ export declare function createPostgresCommandStore(database: CommandDatabase, options?: {
17
+ submission?: CommandSubmissionBinding;
18
+ }): CommandStore<CommandTransaction> & CommandRetentionStore;
@@ -0,0 +1,4 @@
1
+ import type { SQL } from "bun";
2
+ import type { CommandDatabase } from "./command-adapter";
3
+ /** The SQL pool owns one connection from BEGIN through COMMIT/ROLLBACK. */
4
+ export declare function createBunCommandDatabase(sql: SQL): CommandDatabase;
@@ -0,0 +1,19 @@
1
+ // src/command-bun.ts
2
+ function createBunCommandDatabase(sql) {
3
+ return {
4
+ async transaction(run) {
5
+ const result = await sql.begin(async (connection) => ({
6
+ value: await run({
7
+ query: async (text, parameters = []) => {
8
+ const rows = await connection.unsafe(text, [...parameters]);
9
+ return rows;
10
+ }
11
+ })
12
+ }));
13
+ return result.value;
14
+ }
15
+ };
16
+ }
17
+ export {
18
+ createBunCommandDatabase
19
+ };
@@ -0,0 +1,4 @@
1
+ /** Install through the application's normal migration flow, never during a request. */
2
+ export declare const COMMAND_PERSISTENCE_SQL = "\nDO $runtime$\nBEGIN\n IF to_regprocedure('supacloud_workflows.start_run(uuid,text,text,text,jsonb,integer)') IS NULL\n OR to_regprocedure('supacloud_commands.status(jsonb)') IS NULL THEN\n RAISE EXCEPTION 'Install the SupaCloud Commands/Workflow/PGMQ runtime before command persistence';\n END IF;\nEND\n$runtime$;\nCREATE SCHEMA IF NOT EXISTS supacloud_commands;\nREVOKE ALL ON SCHEMA supacloud_commands FROM PUBLIC;\nCREATE TABLE IF NOT EXISTS supacloud_commands.execution_receipts (\n tenant_id text NOT NULL,\n actor_id text NOT NULL,\n command text NOT NULL,\n operation_key text NOT NULL,\n kind text NOT NULL CHECK (kind IN ('transactional', 'external')),\n input_fingerprint text NOT NULL CHECK (input_fingerprint ~ '^[a-f0-9]{64}$'),\n input_payload text,\n dispatch_key uuid NOT NULL DEFAULT gen_random_uuid() UNIQUE,\n status text NOT NULL CHECK (status IN ('pending', 'unknown', 'confirmed')),\n audit_state text NOT NULL CHECK (audit_state IN ('pending', 'complete')),\n result jsonb,\n created_at timestamptz NOT NULL DEFAULT now(),\n updated_at timestamptz NOT NULL DEFAULT now(),\n PRIMARY KEY (tenant_id, actor_id, command, operation_key),\n CHECK ((status = 'confirmed') = (result IS NOT NULL)),\n CHECK (audit_state <> 'complete' OR status = 'confirmed'),\n CHECK (kind <> 'transactional' OR (status = 'confirmed' AND audit_state = 'complete'))\n);\nCREATE TABLE IF NOT EXISTS supacloud_commands.execution_audit (\n tenant_id text NOT NULL,\n actor_id text NOT NULL,\n command text NOT NULL,\n operation_key text NOT NULL,\n event text NOT NULL,\n details jsonb NOT NULL,\n created_at timestamptz NOT NULL DEFAULT now(),\n PRIMARY KEY (tenant_id, actor_id, command, operation_key),\n FOREIGN KEY (tenant_id, actor_id, command, operation_key)\n REFERENCES supacloud_commands.execution_receipts (tenant_id, actor_id, command, operation_key)\n);\nREVOKE ALL ON supacloud_commands.execution_receipts, supacloud_commands.execution_audit FROM PUBLIC;\nCREATE OR REPLACE FUNCTION supacloud_commands.enqueue_execution_recovery()\nRETURNS trigger LANGUAGE plpgsql SET search_path = '' AS $recovery$\nDECLARE submission jsonb;\nBEGIN\n PERFORM pg_advisory_xact_lock(hashtextextended(NEW.dispatch_key::text,0));\n SELECT supacloud_commands.snapshot(NEW.dispatch_key,false) INTO submission;\n IF submission IS NOT NULL THEN\n IF submission->>'commandType' IS DISTINCT FROM NEW.command\n OR submission->>'actorId' IS DISTINCT FROM NEW.actor_id\n OR submission->>'tenantId' IS DISTINCT FROM NEW.tenant_id\n OR NEW.operation_key <> NEW.dispatch_key::text THEN\n RAISE EXCEPTION 'SUPACLOUD_COMMAND_IDENTITY_CONFLICT';\n END IF;\n RETURN NEW;\n END IF;\n IF NEW.kind = 'external' AND (NEW.status <> 'confirmed' OR NEW.audit_state <> 'complete') THEN\n PERFORM supacloud_workflows.start_run(\n NEW.dispatch_key, 'supacloud.command.reconcile', '1', 'reconcile',\n jsonb_build_object('commandId', NEW.dispatch_key, 'tenantId', NEW.tenant_id,\n 'actorId', NEW.actor_id, 'command', NEW.command, 'operationId', NEW.operation_key),\n 20\n );\n END IF;\n RETURN NEW;\nEND\n$recovery$;\nCREATE OR REPLACE TRIGGER execution_recovery_enqueue\nAFTER INSERT ON supacloud_commands.execution_receipts\nFOR EACH ROW EXECUTE FUNCTION supacloud_commands.enqueue_execution_recovery();\n\nCREATE OR REPLACE FUNCTION supacloud_commands.execution_status(request jsonb)\nRETURNS jsonb LANGUAGE sql STABLE SECURITY DEFINER SET search_path = '' AS $status$\n SELECT jsonb_build_object('kind','execution','commandId',r.dispatch_key,\n 'execution',jsonb_build_object('tenantId',r.tenant_id,'actorId',r.actor_id,\n 'command',r.command,'operationId',r.operation_key,'dispatchKey',r.dispatch_key,\n 'status',r.status,'audit',r.audit_state)\n || CASE WHEN r.status='confirmed' THEN jsonb_build_object('result',r.result) ELSE '{}'::jsonb END,\n 'workflow',CASE WHEN w.id IS NULL THEN NULL ELSE jsonb_build_object('runId',w.id,'status',w.status) END)\n FROM supacloud_commands.execution_receipts r\n LEFT JOIN supacloud_workflows.runs w ON w.id=r.dispatch_key\n WHERE CASE WHEN request ? 'commandId' THEN r.dispatch_key::text=request->>'commandId'\n ELSE r.tenant_id=request->>'tenantId' AND r.actor_id=request->>'actorId'\n AND r.command=request->>'command' AND r.operation_key=request->>'operationId' END\n$status$;\nREVOKE ALL ON FUNCTION supacloud_commands.execution_status(jsonb) FROM PUBLIC;\n";
3
+ /** Explicit migration from the unreleased v1 prototype. Never discard operation keys. */
4
+ export declare const COMMAND_PERSISTENCE_UPGRADE_SQL = "\nALTER TABLE supacloud_commands.execution_receipts\n ADD COLUMN IF NOT EXISTS input_fingerprint text,\n ADD COLUMN IF NOT EXISTS input_payload text;\nDO $migration$\nBEGIN\n IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='supacloud_commands'\n AND table_name='execution_receipts' AND column_name='input_key') THEN\n EXECUTE 'UPDATE supacloud_commands.execution_receipts\n SET input_fingerprint=encode(sha256(convert_to(input_key,''UTF8'')),''hex''),input_payload=input_key\n WHERE input_fingerprint IS NULL';\n ALTER TABLE supacloud_commands.execution_receipts DROP COLUMN input_key;\n END IF;\nEND\n$migration$;\nALTER TABLE supacloud_commands.execution_receipts ALTER COLUMN input_fingerprint SET NOT NULL;\nDO $constraint$\nBEGIN\n IF NOT EXISTS (SELECT 1 FROM pg_constraint\n WHERE conrelid='supacloud_commands.execution_receipts'::regclass\n AND conname='execution_receipts_input_fingerprint_check') THEN\n ALTER TABLE supacloud_commands.execution_receipts\n ADD CONSTRAINT execution_receipts_input_fingerprint_check CHECK (input_fingerprint ~ '^[a-f0-9]{64}$');\n END IF;\nEND\n$constraint$;\n\nDO $runtime$\nBEGIN\n IF to_regprocedure('supacloud_workflows.start_run(uuid,text,text,text,jsonb,integer)') IS NULL\n OR to_regprocedure('supacloud_commands.status(jsonb)') IS NULL THEN\n RAISE EXCEPTION 'Install the SupaCloud Commands/Workflow/PGMQ runtime before command persistence';\n END IF;\nEND\n$runtime$;\nCREATE SCHEMA IF NOT EXISTS supacloud_commands;\nREVOKE ALL ON SCHEMA supacloud_commands FROM PUBLIC;\nCREATE TABLE IF NOT EXISTS supacloud_commands.execution_receipts (\n tenant_id text NOT NULL,\n actor_id text NOT NULL,\n command text NOT NULL,\n operation_key text NOT NULL,\n kind text NOT NULL CHECK (kind IN ('transactional', 'external')),\n input_fingerprint text NOT NULL CHECK (input_fingerprint ~ '^[a-f0-9]{64}$'),\n input_payload text,\n dispatch_key uuid NOT NULL DEFAULT gen_random_uuid() UNIQUE,\n status text NOT NULL CHECK (status IN ('pending', 'unknown', 'confirmed')),\n audit_state text NOT NULL CHECK (audit_state IN ('pending', 'complete')),\n result jsonb,\n created_at timestamptz NOT NULL DEFAULT now(),\n updated_at timestamptz NOT NULL DEFAULT now(),\n PRIMARY KEY (tenant_id, actor_id, command, operation_key),\n CHECK ((status = 'confirmed') = (result IS NOT NULL)),\n CHECK (audit_state <> 'complete' OR status = 'confirmed'),\n CHECK (kind <> 'transactional' OR (status = 'confirmed' AND audit_state = 'complete'))\n);\nCREATE TABLE IF NOT EXISTS supacloud_commands.execution_audit (\n tenant_id text NOT NULL,\n actor_id text NOT NULL,\n command text NOT NULL,\n operation_key text NOT NULL,\n event text NOT NULL,\n details jsonb NOT NULL,\n created_at timestamptz NOT NULL DEFAULT now(),\n PRIMARY KEY (tenant_id, actor_id, command, operation_key),\n FOREIGN KEY (tenant_id, actor_id, command, operation_key)\n REFERENCES supacloud_commands.execution_receipts (tenant_id, actor_id, command, operation_key)\n);\nREVOKE ALL ON supacloud_commands.execution_receipts, supacloud_commands.execution_audit FROM PUBLIC;\nCREATE OR REPLACE FUNCTION supacloud_commands.enqueue_execution_recovery()\nRETURNS trigger LANGUAGE plpgsql SET search_path = '' AS $recovery$\nDECLARE submission jsonb;\nBEGIN\n PERFORM pg_advisory_xact_lock(hashtextextended(NEW.dispatch_key::text,0));\n SELECT supacloud_commands.snapshot(NEW.dispatch_key,false) INTO submission;\n IF submission IS NOT NULL THEN\n IF submission->>'commandType' IS DISTINCT FROM NEW.command\n OR submission->>'actorId' IS DISTINCT FROM NEW.actor_id\n OR submission->>'tenantId' IS DISTINCT FROM NEW.tenant_id\n OR NEW.operation_key <> NEW.dispatch_key::text THEN\n RAISE EXCEPTION 'SUPACLOUD_COMMAND_IDENTITY_CONFLICT';\n END IF;\n RETURN NEW;\n END IF;\n IF NEW.kind = 'external' AND (NEW.status <> 'confirmed' OR NEW.audit_state <> 'complete') THEN\n PERFORM supacloud_workflows.start_run(\n NEW.dispatch_key, 'supacloud.command.reconcile', '1', 'reconcile',\n jsonb_build_object('commandId', NEW.dispatch_key, 'tenantId', NEW.tenant_id,\n 'actorId', NEW.actor_id, 'command', NEW.command, 'operationId', NEW.operation_key),\n 20\n );\n END IF;\n RETURN NEW;\nEND\n$recovery$;\nCREATE OR REPLACE TRIGGER execution_recovery_enqueue\nAFTER INSERT ON supacloud_commands.execution_receipts\nFOR EACH ROW EXECUTE FUNCTION supacloud_commands.enqueue_execution_recovery();\n\nCREATE OR REPLACE FUNCTION supacloud_commands.execution_status(request jsonb)\nRETURNS jsonb LANGUAGE sql STABLE SECURITY DEFINER SET search_path = '' AS $status$\n SELECT jsonb_build_object('kind','execution','commandId',r.dispatch_key,\n 'execution',jsonb_build_object('tenantId',r.tenant_id,'actorId',r.actor_id,\n 'command',r.command,'operationId',r.operation_key,'dispatchKey',r.dispatch_key,\n 'status',r.status,'audit',r.audit_state)\n || CASE WHEN r.status='confirmed' THEN jsonb_build_object('result',r.result) ELSE '{}'::jsonb END,\n 'workflow',CASE WHEN w.id IS NULL THEN NULL ELSE jsonb_build_object('runId',w.id,'status',w.status) END)\n FROM supacloud_commands.execution_receipts r\n LEFT JOIN supacloud_workflows.runs w ON w.id=r.dispatch_key\n WHERE CASE WHEN request ? 'commandId' THEN r.dispatch_key::text=request->>'commandId'\n ELSE r.tenant_id=request->>'tenantId' AND r.actor_id=request->>'actorId'\n AND r.command=request->>'command' AND r.operation_key=request->>'operationId' END\n$status$;\nREVOKE ALL ON FUNCTION supacloud_commands.execution_status(jsonb) FROM PUBLIC;\n\nDO $backfill$\nDECLARE r record;\nBEGIN\n FOR r IN SELECT * FROM supacloud_commands.execution_receipts\n WHERE kind='external' AND (status<>'confirmed' OR audit_state<>'complete')\n AND NOT EXISTS(SELECT FROM supacloud_workflows.runs w WHERE w.id=dispatch_key)\n LOOP\n PERFORM supacloud_workflows.start_run(\n r.dispatch_key,'supacloud.command.reconcile','1','reconcile',\n jsonb_build_object('commandId',r.dispatch_key,'tenantId',r.tenant_id,\n 'actorId',r.actor_id,'command',r.command,'operationId',r.operation_key),20\n );\n END LOOP;\nEND\n$backfill$;\nDROP INDEX IF EXISTS supacloud_commands.execution_pending_recovery;\nALTER TABLE supacloud_commands.execution_receipts\n DROP COLUMN IF EXISTS lease_id,\n DROP COLUMN IF EXISTS lease_until,\n DROP COLUMN IF EXISTS next_attempt_at,\n DROP COLUMN IF EXISTS recovery_attempts;\n";
package/dist/index.d.ts CHANGED
@@ -8,3 +8,5 @@ export { migrationBindingSha256, parseMigrationBindingManifest, renderMigrationB
8
8
  export { buildDatabaseManifest, explainObject, type DatabaseManifest, type DatabaseManifestModule, } from './manifest.js';
9
9
  export { createDatabaseAccessBoundary, DatabaseAccessError, type AuthenticatedDatabaseIdentity, type DatabaseAccessBoundary, type DatabaseAccessBoundaryOptions, type DatabaseAccessErrorCode, } from './access.js';
10
10
  export { createRpcClient, defineRpcContract, RpcContractError, type RpcArgs, type RpcCallResult, type RpcContract, type RpcDecoder, type RpcResult, type RpcTransport, } from './rpc.js';
11
+ export { COMMAND_PERSISTENCE_SQL, COMMAND_PERSISTENCE_UPGRADE_SQL } from "./command-schema";
12
+ export { createPostgresCommandStore, type CommandDatabase, type CommandTransaction, type CommandSubmissionBinding, } from "./command-adapter";
package/dist/index.js CHANGED
@@ -572,16 +572,16 @@ function parseMigrationBindingManifest(value) {
572
572
  throw new Error("Duplicate migration binding environment");
573
573
  }
574
574
  const files = new Set;
575
- const templates = manifest.templates.map((value2) => {
576
- const template = record(value2);
575
+ const templates = manifest.templates.map((value) => {
576
+ const template = record(value);
577
577
  keys(template, ["file", "templateSha256", "parameters"]);
578
578
  if (!validFile(template.file) || files.has(template.file) || typeof template.templateSha256 !== "string" || !HASH.test(template.templateSha256) || !Array.isArray(template.parameters) || !template.parameters.length) {
579
579
  throw new Error("Invalid or duplicate migration binding template");
580
580
  }
581
581
  files.add(template.file);
582
582
  const placeholders = new Set;
583
- const parameters = template.parameters.map((value3) => {
584
- const parameter = record(value3);
583
+ const parameters = template.parameters.map((value) => {
584
+ const parameter = record(value);
585
585
  keys(parameter, ["placeholder", "variable", "type", "occurrences"]);
586
586
  if (typeof parameter.placeholder !== "string" || !/^__[A-Z][A-Z0-9_]*__$/.test(parameter.placeholder) || placeholders.has(parameter.placeholder) || typeof parameter.variable !== "string" || !/^[A-Z][A-Z0-9_]*$/.test(parameter.variable) || !["uuid", "https-url", "resource-name"].includes(String(parameter.type)) || !Number.isSafeInteger(parameter.occurrences) || Number(parameter.occurrences) < 1) {
587
587
  throw new Error("Invalid or duplicate migration binding parameter");
@@ -879,12 +879,305 @@ function createRpcClient(transport, contracts) {
879
879
  }
880
880
  };
881
881
  }
882
+ // src/command-schema.ts
883
+ var COMMAND_PERSISTENCE_SQL = `
884
+ DO $runtime$
885
+ BEGIN
886
+ IF to_regprocedure('supacloud_workflows.start_run(uuid,text,text,text,jsonb,integer)') IS NULL
887
+ OR to_regprocedure('supacloud_commands.status(jsonb)') IS NULL THEN
888
+ RAISE EXCEPTION 'Install the SupaCloud Commands/Workflow/PGMQ runtime before command persistence';
889
+ END IF;
890
+ END
891
+ $runtime$;
892
+ CREATE SCHEMA IF NOT EXISTS supacloud_commands;
893
+ REVOKE ALL ON SCHEMA supacloud_commands FROM PUBLIC;
894
+ CREATE TABLE IF NOT EXISTS supacloud_commands.execution_receipts (
895
+ tenant_id text NOT NULL,
896
+ actor_id text NOT NULL,
897
+ command text NOT NULL,
898
+ operation_key text NOT NULL,
899
+ kind text NOT NULL CHECK (kind IN ('transactional', 'external')),
900
+ input_fingerprint text NOT NULL CHECK (input_fingerprint ~ '^[a-f0-9]{64}$'),
901
+ input_payload text,
902
+ dispatch_key uuid NOT NULL DEFAULT gen_random_uuid() UNIQUE,
903
+ status text NOT NULL CHECK (status IN ('pending', 'unknown', 'confirmed')),
904
+ audit_state text NOT NULL CHECK (audit_state IN ('pending', 'complete')),
905
+ result jsonb,
906
+ created_at timestamptz NOT NULL DEFAULT now(),
907
+ updated_at timestamptz NOT NULL DEFAULT now(),
908
+ PRIMARY KEY (tenant_id, actor_id, command, operation_key),
909
+ CHECK ((status = 'confirmed') = (result IS NOT NULL)),
910
+ CHECK (audit_state <> 'complete' OR status = 'confirmed'),
911
+ CHECK (kind <> 'transactional' OR (status = 'confirmed' AND audit_state = 'complete'))
912
+ );
913
+ CREATE TABLE IF NOT EXISTS supacloud_commands.execution_audit (
914
+ tenant_id text NOT NULL,
915
+ actor_id text NOT NULL,
916
+ command text NOT NULL,
917
+ operation_key text NOT NULL,
918
+ event text NOT NULL,
919
+ details jsonb NOT NULL,
920
+ created_at timestamptz NOT NULL DEFAULT now(),
921
+ PRIMARY KEY (tenant_id, actor_id, command, operation_key),
922
+ FOREIGN KEY (tenant_id, actor_id, command, operation_key)
923
+ REFERENCES supacloud_commands.execution_receipts (tenant_id, actor_id, command, operation_key)
924
+ );
925
+ REVOKE ALL ON supacloud_commands.execution_receipts, supacloud_commands.execution_audit FROM PUBLIC;
926
+ CREATE OR REPLACE FUNCTION supacloud_commands.enqueue_execution_recovery()
927
+ RETURNS trigger LANGUAGE plpgsql SET search_path = '' AS $recovery$
928
+ DECLARE submission jsonb;
929
+ BEGIN
930
+ PERFORM pg_advisory_xact_lock(hashtextextended(NEW.dispatch_key::text,0));
931
+ SELECT supacloud_commands.snapshot(NEW.dispatch_key,false) INTO submission;
932
+ IF submission IS NOT NULL THEN
933
+ IF submission->>'commandType' IS DISTINCT FROM NEW.command
934
+ OR submission->>'actorId' IS DISTINCT FROM NEW.actor_id
935
+ OR submission->>'tenantId' IS DISTINCT FROM NEW.tenant_id
936
+ OR NEW.operation_key <> NEW.dispatch_key::text THEN
937
+ RAISE EXCEPTION 'SUPACLOUD_COMMAND_IDENTITY_CONFLICT';
938
+ END IF;
939
+ RETURN NEW;
940
+ END IF;
941
+ IF NEW.kind = 'external' AND (NEW.status <> 'confirmed' OR NEW.audit_state <> 'complete') THEN
942
+ PERFORM supacloud_workflows.start_run(
943
+ NEW.dispatch_key, 'supacloud.command.reconcile', '1', 'reconcile',
944
+ jsonb_build_object('commandId', NEW.dispatch_key, 'tenantId', NEW.tenant_id,
945
+ 'actorId', NEW.actor_id, 'command', NEW.command, 'operationId', NEW.operation_key),
946
+ 20
947
+ );
948
+ END IF;
949
+ RETURN NEW;
950
+ END
951
+ $recovery$;
952
+ CREATE OR REPLACE TRIGGER execution_recovery_enqueue
953
+ AFTER INSERT ON supacloud_commands.execution_receipts
954
+ FOR EACH ROW EXECUTE FUNCTION supacloud_commands.enqueue_execution_recovery();
955
+
956
+ CREATE OR REPLACE FUNCTION supacloud_commands.execution_status(request jsonb)
957
+ RETURNS jsonb LANGUAGE sql STABLE SECURITY DEFINER SET search_path = '' AS $status$
958
+ SELECT jsonb_build_object('kind','execution','commandId',r.dispatch_key,
959
+ 'execution',jsonb_build_object('tenantId',r.tenant_id,'actorId',r.actor_id,
960
+ 'command',r.command,'operationId',r.operation_key,'dispatchKey',r.dispatch_key,
961
+ 'status',r.status,'audit',r.audit_state)
962
+ || CASE WHEN r.status='confirmed' THEN jsonb_build_object('result',r.result) ELSE '{}'::jsonb END,
963
+ 'workflow',CASE WHEN w.id IS NULL THEN NULL ELSE jsonb_build_object('runId',w.id,'status',w.status) END)
964
+ FROM supacloud_commands.execution_receipts r
965
+ LEFT JOIN supacloud_workflows.runs w ON w.id=r.dispatch_key
966
+ WHERE CASE WHEN request ? 'commandId' THEN r.dispatch_key::text=request->>'commandId'
967
+ ELSE r.tenant_id=request->>'tenantId' AND r.actor_id=request->>'actorId'
968
+ AND r.command=request->>'command' AND r.operation_key=request->>'operationId' END
969
+ $status$;
970
+ REVOKE ALL ON FUNCTION supacloud_commands.execution_status(jsonb) FROM PUBLIC;
971
+ `;
972
+ var COMMAND_PERSISTENCE_UPGRADE_SQL = `
973
+ ALTER TABLE supacloud_commands.execution_receipts
974
+ ADD COLUMN IF NOT EXISTS input_fingerprint text,
975
+ ADD COLUMN IF NOT EXISTS input_payload text;
976
+ DO $migration$
977
+ BEGIN
978
+ IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema='supacloud_commands'
979
+ AND table_name='execution_receipts' AND column_name='input_key') THEN
980
+ EXECUTE 'UPDATE supacloud_commands.execution_receipts
981
+ SET input_fingerprint=encode(sha256(convert_to(input_key,''UTF8'')),''hex''),input_payload=input_key
982
+ WHERE input_fingerprint IS NULL';
983
+ ALTER TABLE supacloud_commands.execution_receipts DROP COLUMN input_key;
984
+ END IF;
985
+ END
986
+ $migration$;
987
+ ALTER TABLE supacloud_commands.execution_receipts ALTER COLUMN input_fingerprint SET NOT NULL;
988
+ DO $constraint$
989
+ BEGIN
990
+ IF NOT EXISTS (SELECT 1 FROM pg_constraint
991
+ WHERE conrelid='supacloud_commands.execution_receipts'::regclass
992
+ AND conname='execution_receipts_input_fingerprint_check') THEN
993
+ ALTER TABLE supacloud_commands.execution_receipts
994
+ ADD CONSTRAINT execution_receipts_input_fingerprint_check CHECK (input_fingerprint ~ '^[a-f0-9]{64}$');
995
+ END IF;
996
+ END
997
+ $constraint$;
998
+ ${COMMAND_PERSISTENCE_SQL}
999
+ DO $backfill$
1000
+ DECLARE r record;
1001
+ BEGIN
1002
+ FOR r IN SELECT * FROM supacloud_commands.execution_receipts
1003
+ WHERE kind='external' AND (status<>'confirmed' OR audit_state<>'complete')
1004
+ AND NOT EXISTS(SELECT FROM supacloud_workflows.runs w WHERE w.id=dispatch_key)
1005
+ LOOP
1006
+ PERFORM supacloud_workflows.start_run(
1007
+ r.dispatch_key,'supacloud.command.reconcile','1','reconcile',
1008
+ jsonb_build_object('commandId',r.dispatch_key,'tenantId',r.tenant_id,
1009
+ 'actorId',r.actor_id,'command',r.command,'operationId',r.operation_key),20
1010
+ );
1011
+ END LOOP;
1012
+ END
1013
+ $backfill$;
1014
+ DROP INDEX IF EXISTS supacloud_commands.execution_pending_recovery;
1015
+ ALTER TABLE supacloud_commands.execution_receipts
1016
+ DROP COLUMN IF EXISTS lease_id,
1017
+ DROP COLUMN IF EXISTS lease_until,
1018
+ DROP COLUMN IF EXISTS next_attempt_at,
1019
+ DROP COLUMN IF EXISTS recovery_attempts;
1020
+ `;
1021
+ // src/command-adapter.ts
1022
+ import {
1023
+ CommandError,
1024
+ canonicalCommandJson,
1025
+ commandIdentifier,
1026
+ decodeCommandJson,
1027
+ decodeDurableCommandReceipt
1028
+ } from "@supacloud/contracts";
1029
+ var where = "tenant_id=$1 AND actor_id=$2 AND command=$3 AND operation_key=$4";
1030
+ var keys2 = (ref) => [ref.tenantId, ref.actorId, ref.command, ref.operationId].map(commandIdentifier);
1031
+ function rows(value) {
1032
+ const isRow = (row) => row !== null && typeof row === "object" && !Array.isArray(row);
1033
+ if (!Array.isArray(value) || !Array.from(value).every(isRow))
1034
+ throw new CommandError("COMMAND_RECEIPT_INVALID");
1035
+ return value;
1036
+ }
1037
+ function decodeStored(row) {
1038
+ try {
1039
+ const fingerprint = row["input_fingerprint"], payload = row["input_payload"], kind = row["kind"];
1040
+ if (typeof fingerprint !== "string" || !/^[a-f0-9]{64}$/.test(fingerprint) || payload !== null && typeof payload !== "string" || kind !== "transactional" && kind !== "external")
1041
+ throw new Error("Invalid record");
1042
+ const serialized = row["result_json"];
1043
+ if (serialized !== null && typeof serialized !== "string")
1044
+ throw new Error("Invalid stored JSON");
1045
+ const raw = typeof serialized === "string" ? JSON.parse(serialized) : undefined;
1046
+ const receipt = decodeDurableCommandReceipt({ ...row, ...raw === undefined ? {} : { result: raw } }, decodeCommandJson);
1047
+ return { receipt, kind, inputFingerprint: fingerprint, inputPayload: payload };
1048
+ } catch {
1049
+ throw new CommandError("COMMAND_RECEIPT_INVALID");
1050
+ }
1051
+ }
1052
+ var projection = `tenant_id AS "tenantId", actor_id AS "actorId", command,
1053
+ operation_key AS "operationId", dispatch_key::text AS "dispatchKey",
1054
+ status, audit_state AS audit, result::text AS result_json, input_fingerprint,input_payload,kind`;
1055
+ function createPostgresCommandStore(database, options = {}) {
1056
+ const submission = options.submission === undefined ? undefined : { ...options.submission };
1057
+ if (submission !== undefined && (!/^[0-9a-f-]{36}$/.test(submission.commandId) || !/^[0-9a-f-]{36}$/.test(submission.stepId) || !/^[1-9][0-9]*$/.test(submission.messageId) || !Number.isSafeInteger(submission.attempt) || submission.attempt < 1 || !commandIdentifier(submission.workerId)))
1058
+ throw new CommandError("COMMAND_INPUT_INVALID");
1059
+ const submittedPayload = async (tx, ref, inserting) => {
1060
+ if (submission === undefined)
1061
+ throw new CommandError("COMMAND_INPUT_INVALID");
1062
+ if (ref.operationId !== submission.commandId)
1063
+ throw new CommandError("COMMAND_IDEMPOTENCY_CONFLICT");
1064
+ const found = rows(await tx.query(`SELECT c.command_type,c.actor_id::text,c.tenant_id,c.payload::text AS payload_json,
1065
+ s.step_key,s.status FROM supacloud_commands.receipts c
1066
+ JOIN LATERAL supacloud_workflows.lock_step_attempt($2::uuid,$3::bigint,$4::integer,$5) s
1067
+ ON s.run_id=c.id WHERE c.id=$1::uuid`, [submission.commandId, submission.stepId, submission.messageId, submission.attempt, submission.workerId]));
1068
+ const row = found[0];
1069
+ if (found.length !== 1 || row === undefined || row["command_type"] !== ref.command || row["actor_id"] !== ref.actorId || row["tenant_id"] !== ref.tenantId || row["step_key"] !== "execute" || row["status"] !== "running" && (inserting || row["status"] !== "completed") || typeof row["payload_json"] !== "string")
1070
+ throw new CommandError("COMMAND_REJECTED");
1071
+ const payload = JSON.parse(row["payload_json"]);
1072
+ return payload;
1073
+ };
1074
+ const transaction = async (run) => {
1075
+ let started = false;
1076
+ try {
1077
+ return await database.transaction((tx) => {
1078
+ started = true;
1079
+ return run(tx);
1080
+ });
1081
+ } catch (error) {
1082
+ if (error instanceof CommandError)
1083
+ throw error;
1084
+ throw new CommandError(started ? "COMMAND_OUTCOME_UNKNOWN" : "COMMAND_UNAVAILABLE");
1085
+ }
1086
+ };
1087
+ const session = (tx) => ({
1088
+ transaction: tx,
1089
+ async lock(ref) {
1090
+ await tx.query("SELECT pg_advisory_xact_lock(hashtextextended($1,0))", [JSON.stringify(keys2(ref))]);
1091
+ if (submission !== undefined)
1092
+ await submittedPayload(tx, ref, false);
1093
+ },
1094
+ async read(ref) {
1095
+ const found = rows(await tx.query(`SELECT ${projection} FROM supacloud_commands.execution_receipts WHERE ${where}`, keys2(ref)));
1096
+ if (found.length > 1)
1097
+ throw new CommandError("COMMAND_RECEIPT_INVALID");
1098
+ const row = found[0];
1099
+ return row === undefined ? null : decodeStored(row);
1100
+ },
1101
+ async insert(record) {
1102
+ const receipt = submission === undefined ? record.receipt : { ...record.receipt, dispatchKey: submission.commandId };
1103
+ if (submission !== undefined) {
1104
+ const payload = await submittedPayload(tx, receipt, true);
1105
+ const digest = await crypto.subtle.digest("SHA-256", new TextEncoder().encode(canonicalCommandJson(payload)));
1106
+ const fingerprint = Array.from(new Uint8Array(digest), (byte) => byte.toString(16).padStart(2, "0")).join("");
1107
+ if (fingerprint !== record.inputFingerprint)
1108
+ throw new CommandError("COMMAND_IDEMPOTENCY_CONFLICT");
1109
+ }
1110
+ const result = receipt.status === "confirmed" ? canonicalCommandJson(receipt.result) : null;
1111
+ await tx.query(`INSERT INTO supacloud_commands.execution_receipts
1112
+ (tenant_id,actor_id,command,operation_key,dispatch_key,kind,input_fingerprint,input_payload,status,audit_state,result)
1113
+ VALUES ($1,$2,$3,$4,$5::uuid,$6,$7,$8,$9,$10,$11::text::jsonb)`, [...keys2(receipt), receipt.dispatchKey, record.kind, record.inputFingerprint, record.inputPayload, receipt.status, receipt.audit, result]);
1114
+ if (submission !== undefined) {
1115
+ const attempt = { stepId: submission.stepId, messageId: submission.messageId, attempt: submission.attempt, workerId: submission.workerId };
1116
+ const commandId = submission.commandId;
1117
+ if (record.kind === "external") {
1118
+ await tx.query("SELECT public.supacloud_workflow_advance($1::text::jsonb)", [JSON.stringify({
1119
+ ...attempt,
1120
+ output: { commandId, status: "intent-recorded" },
1121
+ nextStepKey: "reconcile",
1122
+ nextInput: { commandId, tenantId: receipt.tenantId, actorId: receipt.actorId, command: receipt.command, operationId: receipt.operationId },
1123
+ nextMaxAttempts: 20
1124
+ })]);
1125
+ } else {
1126
+ const output = { commandId, status: "confirmed", audit: "complete" };
1127
+ await tx.query("SELECT public.supacloud_workflow_complete($1::text::jsonb)", [JSON.stringify({ ...attempt, stepOutput: output, runOutput: output })]);
1128
+ }
1129
+ }
1130
+ },
1131
+ async confirm(ref, result) {
1132
+ await tx.query(`UPDATE supacloud_commands.execution_receipts SET status='confirmed',result=$5::text::jsonb,updated_at=now()
1133
+ WHERE ${where} AND status<>'confirmed'`, [...keys2(ref), canonicalCommandJson(result)]);
1134
+ },
1135
+ async markUnknown(ref) {
1136
+ await tx.query(`UPDATE supacloud_commands.execution_receipts SET status='unknown',updated_at=now()
1137
+ WHERE ${where} AND status<>'confirmed'`, keys2(ref));
1138
+ },
1139
+ async audit(ref, event, details) {
1140
+ await tx.query(`INSERT INTO supacloud_commands.execution_audit
1141
+ (tenant_id,actor_id,command,operation_key,event,details) VALUES ($1,$2,$3,$4,$5,$6::text::jsonb)`, [...keys2(ref), commandIdentifier(event), canonicalCommandJson(details)]);
1142
+ },
1143
+ async completeAudit(ref) {
1144
+ await tx.query(`UPDATE supacloud_commands.execution_receipts SET audit_state='complete',updated_at=now() WHERE ${where}`, keys2(ref));
1145
+ }
1146
+ });
1147
+ const scopeKeys = (scope) => {
1148
+ if (scope.commands.length === 0)
1149
+ throw new CommandError("COMMAND_INPUT_INVALID");
1150
+ return [commandIdentifier(scope.tenantId), JSON.stringify(scope.commands.map(commandIdentifier))];
1151
+ };
1152
+ const integer = (value, min, max = Number.MAX_SAFE_INTEGER) => {
1153
+ if (!Number.isSafeInteger(value) || value < min || value > max)
1154
+ throw new CommandError("COMMAND_INPUT_INVALID");
1155
+ return value;
1156
+ };
1157
+ const selected = `tenant_id=$1 AND command IN (SELECT jsonb_array_elements_text($2::text::jsonb))`;
1158
+ const join = `r.tenant_id=c.tenant_id AND r.actor_id=c.actor_id AND r.command=c.command AND r.operation_key=c.operation_key`;
1159
+ return {
1160
+ transaction: (run) => transaction((tx) => run(session(tx))),
1161
+ async redactCompleted(options) {
1162
+ return transaction(async (tx) => rows(await tx.query(`WITH candidates AS (
1163
+ SELECT tenant_id,actor_id,command,operation_key FROM supacloud_commands.execution_receipts
1164
+ WHERE ${selected} AND status='confirmed' AND audit_state='complete' AND input_payload IS NOT NULL
1165
+ AND updated_at<to_timestamp($3::double precision/1000)
1166
+ ORDER BY updated_at LIMIT $4 FOR UPDATE SKIP LOCKED
1167
+ ) UPDATE supacloud_commands.execution_receipts r SET input_payload=NULL FROM candidates c
1168
+ WHERE ${join} RETURNING r.operation_key`, [...scopeKeys(options), integer(options.before, 0), integer(options.limit, 1, 1000)])).length);
1169
+ }
1170
+ };
1171
+ }
882
1172
  export {
1173
+ COMMAND_PERSISTENCE_SQL,
1174
+ COMMAND_PERSISTENCE_UPGRADE_SQL,
883
1175
  DatabaseAccessError,
884
1176
  RpcContractError,
885
1177
  applyModulePlan,
886
1178
  buildDatabaseManifest,
887
1179
  createDatabaseAccessBoundary,
1180
+ createPostgresCommandStore,
888
1181
  createRpcClient,
889
1182
  defineDatabaseModule,
890
1183
  defineRpcContract,
package/package.json CHANGED
@@ -1,12 +1,16 @@
1
1
  {
2
2
  "name": "@supacloud/db",
3
- "version": "0.6.0",
3
+ "version": "0.7.1",
4
4
  "description": "Database governance layer for SupaCloud: RLS policies, RPC functions and grants as first-class resources, with manifest/catalog reconciliation",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
7
7
  "module": "./dist/index.js",
8
8
  "types": "./dist/index.d.ts",
9
9
  "exports": {
10
+ "./bun": {
11
+ "types": "./dist/command-bun.d.ts",
12
+ "import": "./dist/command-bun.js"
13
+ },
10
14
  ".": {
11
15
  "types": "./dist/index.d.ts",
12
16
  "import": "./dist/index.js",
@@ -20,7 +24,7 @@
20
24
  ],
21
25
  "scripts": {
22
26
  "build": "bun run clean && bun run build:js && bun run build:types",
23
- "build:js": "bun build src/index.ts --outdir dist --target node",
27
+ "build:js": "bun build src/index.ts src/command-bun.ts --outdir dist --target node --external bun --external @supacloud/contracts",
24
28
  "build:types": "tsc -p tsconfig.json --emitDeclarationOnly",
25
29
  "clean": "rm -rf dist",
26
30
  "prepublishOnly": "bun run build",
@@ -42,7 +46,13 @@
42
46
  "directory": "packages/db"
43
47
  },
44
48
  "devDependencies": {
45
- "@types/bun": "^1.4.0",
49
+ "@types/bun": "^1.4.2",
46
50
  "typescript": "^7.0.2"
51
+ },
52
+ "dependencies": {
53
+ "@supacloud/contracts": "0.3.1"
54
+ },
55
+ "overrides": {
56
+ "@supacloud/contracts": "0.3.1"
47
57
  }
48
58
  }