@cascaide-ts/postgres-js 0.1.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License
2
+
3
+ Copyright (c) 2026 Cascaide
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
package/dist/index.cjs ADDED
@@ -0,0 +1,136 @@
1
+ var l=Object.defineProperty;var I=Object.getOwnPropertyDescriptor;var T=Object.getOwnPropertyNames;var C=Object.prototype.hasOwnProperty;var x=(r,t)=>{for(var a in t)l(r,a,{get:t[a],enumerable:!0})},R=(r,t,a,n)=>{if(t&&typeof t=="object"||typeof t=="function")for(let e of T(t))!C.call(r,e)&&e!==a&&l(r,e,{get:()=>t[e],enumerable:!(n=I(t,e))||n.enumerable});return r};var S=r=>R(l({},"__esModule",{value:!0}),r);var y={};x(y,{PostgresPersistor:()=>u});module.exports=S(y);var f=require("crypto"),u=class{constructor(t){this.sql=t}async claimNodeExecution(t){let a=performance.now(),{nodeInstanceId:n,cascadeId:e,userId:s,nodeName:c,functionId:o,inputContext:d,location:i}=t;console.log("Claiming node execution:",{cascadeId:e,nodeName:c,requestedId:o});let _=(0,f.randomUUID)(),m=await this.sql.begin(async N=>{let E=await N`
2
+ WITH conflict_check AS (
3
+ -- Single aggregation combining conflict detection and MAX calculation
4
+ -- More efficient than separate EXISTS and MAX queries
5
+ SELECT
6
+ CASE
7
+ WHEN COUNT(*) FILTER (WHERE function_id = ${o}) > 0
8
+ THEN MAX(function_id) + 1
9
+ ELSE ${o}
10
+ END as next_fn_id
11
+ FROM node_executions
12
+ WHERE cascade_id = ${e}
13
+ ),
14
+ cascade_upsert AS (
15
+ INSERT INTO cascades (id, user_id, status, fn_id, created_at, updated_at)
16
+ SELECT ${e}, ${s}, 'RUNNING', next_fn_id, NOW(), NOW()
17
+ FROM conflict_check
18
+ ON CONFLICT (id) DO UPDATE
19
+ SET fn_id = EXCLUDED.fn_id, updated_at = NOW()
20
+ RETURNING fn_id
21
+ )
22
+ INSERT INTO node_executions (
23
+ id,
24
+ node_instance_id,
25
+ cascade_id,
26
+ node_name,
27
+ function_id,
28
+ input_context,
29
+ location,
30
+ status,
31
+ started_at
32
+ )
33
+ SELECT
34
+ ${_},
35
+ ${n},
36
+ ${e},
37
+ ${c},
38
+ fn_id,
39
+ ${this.sql.json(d)},
40
+ ${i},
41
+ 'RUNNING',
42
+ NOW()
43
+ FROM cascade_upsert
44
+ RETURNING status, function_id as "functionId"
45
+ `;return{status:E[0].status,functionId:Number(E[0].functionId)}}),p=performance.now()-a;return console.log(`claimNodeExecution latency: ${p.toFixed(2)}ms`,{cascadeId:e,nodeName:c}),m}async finalizeNodeExecution(t){let a=performance.now(),{nodeInstanceId:n,cascadeId:e,fullOutput:s,hasSpawns:c}=t,o=await this.sql.begin(async i=>(await i`
46
+ WITH updated_node AS (
47
+ UPDATE node_executions
48
+ SET
49
+ status = 'COMPLETED',
50
+ full_output = ${this.sql.json(s)},
51
+ completed_at = NOW()
52
+ WHERE node_instance_id = ${n}
53
+ RETURNING cascade_id
54
+ )
55
+ UPDATE cascades
56
+ SET status = 'COMPLETED', updated_at = NOW()
57
+ WHERE id IN (SELECT cascade_id FROM updated_node)
58
+ AND ${!c}
59
+ `,{status:"COMPLETED"})),d=performance.now()-a;return console.log(`finalizeNodeExecution latency: ${d.toFixed(2)}ms`,{cascadeId:e,nodeInstanceId:n}),o}async markExecutionFailed(t,a,n){let e=performance.now(),s=await this.sql.begin(async o=>(await o`
60
+ WITH updated_node AS (
61
+ UPDATE node_executions
62
+ SET status = 'FAILED', error = ${n}, completed_at = NOW()
63
+ WHERE node_instance_id = ${t}
64
+ RETURNING cascade_id
65
+ )
66
+ UPDATE cascades
67
+ SET status = 'ERROR', updated_at = NOW()
68
+ WHERE id IN (SELECT cascade_id FROM updated_node)
69
+ `,{status:"FAILED"})),c=performance.now()-e;return console.log(`markExecutionFailed latency: ${c.toFixed(2)}ms`,{cascadeId:a,nodeInstanceId:t}),s}async recordContextEvents(t){let a=performance.now(),{cascadeId:n,functionId:e,updates:s}=t,c=Object.entries(s).map(([d,i])=>({key:d,value:this.sql.json(i),function_id:e,cascade_id:n,created_at:new Date}));if(c.length===0){let d=performance.now()-a;return console.log(`recordContextEvents latency: ${d.toFixed(2)}ms (no events)`,{cascadeId:n,functionId:e}),{status:"SUCCESS"}}await this.sql`
70
+ INSERT INTO context_events ${this.sql(c,"key","value","function_id","cascade_id","created_at")}
71
+ `;let o=performance.now()-a;return console.log(`recordContextEvents latency: ${o.toFixed(2)}ms (${c.length} events)`,{cascadeId:n,functionId:e}),{status:"SUCCESS"}}async hydrateCascadeContext(t,a){let n=performance.now(),e=await this.sql`
72
+ SELECT key, value, function_id, created_at
73
+ FROM context_events
74
+ WHERE cascade_id = ${t}
75
+ AND function_id < ${a}
76
+ ORDER BY function_id ASC, created_at ASC
77
+ `,s={};for(let o of e){let d=o.key,i=o.value;if(s[d]||(s[d]=[]),typeof i=="object"&&i!==null&&!Array.isArray(i)&&"index"in i){let _=i.index;s[d][_]=i}else s[d].push(i)}let c=performance.now()-n;return console.log(`hydrateCascadeContext latency: ${c.toFixed(2)}ms (${e.length} events)`,{cascadeId:t,upToFunctionId:a}),s}async forkCascade(t){let a=performance.now(),{sourceCascadeId:n,newCascadeId:e,upToFunctionId:s}=t;await this.sql.begin(async o=>{await o`
78
+ INSERT INTO cascades (id, user_id, status, fn_id, created_at, updated_at)
79
+ SELECT
80
+ ${e},
81
+ user_id,
82
+ 'RUNNING',
83
+ ${s-1},
84
+ NOW(),
85
+ NOW()
86
+ FROM cascades
87
+ WHERE id = ${n}
88
+ `,await o`
89
+ INSERT INTO node_executions (
90
+ id,
91
+ node_instance_id,
92
+ cascade_id,
93
+ node_name,
94
+ function_id,
95
+ input_context,
96
+ location,
97
+ status,
98
+ started_at,
99
+ completed_at,
100
+ full_output,
101
+ error
102
+ )
103
+ SELECT
104
+ gen_random_uuid(),
105
+ node_instance_id,
106
+ ${e},
107
+ node_name,
108
+ function_id,
109
+ input_context,
110
+ location,
111
+ status,
112
+ started_at,
113
+ completed_at,
114
+ full_output,
115
+ error
116
+ FROM node_executions
117
+ WHERE cascade_id = ${n}
118
+ AND function_id < ${s}
119
+ `,await o`
120
+ INSERT INTO context_events (
121
+ key,
122
+ value,
123
+ function_id,
124
+ cascade_id,
125
+ created_at
126
+ )
127
+ SELECT
128
+ key,
129
+ value,
130
+ function_id,
131
+ ${e},
132
+ created_at
133
+ FROM context_events
134
+ WHERE cascade_id = ${n}
135
+ AND function_id < ${s}
136
+ `});let c=performance.now()-a;return console.log(`forkCascade latency: ${c.toFixed(2)}ms`,{sourceCascadeId:n,newCascadeId:e,upToFunctionId:s}),{newCascadeId:e,status:"SUCCESS"}}};0&&(module.exports={PostgresPersistor});
@@ -0,0 +1,40 @@
1
+ import postgres from 'postgres';
2
+ import { CascadePersistence, ClaimRequest, ClaimResponse, WorkflowContext } from '@cascaide-ts/core';
3
+ export { CascadePersistence } from '@cascaide-ts/core';
4
+
5
+ declare class PostgresPersistor implements CascadePersistence {
6
+ private sql;
7
+ constructor(sql: postgres.Sql<{}>);
8
+ claimNodeExecution(params: ClaimRequest): Promise<ClaimResponse>;
9
+ finalizeNodeExecution(params: {
10
+ nodeInstanceId: string;
11
+ cascadeId: string;
12
+ fullOutput: any;
13
+ hasSpawns: boolean;
14
+ }): Promise<{
15
+ status: string;
16
+ }>;
17
+ markExecutionFailed(nodeInstanceId: string, cascadeId: string, error: string): Promise<{
18
+ status: string;
19
+ }>;
20
+ recordContextEvents(params: {
21
+ cascadeId: string;
22
+ functionId: number;
23
+ updates: {
24
+ [key: string]: any;
25
+ };
26
+ }): Promise<{
27
+ status: string;
28
+ }>;
29
+ hydrateCascadeContext(cascadeId: string, upToFunctionId: number): Promise<WorkflowContext>;
30
+ forkCascade(params: {
31
+ sourceCascadeId: string;
32
+ newCascadeId: string;
33
+ upToFunctionId: number;
34
+ }): Promise<{
35
+ newCascadeId: string;
36
+ status: string;
37
+ }>;
38
+ }
39
+
40
+ export { PostgresPersistor };
@@ -0,0 +1,40 @@
1
+ import postgres from 'postgres';
2
+ import { CascadePersistence, ClaimRequest, ClaimResponse, WorkflowContext } from '@cascaide-ts/core';
3
+ export { CascadePersistence } from '@cascaide-ts/core';
4
+
5
+ declare class PostgresPersistor implements CascadePersistence {
6
+ private sql;
7
+ constructor(sql: postgres.Sql<{}>);
8
+ claimNodeExecution(params: ClaimRequest): Promise<ClaimResponse>;
9
+ finalizeNodeExecution(params: {
10
+ nodeInstanceId: string;
11
+ cascadeId: string;
12
+ fullOutput: any;
13
+ hasSpawns: boolean;
14
+ }): Promise<{
15
+ status: string;
16
+ }>;
17
+ markExecutionFailed(nodeInstanceId: string, cascadeId: string, error: string): Promise<{
18
+ status: string;
19
+ }>;
20
+ recordContextEvents(params: {
21
+ cascadeId: string;
22
+ functionId: number;
23
+ updates: {
24
+ [key: string]: any;
25
+ };
26
+ }): Promise<{
27
+ status: string;
28
+ }>;
29
+ hydrateCascadeContext(cascadeId: string, upToFunctionId: number): Promise<WorkflowContext>;
30
+ forkCascade(params: {
31
+ sourceCascadeId: string;
32
+ newCascadeId: string;
33
+ upToFunctionId: number;
34
+ }): Promise<{
35
+ newCascadeId: string;
36
+ status: string;
37
+ }>;
38
+ }
39
+
40
+ export { PostgresPersistor };
package/dist/index.js ADDED
@@ -0,0 +1,136 @@
1
+ import{randomUUID as m}from"crypto";var u=class{constructor(o){this.sql=o}async claimNodeExecution(o){let c=performance.now(),{nodeInstanceId:n,cascadeId:e,userId:t,nodeName:s,functionId:a,inputContext:d,location:i}=o;console.log("Claiming node execution:",{cascadeId:e,nodeName:s,requestedId:a});let r=m(),l=await this.sql.begin(async f=>{let _=await f`
2
+ WITH conflict_check AS (
3
+ -- Single aggregation combining conflict detection and MAX calculation
4
+ -- More efficient than separate EXISTS and MAX queries
5
+ SELECT
6
+ CASE
7
+ WHEN COUNT(*) FILTER (WHERE function_id = ${a}) > 0
8
+ THEN MAX(function_id) + 1
9
+ ELSE ${a}
10
+ END as next_fn_id
11
+ FROM node_executions
12
+ WHERE cascade_id = ${e}
13
+ ),
14
+ cascade_upsert AS (
15
+ INSERT INTO cascades (id, user_id, status, fn_id, created_at, updated_at)
16
+ SELECT ${e}, ${t}, 'RUNNING', next_fn_id, NOW(), NOW()
17
+ FROM conflict_check
18
+ ON CONFLICT (id) DO UPDATE
19
+ SET fn_id = EXCLUDED.fn_id, updated_at = NOW()
20
+ RETURNING fn_id
21
+ )
22
+ INSERT INTO node_executions (
23
+ id,
24
+ node_instance_id,
25
+ cascade_id,
26
+ node_name,
27
+ function_id,
28
+ input_context,
29
+ location,
30
+ status,
31
+ started_at
32
+ )
33
+ SELECT
34
+ ${r},
35
+ ${n},
36
+ ${e},
37
+ ${s},
38
+ fn_id,
39
+ ${this.sql.json(d)},
40
+ ${i},
41
+ 'RUNNING',
42
+ NOW()
43
+ FROM cascade_upsert
44
+ RETURNING status, function_id as "functionId"
45
+ `;return{status:_[0].status,functionId:Number(_[0].functionId)}}),E=performance.now()-c;return console.log(`claimNodeExecution latency: ${E.toFixed(2)}ms`,{cascadeId:e,nodeName:s}),l}async finalizeNodeExecution(o){let c=performance.now(),{nodeInstanceId:n,cascadeId:e,fullOutput:t,hasSpawns:s}=o,a=await this.sql.begin(async i=>(await i`
46
+ WITH updated_node AS (
47
+ UPDATE node_executions
48
+ SET
49
+ status = 'COMPLETED',
50
+ full_output = ${this.sql.json(t)},
51
+ completed_at = NOW()
52
+ WHERE node_instance_id = ${n}
53
+ RETURNING cascade_id
54
+ )
55
+ UPDATE cascades
56
+ SET status = 'COMPLETED', updated_at = NOW()
57
+ WHERE id IN (SELECT cascade_id FROM updated_node)
58
+ AND ${!s}
59
+ `,{status:"COMPLETED"})),d=performance.now()-c;return console.log(`finalizeNodeExecution latency: ${d.toFixed(2)}ms`,{cascadeId:e,nodeInstanceId:n}),a}async markExecutionFailed(o,c,n){let e=performance.now(),t=await this.sql.begin(async a=>(await a`
60
+ WITH updated_node AS (
61
+ UPDATE node_executions
62
+ SET status = 'FAILED', error = ${n}, completed_at = NOW()
63
+ WHERE node_instance_id = ${o}
64
+ RETURNING cascade_id
65
+ )
66
+ UPDATE cascades
67
+ SET status = 'ERROR', updated_at = NOW()
68
+ WHERE id IN (SELECT cascade_id FROM updated_node)
69
+ `,{status:"FAILED"})),s=performance.now()-e;return console.log(`markExecutionFailed latency: ${s.toFixed(2)}ms`,{cascadeId:c,nodeInstanceId:o}),t}async recordContextEvents(o){let c=performance.now(),{cascadeId:n,functionId:e,updates:t}=o,s=Object.entries(t).map(([d,i])=>({key:d,value:this.sql.json(i),function_id:e,cascade_id:n,created_at:new Date}));if(s.length===0){let d=performance.now()-c;return console.log(`recordContextEvents latency: ${d.toFixed(2)}ms (no events)`,{cascadeId:n,functionId:e}),{status:"SUCCESS"}}await this.sql`
70
+ INSERT INTO context_events ${this.sql(s,"key","value","function_id","cascade_id","created_at")}
71
+ `;let a=performance.now()-c;return console.log(`recordContextEvents latency: ${a.toFixed(2)}ms (${s.length} events)`,{cascadeId:n,functionId:e}),{status:"SUCCESS"}}async hydrateCascadeContext(o,c){let n=performance.now(),e=await this.sql`
72
+ SELECT key, value, function_id, created_at
73
+ FROM context_events
74
+ WHERE cascade_id = ${o}
75
+ AND function_id < ${c}
76
+ ORDER BY function_id ASC, created_at ASC
77
+ `,t={};for(let a of e){let d=a.key,i=a.value;if(t[d]||(t[d]=[]),typeof i=="object"&&i!==null&&!Array.isArray(i)&&"index"in i){let r=i.index;t[d][r]=i}else t[d].push(i)}let s=performance.now()-n;return console.log(`hydrateCascadeContext latency: ${s.toFixed(2)}ms (${e.length} events)`,{cascadeId:o,upToFunctionId:c}),t}async forkCascade(o){let c=performance.now(),{sourceCascadeId:n,newCascadeId:e,upToFunctionId:t}=o;await this.sql.begin(async a=>{await a`
78
+ INSERT INTO cascades (id, user_id, status, fn_id, created_at, updated_at)
79
+ SELECT
80
+ ${e},
81
+ user_id,
82
+ 'RUNNING',
83
+ ${t-1},
84
+ NOW(),
85
+ NOW()
86
+ FROM cascades
87
+ WHERE id = ${n}
88
+ `,await a`
89
+ INSERT INTO node_executions (
90
+ id,
91
+ node_instance_id,
92
+ cascade_id,
93
+ node_name,
94
+ function_id,
95
+ input_context,
96
+ location,
97
+ status,
98
+ started_at,
99
+ completed_at,
100
+ full_output,
101
+ error
102
+ )
103
+ SELECT
104
+ gen_random_uuid(),
105
+ node_instance_id,
106
+ ${e},
107
+ node_name,
108
+ function_id,
109
+ input_context,
110
+ location,
111
+ status,
112
+ started_at,
113
+ completed_at,
114
+ full_output,
115
+ error
116
+ FROM node_executions
117
+ WHERE cascade_id = ${n}
118
+ AND function_id < ${t}
119
+ `,await a`
120
+ INSERT INTO context_events (
121
+ key,
122
+ value,
123
+ function_id,
124
+ cascade_id,
125
+ created_at
126
+ )
127
+ SELECT
128
+ key,
129
+ value,
130
+ function_id,
131
+ ${e},
132
+ created_at
133
+ FROM context_events
134
+ WHERE cascade_id = ${n}
135
+ AND function_id < ${t}
136
+ `});let s=performance.now()-c;return console.log(`forkCascade latency: ${s.toFixed(2)}ms`,{sourceCascadeId:n,newCascadeId:e,upToFunctionId:t}),{newCascadeId:e,status:"SUCCESS"}}};export{u as PostgresPersistor};
package/package.json ADDED
@@ -0,0 +1,34 @@
1
+ {
2
+ "name": "@cascaide-ts/postgres-js",
3
+ "version": "0.1.0",
4
+ "files": [
5
+ "dist"
6
+ ],
7
+ "type": "module",
8
+ "main": "./dist/index.cjs",
9
+ "module": "./dist/index.js",
10
+ "types": "./dist/index.d.ts",
11
+ "exports": {
12
+ ".": {
13
+ "types": "./dist/index.d.ts",
14
+ "import": "./dist/index.js",
15
+ "require": "./dist/index.cjs"
16
+ }
17
+ },
18
+ "publishConfig": {
19
+ "access": "public"
20
+ },
21
+ "peerDependencies": {
22
+ "postgres": "^3.4.0",
23
+ "@cascaide-ts/core": "0.1.0"
24
+ },
25
+ "devDependencies": {
26
+ "postgres": "^3.4.0",
27
+ "tsup": "^8.0.0",
28
+ "@cascaide-ts/core": "0.1.0"
29
+ },
30
+ "scripts": {
31
+ "build": "tsup",
32
+ "dev": "tsup --watch"
33
+ }
34
+ }