@tanstack/workflow-store-drizzle-postgres 0.0.2 → 0.0.3

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.
@@ -0,0 +1,165 @@
1
+ //#region src/schema-contract.ts
2
+ const defaultDrizzlePostgresWorkflowStoreTables = {
3
+ schemaMigrations: "workflow_schema_migrations",
4
+ runs: "workflow_runs",
5
+ runStates: "workflow_run_states",
6
+ eventLocks: "workflow_event_locks",
7
+ events: "workflow_events",
8
+ timers: "workflow_timers",
9
+ signalDeliveries: "workflow_signal_deliveries",
10
+ schedules: "workflow_schedules",
11
+ scheduleBuckets: "workflow_schedule_buckets"
12
+ };
13
+ function resolveDrizzlePostgresWorkflowStoreTables(tables) {
14
+ return {
15
+ ...defaultDrizzlePostgresWorkflowStoreTables,
16
+ ...tables
17
+ };
18
+ }
19
+ function getDrizzlePostgresWorkflowStoreSchemaStatements(options = {}) {
20
+ const tables = resolveDrizzlePostgresWorkflowStoreTables(options.tables);
21
+ const schemaMigrations = qualifiedTableName(options.schema, tables.schemaMigrations);
22
+ const runs = qualifiedTableName(options.schema, tables.runs);
23
+ const runStates = qualifiedTableName(options.schema, tables.runStates);
24
+ const eventLocks = qualifiedTableName(options.schema, tables.eventLocks);
25
+ const events = qualifiedTableName(options.schema, tables.events);
26
+ const timers = qualifiedTableName(options.schema, tables.timers);
27
+ const signalDeliveries = qualifiedTableName(options.schema, tables.signalDeliveries);
28
+ const schedules = qualifiedTableName(options.schema, tables.schedules);
29
+ const scheduleBuckets = qualifiedTableName(options.schema, tables.scheduleBuckets);
30
+ return [
31
+ ...options.schema ? [`create schema if not exists ${quoteIdent(options.schema)}`] : [],
32
+ `create table if not exists ${schemaMigrations} (
33
+ migration_id text primary key,
34
+ package_name text not null,
35
+ package_version text,
36
+ applied_at bigint not null
37
+ )`,
38
+ `create table if not exists ${runs} (
39
+ run_id text primary key,
40
+ workflow_id text not null,
41
+ workflow_version text,
42
+ status text not null,
43
+ input jsonb not null,
44
+ output jsonb,
45
+ error jsonb,
46
+ awaiting jsonb,
47
+ waiting_for jsonb,
48
+ pending_approval jsonb,
49
+ wake_at bigint,
50
+ lease_owner text,
51
+ lease_expires_at bigint,
52
+ created_at bigint not null,
53
+ updated_at bigint not null
54
+ )`,
55
+ `alter table ${runs} add column if not exists awaiting jsonb`,
56
+ `create index if not exists ${quoteIdent(`${tables.runs}_status_idx`)}
57
+ on ${runs} (status, updated_at)`,
58
+ `create index if not exists ${quoteIdent(`${tables.runs}_lease_idx`)}
59
+ on ${runs} (status, lease_expires_at)`,
60
+ `create table if not exists ${runStates} (
61
+ run_id text primary key,
62
+ workflow_id text not null,
63
+ workflow_version text,
64
+ status text not null,
65
+ input jsonb not null,
66
+ output jsonb,
67
+ error jsonb,
68
+ awaiting jsonb,
69
+ waiting_for jsonb,
70
+ pending_approval jsonb,
71
+ created_at bigint not null,
72
+ updated_at bigint not null
73
+ )`,
74
+ `alter table ${runStates} add column if not exists awaiting jsonb`,
75
+ `create table if not exists ${eventLocks} (
76
+ run_id text primary key,
77
+ created_at bigint not null
78
+ )`,
79
+ `create table if not exists ${events} (
80
+ run_id text not null,
81
+ event_index integer not null,
82
+ event_type text not null,
83
+ step_id text,
84
+ event jsonb not null,
85
+ created_at bigint not null,
86
+ primary key (run_id, event_index)
87
+ )`,
88
+ `create index if not exists ${quoteIdent(`${tables.events}_type_idx`)}
89
+ on ${events} (run_id, event_type)`,
90
+ `create table if not exists ${timers} (
91
+ run_id text not null,
92
+ signal_id text not null,
93
+ workflow_id text not null,
94
+ workflow_version text,
95
+ wake_at bigint not null,
96
+ lease_owner text,
97
+ lease_expires_at bigint,
98
+ primary key (run_id, signal_id)
99
+ )`,
100
+ `create index if not exists ${quoteIdent(`${tables.timers}_due_idx`)}
101
+ on ${timers} (wake_at, lease_expires_at)`,
102
+ `create table if not exists ${signalDeliveries} (
103
+ run_id text not null,
104
+ signal_id text not null,
105
+ created_at bigint not null,
106
+ primary key (run_id, signal_id)
107
+ )`,
108
+ `create table if not exists ${schedules} (
109
+ schedule_id text primary key,
110
+ workflow_id text not null,
111
+ workflow_version text,
112
+ schedule jsonb not null,
113
+ overlap_policy text not null,
114
+ input jsonb,
115
+ next_fire_at bigint,
116
+ enabled boolean not null,
117
+ updated_at bigint not null
118
+ )`,
119
+ `create index if not exists ${quoteIdent(`${tables.schedules}_due_idx`)}
120
+ on ${schedules} (enabled, next_fire_at)`,
121
+ `create table if not exists ${scheduleBuckets} (
122
+ schedule_id text not null,
123
+ bucket_id text not null,
124
+ workflow_id text not null,
125
+ workflow_version text,
126
+ run_id text not null,
127
+ fire_at bigint not null,
128
+ input jsonb,
129
+ overlap_policy text not null,
130
+ status text not null,
131
+ lease_owner text,
132
+ lease_expires_at bigint,
133
+ started_at bigint,
134
+ primary key (schedule_id, bucket_id)
135
+ )`,
136
+ `create index if not exists ${quoteIdent(`${tables.scheduleBuckets}_lease_idx`)}
137
+ on ${scheduleBuckets} (status, fire_at, lease_expires_at)`,
138
+ `insert into ${schemaMigrations} (
139
+ migration_id,
140
+ package_name,
141
+ package_version,
142
+ applied_at
143
+ )
144
+ values (
145
+ '0000_workflow_store',
146
+ '@tanstack/workflow-store-drizzle-postgres',
147
+ null,
148
+ (extract(epoch from now()) * 1000)::bigint
149
+ )
150
+ on conflict (migration_id) do nothing`
151
+ ].map(normalizeSqlStatement);
152
+ }
153
+ function qualifiedTableName(schema, table) {
154
+ return schema ? `${quoteIdent(schema)}.${quoteIdent(table)}` : quoteIdent(table);
155
+ }
156
+ function quoteIdent(identifier) {
157
+ return `"${identifier.replaceAll("\"", "\"\"")}"`;
158
+ }
159
+ function normalizeSqlStatement(statement) {
160
+ return statement.split("\n").map((line, index) => index === 0 ? line.trimEnd() : line.replace(/^ {4}/, "").trimEnd()).join("\n");
161
+ }
162
+
163
+ //#endregion
164
+ export { defaultDrizzlePostgresWorkflowStoreTables, getDrizzlePostgresWorkflowStoreSchemaStatements, qualifiedTableName, resolveDrizzlePostgresWorkflowStoreTables };
165
+ //# sourceMappingURL=schema-contract.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"schema-contract.js","names":[],"sources":["../src/schema-contract.ts"],"sourcesContent":["export interface DrizzlePostgresWorkflowStoreTables {\n schemaMigrations: string\n runs: string\n runStates: string\n eventLocks: string\n events: string\n timers: string\n signalDeliveries: string\n schedules: string\n scheduleBuckets: string\n}\n\nexport interface DrizzlePostgresWorkflowStoreSchemaOptions {\n schema?: string\n tables?: Partial<DrizzlePostgresWorkflowStoreTables>\n}\n\nexport const defaultDrizzlePostgresWorkflowStoreTables: DrizzlePostgresWorkflowStoreTables =\n {\n schemaMigrations: 'workflow_schema_migrations',\n runs: 'workflow_runs',\n runStates: 'workflow_run_states',\n eventLocks: 'workflow_event_locks',\n events: 'workflow_events',\n timers: 'workflow_timers',\n signalDeliveries: 'workflow_signal_deliveries',\n schedules: 'workflow_schedules',\n scheduleBuckets: 'workflow_schedule_buckets',\n }\n\nexport function resolveDrizzlePostgresWorkflowStoreTables(\n tables?: Partial<DrizzlePostgresWorkflowStoreTables>,\n): DrizzlePostgresWorkflowStoreTables {\n return {\n ...defaultDrizzlePostgresWorkflowStoreTables,\n ...tables,\n }\n}\n\nexport function getDrizzlePostgresWorkflowStoreSchemaStatements(\n options: DrizzlePostgresWorkflowStoreSchemaOptions = {},\n): Array<string> {\n const tables = resolveDrizzlePostgresWorkflowStoreTables(options.tables)\n const schemaMigrations = qualifiedTableName(\n options.schema,\n tables.schemaMigrations,\n )\n const runs = qualifiedTableName(options.schema, tables.runs)\n const runStates = qualifiedTableName(options.schema, tables.runStates)\n const eventLocks = qualifiedTableName(options.schema, tables.eventLocks)\n const events = qualifiedTableName(options.schema, tables.events)\n const timers = qualifiedTableName(options.schema, tables.timers)\n const signalDeliveries = qualifiedTableName(\n options.schema,\n tables.signalDeliveries,\n )\n const schedules = qualifiedTableName(options.schema, tables.schedules)\n const scheduleBuckets = qualifiedTableName(\n options.schema,\n tables.scheduleBuckets,\n )\n\n return [\n ...(options.schema\n ? [`create schema if not exists ${quoteIdent(options.schema)}`]\n : []),\n `create table if not exists ${schemaMigrations} (\n migration_id text primary key,\n package_name text not null,\n package_version text,\n applied_at bigint not null\n )`,\n `create table if not exists ${runs} (\n run_id text primary key,\n workflow_id text not null,\n workflow_version text,\n status text not null,\n input jsonb not null,\n output jsonb,\n error jsonb,\n awaiting jsonb,\n waiting_for jsonb,\n pending_approval jsonb,\n wake_at bigint,\n lease_owner text,\n lease_expires_at bigint,\n created_at bigint not null,\n updated_at bigint not null\n )`,\n `alter table ${runs} add column if not exists awaiting jsonb`,\n `create index if not exists ${quoteIdent(`${tables.runs}_status_idx`)}\n on ${runs} (status, updated_at)`,\n `create index if not exists ${quoteIdent(`${tables.runs}_lease_idx`)}\n on ${runs} (status, lease_expires_at)`,\n `create table if not exists ${runStates} (\n run_id text primary key,\n workflow_id text not null,\n workflow_version text,\n status text not null,\n input jsonb not null,\n output jsonb,\n error jsonb,\n awaiting jsonb,\n waiting_for jsonb,\n pending_approval jsonb,\n created_at bigint not null,\n updated_at bigint not null\n )`,\n `alter table ${runStates} add column if not exists awaiting jsonb`,\n `create table if not exists ${eventLocks} (\n run_id text primary key,\n created_at bigint not null\n )`,\n `create table if not exists ${events} (\n run_id text not null,\n event_index integer not null,\n event_type text not null,\n step_id text,\n event jsonb not null,\n created_at bigint not null,\n primary key (run_id, event_index)\n )`,\n `create index if not exists ${quoteIdent(`${tables.events}_type_idx`)}\n on ${events} (run_id, event_type)`,\n `create table if not exists ${timers} (\n run_id text not null,\n signal_id text not null,\n workflow_id text not null,\n workflow_version text,\n wake_at bigint not null,\n lease_owner text,\n lease_expires_at bigint,\n primary key (run_id, signal_id)\n )`,\n `create index if not exists ${quoteIdent(`${tables.timers}_due_idx`)}\n on ${timers} (wake_at, lease_expires_at)`,\n `create table if not exists ${signalDeliveries} (\n run_id text not null,\n signal_id text not null,\n created_at bigint not null,\n primary key (run_id, signal_id)\n )`,\n `create table if not exists ${schedules} (\n schedule_id text primary key,\n workflow_id text not null,\n workflow_version text,\n schedule jsonb not null,\n overlap_policy text not null,\n input jsonb,\n next_fire_at bigint,\n enabled boolean not null,\n updated_at bigint not null\n )`,\n `create index if not exists ${quoteIdent(`${tables.schedules}_due_idx`)}\n on ${schedules} (enabled, next_fire_at)`,\n `create table if not exists ${scheduleBuckets} (\n schedule_id text not null,\n bucket_id text not null,\n workflow_id text not null,\n workflow_version text,\n run_id text not null,\n fire_at bigint not null,\n input jsonb,\n overlap_policy text not null,\n status text not null,\n lease_owner text,\n lease_expires_at bigint,\n started_at bigint,\n primary key (schedule_id, bucket_id)\n )`,\n `create index if not exists ${quoteIdent(\n `${tables.scheduleBuckets}_lease_idx`,\n )}\n on ${scheduleBuckets} (status, fire_at, lease_expires_at)`,\n `insert into ${schemaMigrations} (\n migration_id,\n package_name,\n package_version,\n applied_at\n )\n values (\n '0000_workflow_store',\n '@tanstack/workflow-store-drizzle-postgres',\n null,\n (extract(epoch from now()) * 1000)::bigint\n )\n on conflict (migration_id) do nothing`,\n ].map(normalizeSqlStatement)\n}\n\nexport function qualifiedTableName(schema: string | undefined, table: string) {\n return schema\n ? `${quoteIdent(schema)}.${quoteIdent(table)}`\n : quoteIdent(table)\n}\n\nfunction quoteIdent(identifier: string) {\n return `\"${identifier.replaceAll('\"', '\"\"')}\"`\n}\n\nfunction normalizeSqlStatement(statement: string) {\n return statement\n .split('\\n')\n .map((line, index) =>\n index === 0 ? line.trimEnd() : line.replace(/^ {4}/, '').trimEnd(),\n )\n .join('\\n')\n}\n"],"mappings":";AAiBA,MAAa,4CACX;CACE,kBAAkB;CAClB,MAAM;CACN,WAAW;CACX,YAAY;CACZ,QAAQ;CACR,QAAQ;CACR,kBAAkB;CAClB,WAAW;CACX,iBAAiB;CAClB;AAEH,SAAgB,0CACd,QACoC;AACpC,QAAO;EACL,GAAG;EACH,GAAG;EACJ;;AAGH,SAAgB,gDACd,UAAqD,EAAE,EACxC;CACf,MAAM,SAAS,0CAA0C,QAAQ,OAAO;CACxE,MAAM,mBAAmB,mBACvB,QAAQ,QACR,OAAO,iBACR;CACD,MAAM,OAAO,mBAAmB,QAAQ,QAAQ,OAAO,KAAK;CAC5D,MAAM,YAAY,mBAAmB,QAAQ,QAAQ,OAAO,UAAU;CACtE,MAAM,aAAa,mBAAmB,QAAQ,QAAQ,OAAO,WAAW;CACxE,MAAM,SAAS,mBAAmB,QAAQ,QAAQ,OAAO,OAAO;CAChE,MAAM,SAAS,mBAAmB,QAAQ,QAAQ,OAAO,OAAO;CAChE,MAAM,mBAAmB,mBACvB,QAAQ,QACR,OAAO,iBACR;CACD,MAAM,YAAY,mBAAmB,QAAQ,QAAQ,OAAO,UAAU;CACtE,MAAM,kBAAkB,mBACtB,QAAQ,QACR,OAAO,gBACR;AAED,QAAO;EACL,GAAI,QAAQ,SACR,CAAC,+BAA+B,WAAW,QAAQ,OAAO,GAAG,GAC7D,EAAE;EACN,8BAA8B,iBAAiB;;;;;;EAM/C,8BAA8B,KAAK;;;;;;;;;;;;;;;;;EAiBnC,eAAe,KAAK;EACpB,8BAA8B,WAAW,GAAG,OAAO,KAAK,aAAa,CAAC;WAC/D,KAAK;EACZ,8BAA8B,WAAW,GAAG,OAAO,KAAK,YAAY,CAAC;WAC9D,KAAK;EACZ,8BAA8B,UAAU;;;;;;;;;;;;;;EAcxC,eAAe,UAAU;EACzB,8BAA8B,WAAW;;;;EAIzC,8BAA8B,OAAO;;;;;;;;;EASrC,8BAA8B,WAAW,GAAG,OAAO,OAAO,WAAW,CAAC;WAC/D,OAAO;EACd,8BAA8B,OAAO;;;;;;;;;;EAUrC,8BAA8B,WAAW,GAAG,OAAO,OAAO,UAAU,CAAC;WAC9D,OAAO;EACd,8BAA8B,iBAAiB;;;;;;EAM/C,8BAA8B,UAAU;;;;;;;;;;;EAWxC,8BAA8B,WAAW,GAAG,OAAO,UAAU,UAAU,CAAC;WACjE,UAAU;EACjB,8BAA8B,gBAAgB;;;;;;;;;;;;;;;EAe9C,8BAA8B,WAC5B,GAAG,OAAO,gBAAgB,YAC3B,CAAC;WACK,gBAAgB;EACvB,eAAe,iBAAiB;;;;;;;;;;;;;EAajC,CAAC,IAAI,sBAAsB;;AAG9B,SAAgB,mBAAmB,QAA4B,OAAe;AAC5E,QAAO,SACH,GAAG,WAAW,OAAO,CAAC,GAAG,WAAW,MAAM,KAC1C,WAAW,MAAM;;AAGvB,SAAS,WAAW,YAAoB;AACtC,QAAO,IAAI,WAAW,WAAW,MAAK,OAAK,CAAC;;AAG9C,SAAS,sBAAsB,WAAmB;AAChD,QAAO,UACJ,MAAM,KAAK,CACX,KAAK,MAAM,UACV,UAAU,IAAI,KAAK,SAAS,GAAG,KAAK,QAAQ,SAAS,GAAG,CAAC,SAAS,CACnE,CACA,KAAK,KAAK"}
package/dist/store.cjs CHANGED
@@ -1,27 +1,18 @@
1
+ const require_schema_contract = require('./schema-contract.cjs');
1
2
  let _tanstack_workflow_core = require("@tanstack/workflow-core");
