pg-workflows 0.13.1 → 0.13.2

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.js CHANGED
@@ -1,6 +1,7 @@
1
1
  import {
2
2
  DEFAULT_PGBOSS_SCHEMA,
3
3
  PAUSE_EVENT_NAME,
4
+ STEP_BASE_METHOD_TYPES,
4
5
  WORKFLOW_RUN_DLQ_QUEUE_NAME,
5
6
  WORKFLOW_RUN_QUEUE_NAME,
6
7
  WorkflowClient,
@@ -22,7 +23,7 @@ import {
22
23
  waitForTimelineKey,
23
24
  withPostgresTransaction,
24
25
  workflow
25
- } from "./shared/chunk-5xswmve7.js";
26
+ } from "./shared/chunk-hjycwrsq.js";
26
27
  // src/engine.ts
27
28
  import { merge } from "es-toolkit";
28
29
  import pg from "pg";
@@ -73,11 +74,11 @@ function parseWorkflowHandler(handler) {
73
74
  const propertyAccess = node.expression;
74
75
  const objectName = propertyAccess.expression.getText(sourceFile);
75
76
  const methodName = propertyAccess.name.text;
76
- if (objectName === "step" && (methodName === "run" || methodName === "waitFor" || methodName === "pause" || methodName === "waitUntil" || methodName === "delay" || methodName === "sleep" || methodName === "poll" || methodName === "invokeChildWorkflow")) {
77
+ const stepType = objectName === "step" ? STEP_BASE_METHOD_TYPES.get(methodName) : undefined;
78
+ if (stepType !== undefined) {
77
79
  const firstArg = node.arguments[0];
78
80
  if (firstArg) {
79
81
  const { id, isDynamic } = extractStepId(firstArg);
80
- const stepType = methodName === "sleep" ? "delay" /* DELAY */ : methodName;
81
82
  const stepDefinition = {
82
83
  id,
83
84
  type: stepType,
@@ -381,6 +382,9 @@ class WorkflowEngine {
381
382
  });
382
383
  return run;
383
384
  }
385
+ async createChildWorkflowRun(params) {
386
+ return this.createWorkflowRun(params);
387
+ }
384
388
  async createWorkflowRun({
385
389
  workflowId,
386
390
  input,
@@ -767,75 +771,7 @@ class WorkflowEngine {
767
771
  }, { db });
768
772
  }, this.pool);
769
773
  }
770
- const baseStep = {
771
- run: async (stepId, handler) => {
772
- if (!run) {
773
- throw new WorkflowEngineError("Missing workflow run", workflowId, runId);
774
- }
775
- return this.runStep({ stepId, run, handler });
776
- },
777
- waitFor: async (stepId, { eventName, timeout }) => {
778
- if (!run) {
779
- throw new WorkflowEngineError("Missing workflow run", workflowId, runId);
780
- }
781
- const timeoutDate = timeout ? new Date(Date.now() + timeout) : undefined;
782
- return this.waitStep({ run, stepId, eventName, timeoutDate });
783
- },
784
- waitUntil: async (stepId, dateOrOptions) => {
785
- if (!run) {
786
- throw new WorkflowEngineError("Missing workflow run", workflowId, runId);
787
- }
788
- const date = dateOrOptions instanceof Date ? dateOrOptions : typeof dateOrOptions === "string" ? new Date(dateOrOptions) : dateOrOptions.date instanceof Date ? dateOrOptions.date : new Date(dateOrOptions.date);
789
- await this.waitStep({ run, stepId, timeoutDate: date });
790
- },
791
- pause: async (stepId) => {
792
- if (!run) {
793
- throw new WorkflowEngineError("Missing workflow run", workflowId, runId);
794
- }
795
- await this.waitStep({ run, stepId, eventName: PAUSE_EVENT_NAME });
796
- },
797
- delay: async (stepId, duration) => {
798
- if (!run) {
799
- throw new WorkflowEngineError("Missing workflow run", workflowId, runId);
800
- }
801
- await this.waitStep({
802
- run,
803
- stepId,
804
- timeoutDate: new Date(Date.now() + parseDuration(duration))
805
- });
806
- },
807
- get sleep() {
808
- return this.delay;
809
- },
810
- poll: async (stepId, conditionFn, options) => {
811
- if (!run) {
812
- throw new WorkflowEngineError("Missing workflow run", workflowId, runId);
813
- }
814
- const intervalMs = parseDuration(options?.interval ?? "30s");
815
- if (intervalMs < 30000) {
816
- throw new WorkflowEngineError(`step.poll interval must be at least 30s (got ${intervalMs}ms)`, workflowId, runId);
817
- }
818
- const timeoutMs = options?.timeout ? parseDuration(options.timeout) : undefined;
819
- return this.pollStep({ run, stepId, conditionFn, intervalMs, timeoutMs });
820
- },
821
- invokeChildWorkflow: async (stepId, refOrParams, inputArg, optionsArg) => {
822
- if (!run) {
823
- throw new WorkflowEngineError("Missing workflow run", workflowId, runId);
824
- }
825
- const resolvedChildCall = this.resolveWorkflowRunParameters(refOrParams, inputArg, optionsArg);
826
- const childWorkflowInvocation = {
827
- run,
828
- stepId,
829
- workflowId: resolvedChildCall.workflowId,
830
- input: resolvedChildCall.input,
831
- options: resolvedChildCall.options,
832
- resourceId: resolvedChildCall.resourceId,
833
- idempotencyKey: resolvedChildCall.idempotencyKey
834
- };
835
- return this.invokeChildWorkflowStep(childWorkflowInvocation);
836
- }
837
- };
838
- let step = { ...baseStep };
774
+ let step = this.createBaseStep(run, workflowId, runId);
839
775
  const plugins = workflow2.plugins ?? [];
840
776
  const context = {
841
777
  input: run.input,
@@ -925,6 +861,55 @@ class WorkflowEngine {
925
861
  workflowId: run.workflowId
926
862
  });
927
863
  }
