pg-workflows 0.9.0 → 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[];
@@ -312,6 +338,7 @@ declare class WorkflowEngine {
312
338
  registerWorkflow(definition: WorkflowDefinition<InputParameters>): Promise<WorkflowEngine>;
313
339
  unregisterWorkflow(workflowId: string): Promise<WorkflowEngine>;
314
340
  unregisterAllWorkflows(): Promise<WorkflowEngine>;
341
+ private resolveWorkflowRunParameters;
315
342
  startWorkflow<TInput extends InputParameters>(ref: WorkflowRef<TInput>, input: InferInputParameters<TInput>, options?: StartWorkflowOptions2): Promise<WorkflowRun>;
316
343
  startWorkflow(params: {
317
344
  resourceId?: string;
@@ -320,6 +347,9 @@ declare class WorkflowEngine {
320
347
  idempotencyKey?: string;
321
348
  options?: StartWorkflowOptions2;
322
349
  }): Promise<WorkflowRun>;
350
+ private createWorkflowRun;
351
+ private enqueueWorkflowRun;
352
+ private notifyParentOfChildTerminalRun;
323
353
  pauseWorkflow({ runId, resourceId }: {
324
354
  runId: string;
325
355
  resourceId?: string;
@@ -386,6 +416,24 @@ declare class WorkflowEngine {
386
416
  private handleWorkflowRunDlq;
387
417
  private getCachedStepEntry;
388
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;
389
437
  private runStep;
390
438
  private waitStep;
391
439
  private pollStep;
@@ -419,4 +467,4 @@ declare class WorkflowEngineError extends Error {
419
467
  declare class WorkflowRunNotFoundError extends WorkflowEngineError {
420
468
  constructor(runId?: string, workflowId?: string);
421
469
  }
422
- export { workflow, validateWorkflowId, validateResourceId, 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[];
@@ -312,6 +338,7 @@ declare class WorkflowEngine {
312
338
  registerWorkflow(definition: WorkflowDefinition<InputParameters>): Promise<WorkflowEngine>;
313
339
  unregisterWorkflow(workflowId: string): Promise<WorkflowEngine>;
314
340
  unregisterAllWorkflows(): Promise<WorkflowEngine>;
341
+ private resolveWorkflowRunParameters;
315
342
  startWorkflow<TInput extends InputParameters>(ref: WorkflowRef<TInput>, input: InferInputParameters<TInput>, options?: StartWorkflowOptions2): Promise<WorkflowRun>;
316
343
  startWorkflow(params: {
317
344
  resourceId?: string;
@@ -320,6 +347,9 @@ declare class WorkflowEngine {
320
347
  idempotencyKey?: string;
321
348
  options?: StartWorkflowOptions2;
322
349
  }): Promise<WorkflowRun>;
350
+ private createWorkflowRun;
351
+ private enqueueWorkflowRun;
352
+ private notifyParentOfChildTerminalRun;
323
353
  pauseWorkflow({ runId, resourceId }: {
324
354
  runId: string;
325
355
  resourceId?: string;
@@ -386,6 +416,24 @@ declare class WorkflowEngine {
386
416
  private handleWorkflowRunDlq;
387
417
  private getCachedStepEntry;
388
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;
389
437
  private runStep;
390
438
  private waitStep;
391
439
  private pollStep;
@@ -419,4 +467,4 @@ declare class WorkflowEngineError extends Error {
419
467
  declare class WorkflowRunNotFoundError extends WorkflowEngineError {
420
468
  constructor(runId?: string, workflowId?: string);
421
469
  }
422
- export { workflow, validateWorkflowId, validateResourceId, 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 };