@tanstack/workflow-store-drizzle-postgres 0.0.1 → 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,
@@ -498,12 +500,26 @@ function createDrizzlePostgresWorkflowStore(options) {
498
500
  },
499
501
  async claimDueScheduleBuckets(args) {
500
502
  const schedules = await queryRows(db, drizzle_orm.sql`
501
- select *
502
- from ${tableSql.schedules}
503
- where enabled = true
504
- and next_fire_at is not null
505
- and next_fire_at <= ${args.now}
506
- order by next_fire_at asc, schedule_id asc
503
+ select schedule.*
504
+ from ${tableSql.schedules} schedule
505
+ left join ${tableSql.scheduleBuckets} bucket
506
+ on bucket.schedule_id = schedule.schedule_id
507
+ and bucket.bucket_id = schedule.next_fire_at::text
508
+ where schedule.enabled = true
509
+ and schedule.next_fire_at is not null
510
+ and schedule.next_fire_at <= ${args.now}
511
+ and (
512
+ bucket.schedule_id is null
513
+ or (
514
+ bucket.status <> 'started'
515
+ and (
516
+ bucket.lease_owner is null
517
+ or bucket.lease_owner = ${args.leaseOwner}
518
+ or bucket.lease_expires_at <= ${args.now}
519
+ )
520
+ )
521
+ )
522
+ order by schedule.next_fire_at asc, schedule.schedule_id asc
507
523
  limit ${args.limit}
508
524
  `);
509
525
  const buckets = [];
@@ -620,125 +636,16 @@ function createDrizzlePostgresWorkflowStore(options) {
620
636
  }
621
637
  function tableSqls(schema, tables) {
622
638
  return {
623
- runs: drizzle_orm.sql.raw(qualifiedTableName(schema, tables.runs)),
624
- runStates: drizzle_orm.sql.raw(qualifiedTableName(schema, tables.runStates)),
625
- eventLocks: drizzle_orm.sql.raw(qualifiedTableName(schema, tables.eventLocks)),
626
- events: drizzle_orm.sql.raw(qualifiedTableName(schema, tables.events)),
627
- timers: drizzle_orm.sql.raw(qualifiedTableName(schema, tables.timers)),
628
- signalDeliveries: drizzle_orm.sql.raw(qualifiedTableName(schema, tables.signalDeliveries)),
629
- schedules: drizzle_orm.sql.raw(qualifiedTableName(schema, tables.schedules)),
630
- 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))
631
647
  };
632
648
  }