864
+ createBaseStep(run, workflowId, runId) {
865
+ return {
866
+ run: async (stepId, handler) => {
867
+ return this.runStep({ stepId, run, handler });
868
+ },
869
+ waitFor: async (stepId, { eventName, timeout }) => {
870
+ const timeoutDate = timeout ? new Date(Date.now() + timeout) : undefined;
871
+ return this.waitStep({ run, stepId, eventName, timeoutDate });
872
+ },
873
+ waitUntil: async (stepId, dateOrOptions) => {
874
+ const date = dateOrOptions instanceof Date ? dateOrOptions : typeof dateOrOptions === "string" ? new Date(dateOrOptions) : dateOrOptions.date instanceof Date ? dateOrOptions.date : new Date(dateOrOptions.date);
875
+ await this.waitStep({ run, stepId, timeoutDate: date });
876
+ },
877
+ pause: async (stepId) => {
878
+ await this.waitStep({ run, stepId, eventName: PAUSE_EVENT_NAME });
879
+ },
880
+ delay: async (stepId, duration) => {
881
+ await this.waitStep({
882
+ run,
883
+ stepId,
884
+ timeoutDate: new Date(Date.now() + parseDuration(duration))
885
+ });
886
+ },
887
+ get sleep() {
888
+ return this.delay;
889
+ },
890
+ poll: async (stepId, conditionFn, options) => {
891
+ const intervalMs = parseDuration(options?.interval ?? "30s");
892
+ if (intervalMs < 30000) {
893
+ throw new WorkflowEngineError(`step.poll interval must be at least 30s (got ${intervalMs}ms)`, workflowId, runId);
894
+ }
895
+ const timeoutMs = options?.timeout ? parseDuration(options.timeout) : undefined;
896
+ return this.pollStep({ run, stepId, conditionFn, intervalMs, timeoutMs });
897
+ },
898
+ invokeChildWorkflow: async (stepId, refOrParams, inputArg, optionsArg) => {
899
+ const resolvedChildCall = this.resolveWorkflowRunParameters(refOrParams, inputArg, optionsArg);
900
+ const childWorkflowInvocation = {
901
+ run,
902
+ stepId,
903
+ workflowId: resolvedChildCall.workflowId,
904
+ input: resolvedChildCall.input,
905
+ options: resolvedChildCall.options,
906
+ resourceId: resolvedChildCall.resourceId,
907
+ idempotencyKey: resolvedChildCall.idempotencyKey
908
+ };
909
+ return this.invokeChildWorkflowStep(childWorkflowInvocation);
910
+ }
911
+ };
912
+ }
928
913
  getCachedStepEntry(timeline, stepId) {
929
914
  const stepEntry = timeline[stepId];
930
915
  return stepEntry && typeof stepEntry === "object" && "output" in stepEntry ? stepEntry : null;
@@ -1015,7 +1000,7 @@ class WorkflowEngine {
1015
1000
  });
1016
1001
  return;
1017
1002
  }
1018
- const result = await this.createWorkflowRun({
1003
+ const result = await this.createChildWorkflowRun({
1019
1004
  workflowId,
1020
1005
  input,
1021
1006
  resourceId: childResourceId,
@@ -1581,5 +1566,5 @@ export {
1581
1566
  WorkflowClient
1582
1567
  };
1583
1568
 
1584
- //# debugId=C2CB4B181900270764756E2164756E21
1569
+ //# debugId=EE11F9509955D59264756E2164756E21
1585
1570
  //# sourceMappingURL=index.js.map