pg-workflows 0.8.3 → 0.10.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/dist/index.d.cts CHANGED
@@ -20,6 +20,9 @@ type WorkflowRun = {
20
20
  maxRetries: number;
21
21
  jobId: string | null;
22
22
  idempotencyKey: string | null;
23
+ parentRunId: string | null;
24
+ parentStepId: string | null;
25
+ parentResourceId: string | null;
23
26
  };
24
27
  import { StandardSchemaV1 } from "@standard-schema/spec";
25
28
  type DurationObject = {
@@ -45,10 +48,18 @@ declare enum StepType {
45
48
  WAIT_FOR = "waitFor",
46
49
  WAIT_UNTIL = "waitUntil",
47
50
  DELAY = "delay",
48
- POLL = "poll"
51
+ POLL = "poll",
52
+ INVOKE_CHILD_WORKFLOW = "invokeChildWorkflow"
49
53
  }
50
54
  type InputParameters = StandardSchemaV1;
51
55
  type InferInputParameters<P extends InputParameters> = StandardSchemaV1.InferOutput<P>;
56
+ type WorkflowRunOptions = {
57
+ resourceId?: string;
58
+ timeout?: number;
59
+ retries?: number;
60
+ expireInSeconds?: number;
61
+ idempotencyKey?: string;
62
+ };
52
63
  type WorkflowOptions<I extends InputParameters> = {
53
64
  timeout?: number;
54
65
  retries?: number;
@@ -88,6 +99,23 @@ type StepBaseContext = {
88
99
  } | {
89
100
  timedOut: true;
90
101
  }>;
102
+ /**
103
+ * Invoke a child workflow from inside the current workflow and pause until
104
+ * the child run reaches a terminal state.
105
+ */
106
+ invokeChildWorkflow: {
107
+ <
108
+ TInput extends InputParameters,
109
+ TOutput = unknown
110
+ >(stepId: string, ref: WorkflowRef<TInput, TOutput>, input: InferInputParameters<TInput>, options?: WorkflowRunOptions): Promise<TOutput>;
111
+ <TOutput = unknown>(stepId: string, params: {
112
+ workflowId: string;
113
+ input: unknown;
114
+ resourceId?: string;
115
+ idempotencyKey?: string;
116
+ options?: WorkflowRunOptions;
117
+ }): Promise<TOutput>;
118
+ };
91
119
  };
92
120
  /**
93
121
  * Plugin that extends the workflow step API with extra methods.
@@ -138,9 +166,12 @@ type WorkflowInternalDefinition<TInput extends InputParameters = InputParameters
138
166
  interface WorkflowFactory<TStepExt = object> {
139
167
  <I extends InputParameters = InputParameters>(id: string, handler: (context: WorkflowContext<I, StepBaseContext & TStepExt>) => Promise<unknown>, options?: WorkflowOptions<I>): WorkflowDefinition<I>;
140
168
  use<TNewExt>(plugin: WorkflowPlugin<StepBaseContext & TStepExt, TNewExt>): WorkflowFactory<TStepExt & TNewExt>;
141
- ref<TInput extends InputParameters = InputParameters>(id: string, options?: {
169
+ ref<
170
+ TInput extends InputParameters = InputParameters,
171
+ TOutput = unknown
172
+ >(id: string, options?: {
142
173
  inputSchema?: TInput;
143
- }): WorkflowRef<TInput>;
174
+ }): WorkflowRef<TInput, TOutput>;
144
175
  }
145
176
  /**
146
177
  * Lightweight workflow reference - carries the workflow ID and input type
@@ -149,7 +180,10 @@ interface WorkflowFactory<TStepExt = object> {
149
180
  *
150
181
  * Callable: pass a handler to create a full WorkflowDefinition.
151
182
  */
152
- interface WorkflowRef<TInput extends InputParameters = InputParameters> {
183
+ interface WorkflowRef<
184
+ TInput extends InputParameters = InputParameters,
185
+ TOutput = unknown
186
+ > {
153
187
  (handler: (context: WorkflowContext<TInput, StepBaseContext>) => Promise<unknown>, options?: Omit<WorkflowOptions<TInput>, "inputSchema">): WorkflowDefinition<TInput>;
154
188
  readonly id: string;
155
189
  readonly inputSchema?: TInput;
@@ -187,13 +221,7 @@ type WorkflowClientOptions = {
187
221
  connectionString: string;
188
222
  pool?: never;
189
223
  });
190
- type StartWorkflowOptions = {
191
- resourceId?: string;
192
- timeout?: number;
193
- retries?: number;
194
- expireInSeconds?: number;
195
- idempotencyKey?: string;
196
- };
224
+ type StartWorkflowOptions = WorkflowRunOptions;
197
225
  declare class WorkflowClient {
198
226
  private boss;
199
227
  private db;
@@ -269,19 +297,17 @@ declare class WorkflowClient {
269
297
  * Create a lightweight workflow reference.
270
298
  * Safe to import from `pg-workflows/client` - no engine or handler code.
271
299
  */
272
- declare function createWorkflowRef<TInput extends InputParameters = InputParameters>(id: string, options?: {
300
+ declare function createWorkflowRef<
301
+ TOutput = unknown,
302
+ TInput extends InputParameters = InputParameters
303
+ >(id: string, options?: {
273
304
  inputSchema?: TInput;
274
- }): WorkflowRef<TInput>;
305
+ }): WorkflowRef<TInput, TOutput>;
275
306
  declare const workflow: WorkflowFactory;
276
307
  import pg2 from "pg";
277
308
  import { Db, PgBoss as PgBoss2 } from "pg-boss";
278
- type StartWorkflowOptions2 = {
279
- resourceId?: string;
280
- timeout?: number;
281
- retries?: number;
282
- expireInSeconds?: number;
309
+ type StartWorkflowOptions2 = WorkflowRunOptions & {
283
310
  batchSize?: number;
284
- idempotencyKey?: string;
285
311
  };
286
312
  type WorkflowEngineOptions = {
287
313
  workflows?: WorkflowDefinition[];
@@ -304,13 +330,15 @@ declare class WorkflowEngine {
304
330
  workflows: Map<string, WorkflowInternalDefinition>;
305
331
  private logger;
306
332
  constructor({ workflows, logger, boss,...connectionOptions }: WorkflowEngineOptions);
307
- start(asEngine?: boolean, { batchSize }?: {
333
+ start(asEngine?: boolean, { batchSize, heartbeatSeconds }?: {
308
334
  batchSize?: number;
335
+ heartbeatSeconds?: number;
309
336
  }): Promise<void>;
310
337
  stop(): Promise<void>;
311
338
  registerWorkflow(definition: WorkflowDefinition<InputParameters>): Promise<WorkflowEngine>;
312
339
  unregisterWorkflow(workflowId: string): Promise<WorkflowEngine>;
313
340
  unregisterAllWorkflows(): Promise<WorkflowEngine>;
341
+ private resolveWorkflowRunParameters;
314
342
  startWorkflow<TInput extends InputParameters>(ref: WorkflowRef<TInput>, input: InferInputParameters<TInput>, options?: StartWorkflowOptions2): Promise<WorkflowRun>;
315
343
  startWorkflow(params: {
316
344
  resourceId?: string;
@@ -319,6 +347,9 @@ declare class WorkflowEngine {
319
347
  idempotencyKey?: string;
320
348
  options?: StartWorkflowOptions2;
321
349
  }): Promise<WorkflowRun>;
350
+ private createWorkflowRun;
351
+ private enqueueWorkflowRun;
352
+ private notifyParentOfChildTerminalRun;
322
353
  pauseWorkflow({ runId, resourceId }: {
323
354
  runId: string;
324
355
  resourceId?: string;
@@ -375,8 +406,34 @@ declare class WorkflowEngine {
375
406
  */
376
407
  private resolveScopedResourceId;
377
408
  private handleWorkflowRun;
409
+ /**
410
+ * Reconciles workflow runs whose retries pg-boss has exhausted (handler
411
+ * threw on the final attempt, or worker died and missed the heartbeat
412
+ * past the retry budget). The DLQ entry tells us the run is unrecoverable;
413
+ * we mark it FAILED with whatever error message the catch block last
414
+ * persisted, falling back to a worker-death message.
415
+ */
416
+ private handleWorkflowRunDlq;
378
417
  private getCachedStepEntry;
379
418
  private getWaitForStepEntry;
419
+ private getInvokeChildWorkflowStepEntry;
420
+ /**
421
+ * Returns the cached output for a COMPLETED child run. Treats `undefined`
422
+ * outputs as `{}` so the parent timeline always has a defined value.
423
+ * Caller must ensure `childRun.status === COMPLETED` before calling.
424
+ */
425
+ private getCompletedChildOutput;
426
+ /**
427
+ * Throws a `WorkflowEngineError` describing why an invoked child run did not
428
+ * produce output (it FAILED or was CANCELLED). The throw aborts the parent
429
+ * step, which is then caught by `handleWorkflowRun` and marks the parent
430
+ * FAILED with the same message — no fake sentinel value is ever written to
431
+ * the parent timeline.
432
+ */
433
+ private throwForNonCompletedChild;
434
+ private assertInvokeChildWorkflowStepOwnership;
435
+ private invokeChildWorkflowStep;
436
+ private pauseRunForWait;
380
437
  private runStep;
381
438
  private waitStep;
382
439
  private pollStep;
@@ -398,6 +455,8 @@ declare class WorkflowEngine {
398
455
  }>;
399
456
  }
400
457
  import { StandardSchemaV1 as StandardSchemaV12 } from "@standard-schema/spec";
458
+ declare function validateWorkflowId(workflowId: string): void;
459
+ declare function validateResourceId(resourceId: string | undefined | null): void;
401
460
  declare class WorkflowEngineError extends Error {
402
461
  readonly workflowId?: string | undefined;
403
462
  readonly runId?: string | undefined;
@@ -408,4 +467,4 @@ declare class WorkflowEngineError extends Error {
408
467
  declare class WorkflowRunNotFoundError extends WorkflowEngineError {
409
468
  constructor(runId?: string, workflowId?: string);
410
469
  }
411
- export { workflow, parseDuration, createWorkflowRef, WorkflowStatus, WorkflowRunProgress, WorkflowRunNotFoundError, WorkflowRef, WorkflowPlugin, WorkflowOptions, WorkflowLogger, WorkflowInternalLoggerContext, WorkflowInternalLogger, WorkflowInternalDefinition, WorkflowFactory, WorkflowEngineOptions, WorkflowEngineError, WorkflowEngine, WorkflowDefinition, WorkflowContext, WorkflowClientOptions, WorkflowClient, StepType, StepInternalDefinition, StepBaseContext, StartWorkflowOptions, InputParameters, InferInputParameters, DurationObject, Duration };
470
+ export { workflow, validateWorkflowId, validateResourceId, parseDuration, createWorkflowRef, WorkflowStatus, WorkflowRunProgress, WorkflowRunOptions, WorkflowRunNotFoundError, WorkflowRef, WorkflowPlugin, WorkflowOptions, WorkflowLogger, WorkflowInternalLoggerContext, WorkflowInternalLogger, WorkflowInternalDefinition, WorkflowFactory, WorkflowEngineOptions, WorkflowEngineError, WorkflowEngine, WorkflowDefinition, WorkflowContext, WorkflowClientOptions, WorkflowClient, StepType, StepInternalDefinition, StepBaseContext, StartWorkflowOptions, InputParameters, InferInputParameters, DurationObject, Duration };
package/dist/index.d.ts CHANGED
@@ -20,6 +20,9 @@ type WorkflowRun = {
20
20
  maxRetries: number;
21
21
  jobId: string | null;
22
22
  idempotencyKey: string | null;
23
+ parentRunId: string | null;
24
+ parentStepId: string | null;
25
+ parentResourceId: string | null;
23
26
  };
24
27
  import { StandardSchemaV1 } from "@standard-schema/spec";
25
28
  type DurationObject = {
@@ -45,10 +48,18 @@ declare enum StepType {
45
48
  WAIT_FOR = "waitFor",
46
49
  WAIT_UNTIL = "waitUntil",
47
50
  DELAY = "delay",
48
- POLL = "poll"
51
+ POLL = "poll",
52
+ INVOKE_CHILD_WORKFLOW = "invokeChildWorkflow"
49
53
  }
50
54
  type InputParameters = StandardSchemaV1;
51
55
  type InferInputParameters<P extends InputParameters> = StandardSchemaV1.InferOutput<P>;
56
+ type WorkflowRunOptions = {
57
+ resourceId?: string;
58
+ timeout?: number;
59
+ retries?: number;
60
+ expireInSeconds?: number;
61
+ idempotencyKey?: string;
62
+ };
52
63
  type WorkflowOptions<I extends InputParameters> = {
53
64
  timeout?: number;
54
65
  retries?: number;
@@ -88,6 +99,23 @@ type StepBaseContext = {
88
99
  } | {
89
100
  timedOut: true;
90
101
  }>;
102
+ /**
103
+ * Invoke a child workflow from inside the current workflow and pause until
104
+ * the child run reaches a terminal state.
105
+ */
106
+ invokeChildWorkflow: {
107
+ <
108
+ TInput extends InputParameters,
109
+ TOutput = unknown
110
+ >(stepId: string, ref: WorkflowRef<TInput, TOutput>, input: InferInputParameters<TInput>, options?: WorkflowRunOptions): Promise<TOutput>;
111
+ <TOutput = unknown>(stepId: string, params: {
112
+ workflowId: string;
113
+ input: unknown;
114
+ resourceId?: string;
115
+ idempotencyKey?: string;
116
+ options?: WorkflowRunOptions;
117
+ }): Promise<TOutput>;
118
+ };
91
119
  };
92
120
  /**
93
121
  * Plugin that extends the workflow step API with extra methods.
@@ -138,9 +166,12 @@ type WorkflowInternalDefinition<TInput extends InputParameters = InputParameters
138
166
  interface WorkflowFactory<TStepExt = object> {
139
167
  <I extends InputParameters = InputParameters>(id: string, handler: (context: WorkflowContext<I, StepBaseContext & TStepExt>) => Promise<unknown>, options?: WorkflowOptions<I>): WorkflowDefinition<I>;
140
168
  use<TNewExt>(plugin: WorkflowPlugin<StepBaseContext & TStepExt, TNewExt>): WorkflowFactory<TStepExt & TNewExt>;
141
- ref<TInput extends InputParameters = InputParameters>(id: string, options?: {
169
+ ref<
170
+ TInput extends InputParameters = InputParameters,
171
+ TOutput = unknown
172
+ >(id: string, options?: {
142
173
  inputSchema?: TInput;
143
- }): WorkflowRef<TInput>;
174
+ }): WorkflowRef<TInput, TOutput>;
144
175
  }
145
176
  /**
146
177
  * Lightweight workflow reference - carries the workflow ID and input type
@@ -149,7 +180,10 @@ interface WorkflowFactory<TStepExt = object> {
149
180
  *
150
181
  * Callable: pass a handler to create a full WorkflowDefinition.
151
182
  */
152
- interface WorkflowRef<TInput extends InputParameters = InputParameters> {
183
+ interface WorkflowRef<
184
+ TInput extends InputParameters = InputParameters,
185
+ TOutput = unknown
186
+ > {
153
187
  (handler: (context: WorkflowContext<TInput, StepBaseContext>) => Promise<unknown>, options?: Omit<WorkflowOptions<TInput>, "inputSchema">): WorkflowDefinition<TInput>;
154
188
  readonly id: string;
155
189
  readonly inputSchema?: TInput;
@@ -187,13 +221,7 @@ type WorkflowClientOptions = {
187
221
  connectionString: string;
188
222
  pool?: never;
189
223
  });
190
- type StartWorkflowOptions = {
191
- resourceId?: string;
192
- timeout?: number;
193
- retries?: number;
194
- expireInSeconds?: number;
195
- idempotencyKey?: string;
196
- };
224
+ type StartWorkflowOptions = WorkflowRunOptions;
197
225
  declare class WorkflowClient {
198
226
  private boss;
199
227
  private db;
@@ -269,19 +297,17 @@ declare class WorkflowClient {
269
297
  * Create a lightweight workflow reference.
270
298
  * Safe to import from `pg-workflows/client` - no engine or handler code.
271
299
  */
272
- declare function createWorkflowRef<TInput extends InputParameters = InputParameters>(id: string, options?: {
300
+ declare function createWorkflowRef<
301
+ TOutput = unknown,
302
+ TInput extends InputParameters = InputParameters
303
+ >(id: string, options?: {
273
304
  inputSchema?: TInput;
274
- }): WorkflowRef<TInput>;
305
+ }): WorkflowRef<TInput, TOutput>;
275
306
  declare const workflow: WorkflowFactory;
276
307
  import pg2 from "pg";
277
308
  import { Db, PgBoss as PgBoss2 } from "pg-boss";
278
- type StartWorkflowOptions2 = {
279
- resourceId?: string;
280
- timeout?: number;
281
- retries?: number;
282
- expireInSeconds?: number;
309
+ type StartWorkflowOptions2 = WorkflowRunOptions & {
283
310
  batchSize?: number;
284
- idempotencyKey?: string;
285
311
  };
286
312
  type WorkflowEngineOptions = {
287
313
  workflows?: WorkflowDefinition[];
@@ -304,13 +330,15 @@ declare class WorkflowEngine {
304
330
  workflows: Map<string, WorkflowInternalDefinition>;
305
331
  private logger;
306
332
  constructor({ workflows, logger, boss,...connectionOptions }: WorkflowEngineOptions);
307
- start(asEngine?: boolean, { batchSize }?: {
333
+ start(asEngine?: boolean, { batchSize, heartbeatSeconds }?: {
308
334
  batchSize?: number;
335
+ heartbeatSeconds?: number;
309
336
  }): Promise<void>;
310
337
  stop(): Promise<void>;
311
338
  registerWorkflow(definition: WorkflowDefinition<InputParameters>): Promise<WorkflowEngine>;
312
339
  unregisterWorkflow(workflowId: string): Promise<WorkflowEngine>;
313
340
  unregisterAllWorkflows(): Promise<WorkflowEngine>;
341
+ private resolveWorkflowRunParameters;
314
342
  startWorkflow<TInput extends InputParameters>(ref: WorkflowRef<TInput>, input: InferInputParameters<TInput>, options?: StartWorkflowOptions2): Promise<WorkflowRun>;
315
343
  startWorkflow(params: {
316
344
  resourceId?: string;
@@ -319,6 +347,9 @@ declare class WorkflowEngine {
319
347
  idempotencyKey?: string;
320
348
  options?: StartWorkflowOptions2;
321
349
  }): Promise<WorkflowRun>;
350
+ private createWorkflowRun;
351
+ private enqueueWorkflowRun;
352
+ private notifyParentOfChildTerminalRun;
322
353
  pauseWorkflow({ runId, resourceId }: {
323
354
  runId: string;
324
355
  resourceId?: string;
@@ -375,8 +406,34 @@ declare class WorkflowEngine {
375
406
  */
376
407
  private resolveScopedResourceId;
377
408
  private handleWorkflowRun;
409
+ /**
410
+ * Reconciles workflow runs whose retries pg-boss has exhausted (handler
411
+ * threw on the final attempt, or worker died and missed the heartbeat
412
+ * past the retry budget). The DLQ entry tells us the run is unrecoverable;
413
+ * we mark it FAILED with whatever error message the catch block last
414
+ * persisted, falling back to a worker-death message.
415
+ */
416
+ private handleWorkflowRunDlq;
378
417
  private getCachedStepEntry;
379
418
  private getWaitForStepEntry;
419
+ private getInvokeChildWorkflowStepEntry;
420
+ /**
421
+ * Returns the cached output for a COMPLETED child run. Treats `undefined`
422
+ * outputs as `{}` so the parent timeline always has a defined value.
423
+ * Caller must ensure `childRun.status === COMPLETED` before calling.
424
+ */
425
+ private getCompletedChildOutput;
426
+ /**
427
+ * Throws a `WorkflowEngineError` describing why an invoked child run did not
428
+ * produce output (it FAILED or was CANCELLED). The throw aborts the parent
429
+ * step, which is then caught by `handleWorkflowRun` and marks the parent
430
+ * FAILED with the same message — no fake sentinel value is ever written to
431
+ * the parent timeline.
432
+ */
433
+ private throwForNonCompletedChild;
434
+ private assertInvokeChildWorkflowStepOwnership;
435
+ private invokeChildWorkflowStep;
436
+ private pauseRunForWait;
380
437
  private runStep;
381
438
  private waitStep;
382
439
  private pollStep;
@@ -398,6 +455,8 @@ declare class WorkflowEngine {
398
455
  }>;
399
456
  }
400
457
  import { StandardSchemaV1 as StandardSchemaV12 } from "@standard-schema/spec";
458
+ declare function validateWorkflowId(workflowId: string): void;
459
+ declare function validateResourceId(resourceId: string | undefined | null): void;
401
460
  declare class WorkflowEngineError extends Error {
402
461
  readonly workflowId?: string | undefined;
403
462
  readonly runId?: string | undefined;
@@ -408,4 +467,4 @@ declare class WorkflowEngineError extends Error {
408
467
  declare class WorkflowRunNotFoundError extends WorkflowEngineError {
409
468
  constructor(runId?: string, workflowId?: string);
410
469
  }
411
- export { workflow, parseDuration, createWorkflowRef, WorkflowStatus, WorkflowRunProgress, WorkflowRunNotFoundError, WorkflowRef, WorkflowPlugin, WorkflowOptions, WorkflowLogger, WorkflowInternalLoggerContext, WorkflowInternalLogger, WorkflowInternalDefinition, WorkflowFactory, WorkflowEngineOptions, WorkflowEngineError, WorkflowEngine, WorkflowDefinition, WorkflowContext, WorkflowClientOptions, WorkflowClient, StepType, StepInternalDefinition, StepBaseContext, StartWorkflowOptions, InputParameters, InferInputParameters, DurationObject, Duration };
470
+ export { workflow, validateWorkflowId, validateResourceId, parseDuration, createWorkflowRef, WorkflowStatus, WorkflowRunProgress, WorkflowRunOptions, WorkflowRunNotFoundError, WorkflowRef, WorkflowPlugin, WorkflowOptions, WorkflowLogger, WorkflowInternalLoggerContext, WorkflowInternalLogger, WorkflowInternalDefinition, WorkflowFactory, WorkflowEngineOptions, WorkflowEngineError, WorkflowEngine, WorkflowDefinition, WorkflowContext, WorkflowClientOptions, WorkflowClient, StepType, StepInternalDefinition, StepBaseContext, StartWorkflowOptions, InputParameters, InferInputParameters, DurationObject, Duration };