pg-workflows 0.9.0 → 0.11.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 = {
@@ -30,7 +33,6 @@ type DurationObject = {
30
33
  seconds?: number;
31
34
  };
32
35
  type Duration = string | DurationObject;
33
- declare function parseDuration(duration: Duration): number;
34
36
  declare enum WorkflowStatus {
35
37
  PENDING = "pending",
36
38
  RUNNING = "running",
@@ -45,10 +47,18 @@ declare enum StepType {
45
47
  WAIT_FOR = "waitFor",
46
48
  WAIT_UNTIL = "waitUntil",
47
49
  DELAY = "delay",
48
- POLL = "poll"
50
+ POLL = "poll",
51
+ INVOKE_CHILD_WORKFLOW = "invokeChildWorkflow"
49
52
  }
50
53
  type InputParameters = StandardSchemaV1;
51
54
  type InferInputParameters<P extends InputParameters> = StandardSchemaV1.InferOutput<P>;
55
+ type StartWorkflowOptions = {
56
+ resourceId?: string;
57
+ timeout?: number;
58
+ retries?: number;
59
+ expireInSeconds?: number;
60
+ idempotencyKey?: string;
61
+ };
52
62
  type WorkflowOptions<I extends InputParameters> = {
53
63
  timeout?: number;
54
64
  retries?: number;
@@ -88,6 +98,23 @@ type StepBaseContext = {
88
98
  } | {
89
99
  timedOut: true;
90
100
  }>;
101
+ /**
102
+ * Invoke a child workflow from inside the current workflow and pause until
103
+ * the child run reaches a terminal state.
104
+ */
105
+ invokeChildWorkflow: {
106
+ <
107
+ TInput extends InputParameters,
108
+ TOutput = unknown
109
+ >(stepId: string, ref: WorkflowRef<TInput, TOutput>, input: InferInputParameters<TInput>, options?: StartWorkflowOptions): Promise<TOutput>;
110
+ <TOutput = unknown>(stepId: string, params: {
111
+ workflowId: string;
112
+ input: unknown;
113
+ resourceId?: string;
114
+ idempotencyKey?: string;
115
+ options?: StartWorkflowOptions;
116
+ }): Promise<TOutput>;
117
+ };
91
118
  };
92
119
  /**
93
120
  * Plugin that extends the workflow step API with extra methods.
@@ -138,9 +165,12 @@ type WorkflowInternalDefinition<TInput extends InputParameters = InputParameters
138
165
  interface WorkflowFactory<TStepExt = object> {
139
166
  <I extends InputParameters = InputParameters>(id: string, handler: (context: WorkflowContext<I, StepBaseContext & TStepExt>) => Promise<unknown>, options?: WorkflowOptions<I>): WorkflowDefinition<I>;
140
167
  use<TNewExt>(plugin: WorkflowPlugin<StepBaseContext & TStepExt, TNewExt>): WorkflowFactory<TStepExt & TNewExt>;
141
- ref<TInput extends InputParameters = InputParameters>(id: string, options?: {
168
+ ref<
169
+ TInput extends InputParameters = InputParameters,
170
+ TOutput = unknown
171
+ >(id: string, options?: {
142
172
  inputSchema?: TInput;
143
- }): WorkflowRef<TInput>;
173
+ }): WorkflowRef<TInput, TOutput>;
144
174
  }
145
175
  /**
146
176
  * Lightweight workflow reference - carries the workflow ID and input type
@@ -149,7 +179,10 @@ interface WorkflowFactory<TStepExt = object> {
149
179
  *
150
180
  * Callable: pass a handler to create a full WorkflowDefinition.
151
181
  */
152
- interface WorkflowRef<TInput extends InputParameters = InputParameters> {
182
+ interface WorkflowRef<
183
+ TInput extends InputParameters = InputParameters,
184
+ TOutput = unknown
185
+ > {
153
186
  (handler: (context: WorkflowContext<TInput, StepBaseContext>) => Promise<unknown>, options?: Omit<WorkflowOptions<TInput>, "inputSchema">): WorkflowDefinition<TInput>;
154
187
  readonly id: string;
155
188
  readonly inputSchema?: TInput;
@@ -163,14 +196,6 @@ interface WorkflowLogger {
163
196
  log(message: string): void;
164
197
  error(message: string, ...args: unknown[]): void;
165
198
  }
166
- type WorkflowInternalLoggerContext = {
167
- runId?: string;
168
- workflowId?: string;
169
- };
170
- interface WorkflowInternalLogger {
171
- log(message: string, context?: WorkflowInternalLoggerContext): void;
172
- error(message: string, error: Error, context?: WorkflowInternalLoggerContext): void;
173
- }
174
199
  type WorkflowClientOptions = {
175
200
  logger?: WorkflowLogger;
176
201
  /**
@@ -187,13 +212,6 @@ type WorkflowClientOptions = {
187
212
  connectionString: string;
188
213
  pool?: never;
189
214
  });
190
- type StartWorkflowOptions = {
191
- resourceId?: string;
192
- timeout?: number;
193
- retries?: number;
194
- expireInSeconds?: number;
195
- idempotencyKey?: string;
196
- };
197
215
  declare class WorkflowClient {
198
216
  private boss;
199
217
  private db;
@@ -269,20 +287,15 @@ declare class WorkflowClient {
269
287
  * Create a lightweight workflow reference.
270
288
  * Safe to import from `pg-workflows/client` - no engine or handler code.
271
289
  */
272
- declare function createWorkflowRef<TInput extends InputParameters = InputParameters>(id: string, options?: {
290
+ declare function createWorkflowRef<
291
+ TOutput = unknown,
292
+ TInput extends InputParameters = InputParameters
293
+ >(id: string, options?: {
273
294
  inputSchema?: TInput;
274
- }): WorkflowRef<TInput>;
295
+ }): WorkflowRef<TInput, TOutput>;
275
296
  declare const workflow: WorkflowFactory;
276
297
  import pg2 from "pg";
277
298
  import { Db, PgBoss as PgBoss2 } from "pg-boss";
278
- type StartWorkflowOptions2 = {
279
- resourceId?: string;
280
- timeout?: number;
281
- retries?: number;
282
- expireInSeconds?: number;
283
- batchSize?: number;
284
- idempotencyKey?: string;
285
- };
286
299
  type WorkflowEngineOptions = {
287
300
  workflows?: WorkflowDefinition[];
288
301
  logger?: WorkflowLogger;
@@ -312,14 +325,18 @@ declare class WorkflowEngine {
312
325
  registerWorkflow(definition: WorkflowDefinition<InputParameters>): Promise<WorkflowEngine>;
313
326
  unregisterWorkflow(workflowId: string): Promise<WorkflowEngine>;
314
327
  unregisterAllWorkflows(): Promise<WorkflowEngine>;
315
- startWorkflow<TInput extends InputParameters>(ref: WorkflowRef<TInput>, input: InferInputParameters<TInput>, options?: StartWorkflowOptions2): Promise<WorkflowRun>;
328
+ private resolveWorkflowRunParameters;
329
+ startWorkflow<TInput extends InputParameters>(ref: WorkflowRef<TInput>, input: InferInputParameters<TInput>, options?: StartWorkflowOptions): Promise<WorkflowRun>;
316
330
  startWorkflow(params: {
317
331
  resourceId?: string;
318
332
  workflowId: string;
319
333
  input: unknown;
320
334
  idempotencyKey?: string;
321
- options?: StartWorkflowOptions2;
335
+ options?: StartWorkflowOptions;
322
336
  }): Promise<WorkflowRun>;
337
+ private createWorkflowRun;
338
+ private enqueueWorkflowRun;
339
+ private notifyParentOfChildTerminalRun;
323
340
  pauseWorkflow({ runId, resourceId }: {
324
341
  runId: string;
325
342
  resourceId?: string;
@@ -386,6 +403,24 @@ declare class WorkflowEngine {
386
403
  private handleWorkflowRunDlq;
387
404
  private getCachedStepEntry;
388
405
  private getWaitForStepEntry;
406
+ private getInvokeChildWorkflowStepEntry;
407
+ /**
408
+ * Returns the cached output for a COMPLETED child run. Treats `undefined`
409
+ * outputs as `{}` so the parent timeline always has a defined value.
410
+ * Caller must ensure `childRun.status === COMPLETED` before calling.
411
+ */
412
+ private getCompletedChildOutput;
413
+ /**
414
+ * Throws a `WorkflowEngineError` describing why an invoked child run did not
415
+ * produce output (it FAILED or was CANCELLED). The throw aborts the parent
416
+ * step, which is then caught by `handleWorkflowRun` and marks the parent
417
+ * FAILED with the same message — no fake sentinel value is ever written to
418
+ * the parent timeline.
419
+ */
420
+ private throwForNonCompletedChild;
421
+ private assertInvokeChildWorkflowStepOwnership;
422
+ private invokeChildWorkflowStep;
423
+ private pauseRunForWait;
389
424
  private runStep;
390
425
  private waitStep;
391
426
  private pollStep;
@@ -407,8 +442,6 @@ declare class WorkflowEngine {
407
442
  }>;
408
443
  }
409
444
  import { StandardSchemaV1 as StandardSchemaV12 } from "@standard-schema/spec";
410
- declare function validateWorkflowId(workflowId: string): void;
411
- declare function validateResourceId(resourceId: string | undefined | null): void;
412
445
  declare class WorkflowEngineError extends Error {
413
446
  readonly workflowId?: string | undefined;
414
447
  readonly runId?: string | undefined;
@@ -419,4 +452,4 @@ declare class WorkflowEngineError extends Error {
419
452
  declare class WorkflowRunNotFoundError extends WorkflowEngineError {
420
453
  constructor(runId?: string, workflowId?: string);
421
454
  }
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 };
455
+ export { workflow, createWorkflowRef, WorkflowStatus, WorkflowRunProgress, WorkflowRunNotFoundError, WorkflowRun, WorkflowRef, WorkflowPlugin, WorkflowOptions, WorkflowLogger, WorkflowEngineOptions, WorkflowEngineError, WorkflowEngine, WorkflowDefinition, WorkflowContext, WorkflowClientOptions, WorkflowClient, StepBaseContext, StartWorkflowOptions, InputParameters, InferInputParameters, 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 = {
@@ -30,7 +33,6 @@ type DurationObject = {
30
33
  seconds?: number;
31
34
  };
32
35
  type Duration = string | DurationObject;
33
- declare function parseDuration(duration: Duration): number;
34
36
  declare enum WorkflowStatus {
35
37
  PENDING = "pending",
36
38
  RUNNING = "running",
@@ -45,10 +47,18 @@ declare enum StepType {
45
47
  WAIT_FOR = "waitFor",
46
48
  WAIT_UNTIL = "waitUntil",
47
49
  DELAY = "delay",
48
- POLL = "poll"
50
+ POLL = "poll",
51
+ INVOKE_CHILD_WORKFLOW = "invokeChildWorkflow"
49
52
  }
50
53
  type InputParameters = StandardSchemaV1;
51
54
  type InferInputParameters<P extends InputParameters> = StandardSchemaV1.InferOutput<P>;
55
+ type StartWorkflowOptions = {
56
+ resourceId?: string;
57
+ timeout?: number;
58
+ retries?: number;
59
+ expireInSeconds?: number;
60
+ idempotencyKey?: string;
61
+ };
52
62
  type WorkflowOptions<I extends InputParameters> = {
53
63
  timeout?: number;
54
64
  retries?: number;
@@ -88,6 +98,23 @@ type StepBaseContext = {
88
98
  } | {
89
99
  timedOut: true;
90
100
  }>;
101
+ /**
102
+ * Invoke a child workflow from inside the current workflow and pause until
103
+ * the child run reaches a terminal state.
104
+ */
105
+ invokeChildWorkflow: {
106
+ <
107
+ TInput extends InputParameters,
108
+ TOutput = unknown
109
+ >(stepId: string, ref: WorkflowRef<TInput, TOutput>, input: InferInputParameters<TInput>, options?: StartWorkflowOptions): Promise<TOutput>;
110
+ <TOutput = unknown>(stepId: string, params: {
111
+ workflowId: string;
112
+ input: unknown;
113
+ resourceId?: string;
114
+ idempotencyKey?: string;
115
+ options?: StartWorkflowOptions;
116
+ }): Promise<TOutput>;
117
+ };
91
118
  };
92
119
  /**
93
120
  * Plugin that extends the workflow step API with extra methods.
@@ -138,9 +165,12 @@ type WorkflowInternalDefinition<TInput extends InputParameters = InputParameters
138
165
  interface WorkflowFactory<TStepExt = object> {
139
166
  <I extends InputParameters = InputParameters>(id: string, handler: (context: WorkflowContext<I, StepBaseContext & TStepExt>) => Promise<unknown>, options?: WorkflowOptions<I>): WorkflowDefinition<I>;
140
167
  use<TNewExt>(plugin: WorkflowPlugin<StepBaseContext & TStepExt, TNewExt>): WorkflowFactory<TStepExt & TNewExt>;
141
- ref<TInput extends InputParameters = InputParameters>(id: string, options?: {
168
+ ref<
169
+ TInput extends InputParameters = InputParameters,
170
+ TOutput = unknown
171
+ >(id: string, options?: {
142
172
  inputSchema?: TInput;
143
- }): WorkflowRef<TInput>;
173
+ }): WorkflowRef<TInput, TOutput>;
144
174
  }
145
175
  /**
146
176
  * Lightweight workflow reference - carries the workflow ID and input type
@@ -149,7 +179,10 @@ interface WorkflowFactory<TStepExt = object> {
149
179
  *
150
180
  * Callable: pass a handler to create a full WorkflowDefinition.
151
181
  */
152
- interface WorkflowRef<TInput extends InputParameters = InputParameters> {
182
+ interface WorkflowRef<
183
+ TInput extends InputParameters = InputParameters,
184
+ TOutput = unknown
185
+ > {
153
186
  (handler: (context: WorkflowContext<TInput, StepBaseContext>) => Promise<unknown>, options?: Omit<WorkflowOptions<TInput>, "inputSchema">): WorkflowDefinition<TInput>;
154
187
  readonly id: string;
155
188
  readonly inputSchema?: TInput;
@@ -163,14 +196,6 @@ interface WorkflowLogger {
163
196
  log(message: string): void;
164
197
  error(message: string, ...args: unknown[]): void;
165
198
  }
166
- type WorkflowInternalLoggerContext = {
167
- runId?: string;
168
- workflowId?: string;
169
- };
170
- interface WorkflowInternalLogger {
171
- log(message: string, context?: WorkflowInternalLoggerContext): void;
172
- error(message: string, error: Error, context?: WorkflowInternalLoggerContext): void;
173
- }
174
199
  type WorkflowClientOptions = {
175
200
  logger?: WorkflowLogger;
176
201
  /**
@@ -187,13 +212,6 @@ type WorkflowClientOptions = {
187
212
  connectionString: string;
188
213
  pool?: never;
189
214
  });
190
- type StartWorkflowOptions = {
191
- resourceId?: string;
192
- timeout?: number;
193
- retries?: number;
194
- expireInSeconds?: number;
195
- idempotencyKey?: string;
196
- };
197
215
  declare class WorkflowClient {
198
216
  private boss;
199
217
  private db;
@@ -269,20 +287,15 @@ declare class WorkflowClient {
269
287
  * Create a lightweight workflow reference.
270
288
  * Safe to import from `pg-workflows/client` - no engine or handler code.
271
289
  */
272
- declare function createWorkflowRef<TInput extends InputParameters = InputParameters>(id: string, options?: {
290
+ declare function createWorkflowRef<
291
+ TOutput = unknown,
292
+ TInput extends InputParameters = InputParameters
293
+ >(id: string, options?: {
273
294
  inputSchema?: TInput;
274
- }): WorkflowRef<TInput>;
295
+ }): WorkflowRef<TInput, TOutput>;
275
296
  declare const workflow: WorkflowFactory;
276
297
  import pg2 from "pg";
277
298
  import { Db, PgBoss as PgBoss2 } from "pg-boss";
278
- type StartWorkflowOptions2 = {
279
- resourceId?: string;
280
- timeout?: number;
281
- retries?: number;
282
- expireInSeconds?: number;
283
- batchSize?: number;
284
- idempotencyKey?: string;
285
- };
286
299
  type WorkflowEngineOptions = {
287
300
  workflows?: WorkflowDefinition[];
288
301
  logger?: WorkflowLogger;
@@ -312,14 +325,18 @@ declare class WorkflowEngine {
312
325
  registerWorkflow(definition: WorkflowDefinition<InputParameters>): Promise<WorkflowEngine>;
313
326
  unregisterWorkflow(workflowId: string): Promise<WorkflowEngine>;
314
327
  unregisterAllWorkflows(): Promise<WorkflowEngine>;
315
- startWorkflow<TInput extends InputParameters>(ref: WorkflowRef<TInput>, input: InferInputParameters<TInput>, options?: StartWorkflowOptions2): Promise<WorkflowRun>;
328
+ private resolveWorkflowRunParameters;
329
+ startWorkflow<TInput extends InputParameters>(ref: WorkflowRef<TInput>, input: InferInputParameters<TInput>, options?: StartWorkflowOptions): Promise<WorkflowRun>;
316
330
  startWorkflow(params: {
317
331
  resourceId?: string;
318
332
  workflowId: string;
319
333
  input: unknown;
320
334
  idempotencyKey?: string;
321
- options?: StartWorkflowOptions2;
335
+ options?: StartWorkflowOptions;
322
336
  }): Promise<WorkflowRun>;
337
+ private createWorkflowRun;
338
+ private enqueueWorkflowRun;
339
+ private notifyParentOfChildTerminalRun;
323
340
  pauseWorkflow({ runId, resourceId }: {
324
341
  runId: string;
325
342
  resourceId?: string;
@@ -386,6 +403,24 @@ declare class WorkflowEngine {
386
403
  private handleWorkflowRunDlq;
387
404
  private getCachedStepEntry;
388
405
  private getWaitForStepEntry;
406
+ private getInvokeChildWorkflowStepEntry;
407
+ /**
408
+ * Returns the cached output for a COMPLETED child run. Treats `undefined`
409
+ * outputs as `{}` so the parent timeline always has a defined value.
410
+ * Caller must ensure `childRun.status === COMPLETED` before calling.
411
+ */
412
+ private getCompletedChildOutput;
413
+ /**
414
+ * Throws a `WorkflowEngineError` describing why an invoked child run did not
415
+ * produce output (it FAILED or was CANCELLED). The throw aborts the parent
416
+ * step, which is then caught by `handleWorkflowRun` and marks the parent
417
+ * FAILED with the same message — no fake sentinel value is ever written to
418
+ * the parent timeline.
419
+ */
420
+ private throwForNonCompletedChild;
421
+ private assertInvokeChildWorkflowStepOwnership;
422
+ private invokeChildWorkflowStep;
423
+ private pauseRunForWait;
389
424
  private runStep;
390
425
  private waitStep;
391
426
  private pollStep;
@@ -407,8 +442,6 @@ declare class WorkflowEngine {
407
442
  }>;
408
443
  }
409
444
  import { StandardSchemaV1 as StandardSchemaV12 } from "@standard-schema/spec";
410
- declare function validateWorkflowId(workflowId: string): void;
411
- declare function validateResourceId(resourceId: string | undefined | null): void;
412
445
  declare class WorkflowEngineError extends Error {
413
446
  readonly workflowId?: string | undefined;
414
447
  readonly runId?: string | undefined;
@@ -419,4 +452,4 @@ declare class WorkflowEngineError extends Error {
419
452
  declare class WorkflowRunNotFoundError extends WorkflowEngineError {
420
453
  constructor(runId?: string, workflowId?: string);
421
454
  }
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 };
455
+ export { workflow, createWorkflowRef, WorkflowStatus, WorkflowRunProgress, WorkflowRunNotFoundError, WorkflowRun, WorkflowRef, WorkflowPlugin, WorkflowOptions, WorkflowLogger, WorkflowEngineOptions, WorkflowEngineError, WorkflowEngine, WorkflowDefinition, WorkflowContext, WorkflowClientOptions, WorkflowClient, StepBaseContext, StartWorkflowOptions, InputParameters, InferInputParameters, Duration };