633
- function schemaStatements(schema, tables) {
634
- const runs = qualifiedTableName(schema, tables.runs);
635
- const runStates = qualifiedTableName(schema, tables.runStates);
636
- const eventLocks = qualifiedTableName(schema, tables.eventLocks);
637
- const events = qualifiedTableName(schema, tables.events);
638
- const timers = qualifiedTableName(schema, tables.timers);
639
- const signalDeliveries = qualifiedTableName(schema, tables.signalDeliveries);
640
- const schedules = qualifiedTableName(schema, tables.schedules);
641
- const scheduleBuckets = qualifiedTableName(schema, tables.scheduleBuckets);
642
- return [
643
- ...schema ? [`create schema if not exists ${quoteIdent(schema)}`] : [],
644
- `create table if not exists ${runs} (
645
- run_id text primary key,
646
- workflow_id text not null,
647
- workflow_version text,
648
- status text not null,
649
- input jsonb not null,
650
- output jsonb,
651
- error jsonb,
652
- waiting_for jsonb,
653
- pending_approval jsonb,
654
- wake_at bigint,
655
- lease_owner text,
656
- lease_expires_at bigint,
657
- created_at bigint not null,
658
- updated_at bigint not null
659
- )`,
660
- `create index if not exists ${quoteIdent(`${tables.runs}_status_idx`)}
661
- on ${runs} (status, updated_at)`,
662
- `create index if not exists ${quoteIdent(`${tables.runs}_lease_idx`)}
663
- on ${runs} (status, lease_expires_at)`,
664
- `create table if not exists ${runStates} (
665
- run_id text primary key,
666
- workflow_id text not null,
667
- workflow_version text,
668
- status text not null,
669
- input jsonb not null,
670
- output jsonb,
671
- error jsonb,
672
- waiting_for jsonb,
673
- pending_approval jsonb,
674
- created_at bigint not null,
675
- updated_at bigint not null
676
- )`,
677
- `create table if not exists ${eventLocks} (
678
- run_id text primary key,
679
- created_at bigint not null
680
- )`,
681
- `create table if not exists ${events} (
682
- run_id text not null,
683
- event_index integer not null,
684
- event_type text not null,
685
- step_id text,
686
- event jsonb not null,
687
- created_at bigint not null,
688
- primary key (run_id, event_index)
689
- )`,
690
- `create index if not exists ${quoteIdent(`${tables.events}_type_idx`)}
691
- on ${events} (run_id, event_type)`,
692
- `create table if not exists ${timers} (
693
- run_id text not null,
694
- signal_id text not null,
695
- workflow_id text not null,
696
- workflow_version text,
697
- wake_at bigint not null,
698
- lease_owner text,
699
- lease_expires_at bigint,
700
- primary key (run_id, signal_id)
701
- )`,
702
- `create index if not exists ${quoteIdent(`${tables.timers}_due_idx`)}
703
- on ${timers} (wake_at, lease_expires_at)`,
704
- `create table if not exists ${signalDeliveries} (
705
- run_id text not null,
706
- signal_id text not null,
707
- created_at bigint not null,
708
- primary key (run_id, signal_id)
709
- )`,
710
- `create table if not exists ${schedules} (
711
- schedule_id text primary key,
712
- workflow_id text not null,
713
- workflow_version text,
714
- schedule jsonb not null,
715
- overlap_policy text not null,
716
- input jsonb,
717
- next_fire_at bigint,
718
- enabled boolean not null,
719
- updated_at bigint not null
720
- )`,
721
- `create index if not exists ${quoteIdent(`${tables.schedules}_due_idx`)}
722
- on ${schedules} (enabled, next_fire_at)`,
723
- `create table if not exists ${scheduleBuckets} (
724
- schedule_id text not null,
725
- bucket_id text not null,
726
- workflow_id text not null,
727
- workflow_version text,
728
- run_id text not null,
729
- fire_at bigint not null,
730
- input jsonb,
731
- overlap_policy text not null,
732
- status text not null,
733
- lease_owner text,
734
- lease_expires_at bigint,
735
- started_at bigint,
736
- primary key (schedule_id, bucket_id)
737
- )`,
738
- `create index if not exists ${quoteIdent(`${tables.scheduleBuckets}_lease_idx`)}
739
- on ${scheduleBuckets} (status, fire_at, lease_expires_at)`
740
- ];
741
- }
742
649
  async function loadRunById(db, tables, runId) {
743
650
  const rows = await queryRows(db, drizzle_orm.sql`
744
651
  select *
@@ -817,6 +724,7 @@ function runFromRow(row) {
817
724
  input: decodeJson(row.input),
818
725
  output: decodeJsonOrUndefined(row.output),
819
726
  error: decodeJsonOrUndefined(row.error),
727
+ awaiting: decodeJsonOrUndefined(row.awaiting),
820
728
  waitingFor: decodeJsonOrUndefined(row.waiting_for),
821
729
  pendingApproval: decodeJsonOrUndefined(row.pending_approval),
822
730
  wakeAt: numberOrUndefined(row.wake_at),
@@ -837,6 +745,7 @@ function runStateFromRow(row) {
837
745
  input: decodeJson(row.input),
838
746
  output: decodeJsonOrUndefined(row.output),
839
747
  error: decodeJsonOrUndefined(row.error),
748
+ awaiting: decodeJsonOrUndefined(row.awaiting),
840
749
  waitingFor: decodeJsonOrUndefined(row.waiting_for),
841
750
  pendingApproval: decodeJsonOrUndefined(row.pending_approval),
842
751
  createdAt: Number(row.created_at),
@@ -862,6 +771,15 @@ function timerFromRow(row) {
862
771
  signalId: row.signal_id
863
772
  };
864
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
+ }
865
783
  function scheduleFromRow(row) {
866
784
  return {
867
785
  scheduleId: row.schedule_id,
@@ -891,6 +809,7 @@ function toRunSummary(row) {
891
809
  workflowId: run.workflowId,
892
810
  workflowVersion: run.workflowVersion,
893
811
  status: run.status,
812
+ awaiting: run.awaiting,
894
813
  waitingFor: run.waitingFor,
895
814
  pendingApproval: run.pendingApproval,
896
815
  wakeAt: run.wakeAt,
@@ -922,14 +841,7 @@ function numberOrUndefined(value) {
922
841
  function getStepId(event) {
923
842
  return "stepId" in event ? event.stepId : void 0;
924
843
  }
925
- function qualifiedTableName(schema, table) {
926
- return schema ? `${quoteIdent(schema)}.${quoteIdent(table)}` : quoteIdent(table);
927
- }
928
- function quoteIdent(identifier) {
929
- return `"${identifier.replaceAll("\"", "\"\"")}"`;
930
- }
931
844
 
932
845
  //#endregion
933
846
  exports.createDrizzlePostgresWorkflowStore = createDrizzlePostgresWorkflowStore;
934
- exports.defaultDrizzlePostgresWorkflowStoreTables = defaultDrizzlePostgresWorkflowStoreTables;
935
847
  //# sourceMappingURL=store.cjs.map