2
3
  let drizzle_orm = require("drizzle-orm");
3
4
 
4
5
  //#region src/store.ts
5
- const defaultDrizzlePostgresWorkflowStoreTables = {
6
- runs: "workflow_runs",
7
- runStates: "workflow_run_states",
8
- eventLocks: "workflow_event_locks",
9
- events: "workflow_events",
10
- timers: "workflow_timers",
11
- signalDeliveries: "workflow_signal_deliveries",
12
- schedules: "workflow_schedules",
13
- scheduleBuckets: "workflow_schedule_buckets"
14
- };
15
6
  function createDrizzlePostgresWorkflowStore(options) {
16
- const tableNames = {
17
- ...defaultDrizzlePostgresWorkflowStoreTables,
18
- ...options.tables
19
- };
7
+ const tableNames = require_schema_contract.resolveDrizzlePostgresWorkflowStoreTables(options.tables);
20
8
  const tableSql = tableSqls(options.schema, tableNames);
21
9
  const db = options.db;
22
10
  return {
23
11
  async ensureSchema() {
24
- for (const statement of schemaStatements(options.schema, tableNames)) await db.execute(drizzle_orm.sql.raw(statement));
12
+ for (const statement of require_schema_contract.getDrizzlePostgresWorkflowStoreSchemaStatements({
13
+ schema: options.schema,
14
+ tables: tableNames
15
+ })) await db.execute(drizzle_orm.sql.raw(statement));
25
16
  },
26
17
  async createRun(args) {
27
18
  const rows = await queryRows(db, drizzle_orm.sql`
@@ -83,6 +74,7 @@ function createDrizzlePostgresWorkflowStore(options) {
83
74
  input,
84
75
  output,
85
76
  error,
77
+ awaiting,
86
78
  waiting_for,
87
79
  pending_approval,
88
80
  created_at,
@@ -96,6 +88,7 @@ function createDrizzlePostgresWorkflowStore(options) {
96
88
  ${encodeJson(state.input)}::jsonb,
97
89
  ${encodeJsonOrNull(state.output)}::jsonb,
98
90
  ${encodeJsonOrNull(state.error)}::jsonb,
91
+ ${encodeJsonOrNull(state.awaiting)}::jsonb,
99
92
  ${encodeJsonOrNull(state.waitingFor)}::jsonb,
100
93
  ${encodeJsonOrNull(state.pendingApproval)}::jsonb,
101
94
  ${state.createdAt},
@@ -108,6 +101,7 @@ function createDrizzlePostgresWorkflowStore(options) {
108
101
  input = excluded.input,
109
102
  output = excluded.output,
110
103
  error = excluded.error,
104
+ awaiting = excluded.awaiting,
111
105
  waiting_for = excluded.waiting_for,
112
106
  pending_approval = excluded.pending_approval,
113
107
  created_at = excluded.created_at,
@@ -122,6 +116,7 @@ function createDrizzlePostgresWorkflowStore(options) {
122
116
  input,
123
117
  output,
124
118
  error,
119
+ awaiting,
125
120
  waiting_for,
126
121
  pending_approval,
127
122
  wake_at,
@@ -136,6 +131,7 @@ function createDrizzlePostgresWorkflowStore(options) {
136
131
  ${encodeJson(state.input)}::jsonb,
137
132
  ${encodeJsonOrNull(state.output)}::jsonb,
138
133
  ${encodeJsonOrNull(state.error)}::jsonb,
134
+ ${encodeJsonOrNull(state.awaiting)}::jsonb,
139
135
  ${encodeJsonOrNull(state.waitingFor)}::jsonb,
140
136
  ${encodeJsonOrNull(state.pendingApproval)}::jsonb,
141
137
  ${state.waitingFor?.signalName === "__timer" ? state.waitingFor.deadline : null},
@@ -149,6 +145,7 @@ function createDrizzlePostgresWorkflowStore(options) {
149
145
  input = excluded.input,
150
146
  output = excluded.output,
151
147
  error = excluded.error,
148
+ awaiting = excluded.awaiting,
152
149
  waiting_for = excluded.waiting_for,
153
150
  pending_approval = excluded.pending_approval,
154
151
  wake_at = excluded.wake_at,
@@ -293,6 +290,7 @@ function createDrizzlePostgresWorkflowStore(options) {
293
290
  update ${tableSql.runs}
294
291
  set
295
292
  status = 'paused',
293
+ awaiting = ${encodeJsonOrNull(args.awaiting)}::jsonb,
296
294
  waiting_for = ${encodeJsonOrNull(args.waitingFor)}::jsonb,
297
295
  pending_approval = ${encodeJsonOrNull(args.pendingApproval)}::jsonb,
298
296
  wake_at = ${args.wakeAt ?? null},
@@ -308,6 +306,7 @@ function createDrizzlePostgresWorkflowStore(options) {
308
306
  set
309
307
  status = 'finished',
310
308
  output = ${encodeJson(args.output)}::jsonb,
309
+ awaiting = null,
311
310
  waiting_for = null,
312
311
  pending_approval = null,
313
312
  wake_at = null,
@@ -324,6 +323,7 @@ function createDrizzlePostgresWorkflowStore(options) {
324
323
  set
325
324
  status = 'errored',
326
325
  error = ${encodeJson(args.error)}::jsonb,
326
+ awaiting = null,
327
327
  waiting_for = null,
328
328
  pending_approval = null,
329
329
  wake_at = null,
@@ -399,7 +399,7 @@ function createDrizzlePostgresWorkflowStore(options) {
399
399
  kind: "duplicate",
400
400
  run
401
401
  };
402
- if (run.waitingFor?.signalName !== args.delivery.name) return {
402
+ if (!isRunWaitingForSignal(run, args.delivery)) return {
403
403
  kind: "not-waiting",
404
404
  run
405
405
  };
@@ -418,6 +418,7 @@ function createDrizzlePostgresWorkflowStore(options) {
418
418
  update ${tableSql.runs}
419
419
  set
420
420
  status = 'queued',
421
+ awaiting = null,
421
422
  waiting_for = null,
422
423
  pending_approval = null,
423
424
  wake_at = null,
@@ -437,7 +438,7 @@ function createDrizzlePostgresWorkflowStore(options) {
437
438
  kind: "duplicate",
438
439
  run
439
440
  };
440
- if (run.pendingApproval?.approvalId !== args.approval.approvalId) return {
441
+ if (!isRunWaitingForApproval(run, args.approval)) return {
441
442
  kind: "not-waiting",
442
443
  run
443
444
  };
@@ -451,6 +452,7 @@ function createDrizzlePostgresWorkflowStore(options) {
451
452
  update ${tableSql.runs}
452
453
  set
453
454
  status = 'queued',
455
+ awaiting = null,
454
456
  waiting_for = null,
455
457
  pending_approval = null,
456
458
  wake_at = null,
@@ -634,125 +636,16 @@ function createDrizzlePostgresWorkflowStore(options) {
634
636
  }
635
637
  function tableSqls(schema, tables) {
636
638
  return {
637
- runs: drizzle_orm.sql.raw(qualifiedTableName(schema, tables.runs)),
638
- runStates: drizzle_orm.sql.raw(qualifiedTableName(schema, tables.runStates)),
639
- eventLocks: drizzle_orm.sql.raw(qualifiedTableName(schema, tables.eventLocks)),
640
- events: drizzle_orm.sql.raw(qualifiedTableName(schema, tables.events)),
641
- timers: drizzle_orm.sql.raw(qualifiedTableName(schema, tables.timers)),
642
- signalDeliveries: drizzle_orm.sql.raw(qualifiedTableName(schema, tables.signalDeliveries)),
643
- schedules: drizzle_orm.sql.raw(qualifiedTableName(schema, tables.schedules)),
644
- scheduleBuckets: drizzle_orm.sql.raw(qualifiedTableName(schema, tables.scheduleBuckets))
639
+ runs: drizzle_orm.sql.raw(require_schema_contract.qualifiedTableName(schema, tables.runs)),
640
+ runStates: drizzle_orm.sql.raw(require_schema_contract.qualifiedTableName(schema, tables.runStates)),
641
+ eventLocks: drizzle_orm.sql.raw(require_schema_contract.qualifiedTableName(schema, tables.eventLocks)),
642
+ events: drizzle_orm.sql.raw(require_schema_contract.qualifiedTableName(schema, tables.events)),
643
+ timers: drizzle_orm.sql.raw(require_schema_contract.qualifiedTableName(schema, tables.timers)),
644
+ signalDeliveries: drizzle_orm.sql.raw(require_schema_contract.qualifiedTableName(schema, tables.signalDeliveries)),
645
+ schedules: drizzle_orm.sql.raw(require_schema_contract.qualifiedTableName(schema, tables.schedules)),
646
+ scheduleBuckets: drizzle_orm.sql.raw(require_schema_contract.qualifiedTableName(schema, tables.scheduleBuckets))
645
647
  };
646
648
  }
647
- function schemaStatements(schema, tables) {
648
- const runs = qualifiedTableName(schema, tables.runs);
649
- const runStates = qualifiedTableName(schema, tables.runStates);
650
- const eventLocks = qualifiedTableName(schema, tables.eventLocks);
651
- const events = qualifiedTableName(schema, tables.events);
652
- const timers = qualifiedTableName(schema, tables.timers);
653
- const signalDeliveries = qualifiedTableName(schema, tables.signalDeliveries);
654
- const schedules = qualifiedTableName(schema, tables.schedules);
655
- const scheduleBuckets = qualifiedTableName(schema, tables.scheduleBuckets);
656
- return [
657
- ...schema ? [`create schema if not exists ${quoteIdent(schema)}`] : [],
658
- `create table if not exists ${runs} (
659
- run_id text primary key,
660
- workflow_id text not null,
661
- workflow_version text,
662
- status text not null,
663
- input jsonb not null,
664
- output jsonb,
665
- error jsonb,
666
- waiting_for jsonb,
667
- pending_approval jsonb,
668
- wake_at bigint,
669
- lease_owner text,
670
- lease_expires_at bigint,
671
- created_at bigint not null,
672
- updated_at bigint not null
673
- )`,
674
- `create index if not exists ${quoteIdent(`${tables.runs}_status_idx`)}
675
- on ${runs} (status, updated_at)`,
676
- `create index if not exists ${quoteIdent(`${tables.runs}_lease_idx`)}
677
- on ${runs} (status, lease_expires_at)`,
678
- `create table if not exists ${runStates} (
679
- run_id text primary key,
680
- workflow_id text not null,
681
- workflow_version text,
682
- status text not null,
683
- input jsonb not null,
684
- output jsonb,
685
- error jsonb,
686
- waiting_for jsonb,
687
- pending_approval jsonb,
688
- created_at bigint not null,
689
- updated_at bigint not null
690
- )`,
691
- `create table if not exists ${eventLocks} (
692
- run_id text primary key,
693
- created_at bigint not null
694
- )`,
695
- `create table if not exists ${events} (
696
- run_id text not null,
697
- event_index integer not null,
698
- event_type text not null,
699
- step_id text,
700
- event jsonb not null,
701
- created_at bigint not null,
702
- primary key (run_id, event_index)
703
- )`,
704
- `create index if not exists ${quoteIdent(`${tables.events}_type_idx`)}
705
- on ${events} (run_id, event_type)`,
706
- `create table if not exists ${timers} (
707
- run_id text not null,
708
- signal_id text not null,
709
- workflow_id text not null,
710
- workflow_version text,
711
- wake_at bigint not null,
712
- lease_owner text,
713
- lease_expires_at bigint,
714
- primary key (run_id, signal_id)
715
- )`,
716
- `create index if not exists ${quoteIdent(`${tables.timers}_due_idx`)}
717
- on ${timers} (wake_at, lease_expires_at)`,
718
- `create table if not exists ${signalDeliveries} (
719
- run_id text not null,
720
- signal_id text not null,
721
- created_at bigint not null,
722
- primary key (run_id, signal_id)
723
- )`,
724
- `create table if not exists ${schedules} (
725
- schedule_id text primary key,
726
- workflow_id text not null,
727
- workflow_version text,
728
- schedule jsonb not null,
729
- overlap_policy text not null,
730
- input jsonb,
731
- next_fire_at bigint,
732
- enabled boolean not null,
733
- updated_at bigint not null
734
- )`,
735
- `create index if not exists ${quoteIdent(`${tables.schedules}_due_idx`)}
736
- on ${schedules} (enabled, next_fire_at)`,
737
- `create table if not exists ${scheduleBuckets} (
738
- schedule_id text not null,
739
- bucket_id text not null,
740
- workflow_id text not null,
741
- workflow_version text,
742
- run_id text not null,
743
- fire_at bigint not null,
744
- input jsonb,
745
- overlap_policy text not null,
746
- status text not null,
747
- lease_owner text,
748
- lease_expires_at bigint,
749
- started_at bigint,
750
- primary key (schedule_id, bucket_id)
751
- )`,
752
- `create index if not exists ${quoteIdent(`${tables.scheduleBuckets}_lease_idx`)}
753
- on ${scheduleBuckets} (status, fire_at, lease_expires_at)`
754
- ];
755
- }
756
649
  async function loadRunById(db, tables, runId) {
757
650
  const rows = await queryRows(db, drizzle_orm.sql`
758
651
  select *
@@ -831,6 +724,7 @@ function runFromRow(row) {
831
724
  input: decodeJson(row.input),
832
725
  output: decodeJsonOrUndefined(row.output),
833
726
  error: decodeJsonOrUndefined(row.error),
727
+ awaiting: decodeJsonOrUndefined(row.awaiting),
834
728
  waitingFor: decodeJsonOrUndefined(row.waiting_for),
835
729
  pendingApproval: decodeJsonOrUndefined(row.pending_approval),
836
730
  wakeAt: numberOrUndefined(row.wake_at),
@@ -851,6 +745,7 @@ function runStateFromRow(row) {
851
745
  input: decodeJson(row.input),
852
746
  output: decodeJsonOrUndefined(row.output),
853
747
  error: decodeJsonOrUndefined(row.error),
748
+ awaiting: decodeJsonOrUndefined(row.awaiting),
854
749
  waitingFor: decodeJsonOrUndefined(row.waiting_for),
855
750
  pendingApproval: decodeJsonOrUndefined(row.pending_approval),
856
751
  createdAt: Number(row.created_at),
@@ -876,6 +771,15 @@ function timerFromRow(row) {
876
771
  signalId: row.signal_id
877
772
  };
878
773
  }
774
+ function isRunWaitingForSignal(run, delivery) {
775
+ return signalAwaitableMatches(run.waitingFor, delivery) || run.awaiting?.some((awaitable) => awaitable.type === "signal" && signalAwaitableMatches(awaitable, delivery)) === true;
776
+ }
777
+ function signalAwaitableMatches(awaitable, delivery) {
778
+ return awaitable?.signalName === delivery.name && (delivery.stepId === void 0 || awaitable.stepId === void 0 || awaitable.stepId === delivery.stepId);
779
+ }
780
+ function isRunWaitingForApproval(run, approval) {
781
+ return run.pendingApproval?.approvalId === approval.approvalId || run.awaiting?.some((awaitable) => awaitable.type === "approval" && awaitable.approvalId === approval.approvalId) === true;
782
+ }
879
783
  function scheduleFromRow(row) {
880
784
  return {
881
785
  scheduleId: row.schedule_id,
@@ -905,6 +809,7 @@ function toRunSummary(row) {
905
809
  workflowId: run.workflowId,
906
810
  workflowVersion: run.workflowVersion,
907
811
  status: run.status,
812
+ awaiting: run.awaiting,
908
813
  waitingFor: run.waitingFor,
909
814
  pendingApproval: run.pendingApproval,
910
815
  wakeAt: run.wakeAt,
@@ -936,14 +841,7 @@ function numberOrUndefined(value) {
936
841
  function getStepId(event) {
937
842
  return "stepId" in event ? event.stepId : void 0;
938
843
  }
939
- function qualifiedTableName(schema, table) {
940
- return schema ? `${quoteIdent(schema)}.${quoteIdent(table)}` : quoteIdent(table);
941
- }
942
- function quoteIdent(identifier) {
943
- return `"${identifier.replaceAll("\"", "\"\"")}"`;
944
- }
945
844
 
946
845
  //#endregion
947
846
  exports.createDrizzlePostgresWorkflowStore = createDrizzlePostgresWorkflowStore;
948
- exports.defaultDrizzlePostgresWorkflowStoreTables = defaultDrizzlePostgresWorkflowStoreTables;
949
847
  //# sourceMappingURL=store.cjs.map