pg-workflows 0.7.1 → 0.8.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
@@ -1,4 +1,4 @@
1
- import { StandardSchemaV1 } from "@standard-schema/spec";
1
+ import pg from "pg";
2
2
  type WorkflowRun = {
3
3
  id: string;
4
4
  createdAt: Date;
@@ -20,6 +20,7 @@ type WorkflowRun = {
20
20
  jobId: string | null;
21
21
  idempotencyKey: string | null;
22
22
  };
23
+ import { StandardSchemaV1 } from "@standard-schema/spec";
23
24
  type DurationObject = {
24
25
  weeks?: number;
25
26
  days?: number;
@@ -136,6 +137,21 @@ type WorkflowInternalDefinition<TInput extends InputParameters = InputParameters
136
137
  interface WorkflowFactory<TStepExt = object> {
137
138
  <I extends InputParameters = InputParameters>(id: string, handler: (context: WorkflowContext<I, StepBaseContext & TStepExt>) => Promise<unknown>, options?: WorkflowOptions<I>): WorkflowDefinition<I>;
138
139
  use<TNewExt>(plugin: WorkflowPlugin<StepBaseContext & TStepExt, TNewExt>): WorkflowFactory<TStepExt & TNewExt>;
140
+ ref<TInput extends InputParameters = InputParameters>(id: string, options?: {
141
+ inputSchema?: TInput;
142
+ }): WorkflowRef<TInput>;
143
+ }
144
+ /**
145
+ * Lightweight workflow reference - carries the workflow ID and input type
146
+ * but no handler code. Safe to import in API services without pulling in
147
+ * heavy worker dependencies.
148
+ *
149
+ * Callable: pass a handler to create a full WorkflowDefinition.
150
+ */
151
+ interface WorkflowRef<TInput extends InputParameters = InputParameters> {
152
+ (handler: (context: WorkflowContext<TInput, StepBaseContext>) => Promise<unknown>, options?: Omit<WorkflowOptions<TInput>, "inputSchema">): WorkflowDefinition<TInput>;
153
+ readonly id: string;
154
+ readonly inputSchema?: TInput;
139
155
  }
140
156
  type WorkflowRunProgress = WorkflowRun & {
141
157
  completionPercentage: number;
@@ -154,15 +170,115 @@ interface WorkflowInternalLogger {
154
170
  log(message: string, context?: WorkflowInternalLoggerContext): void;
155
171
  error(message: string, error: Error, context?: WorkflowInternalLoggerContext): void;
156
172
  }
173
+ type WorkflowClientOptions = {
174
+ logger?: WorkflowLogger;
175
+ } & ({
176
+ pool: pg.Pool;
177
+ connectionString?: never;
178
+ } | {
179
+ connectionString: string;
180
+ pool?: never;
181
+ });
182
+ type StartWorkflowOptions = {
183
+ resourceId?: string;
184
+ timeout?: number;
185
+ retries?: number;
186
+ expireInSeconds?: number;
187
+ };
188
+ declare class WorkflowClient {
189
+ private boss;
190
+ private db;
191
+ private pool;
192
+ private _ownsPool;
193
+ private _started;
194
+ private logger;
195
+ constructor({ logger,...connectionOptions }: WorkflowClientOptions);
196
+ start(): Promise<void>;
197
+ stop(): Promise<void>;
198
+ startWorkflow<TInput extends InputParameters>(ref: WorkflowRef<TInput>, input: InferInputParameters<TInput>, options?: StartWorkflowOptions): Promise<WorkflowRun>;
199
+ startWorkflow(params: {
200
+ workflowId: string;
201
+ input: unknown;
202
+ resourceId?: string;
203
+ options?: StartWorkflowOptions;
204
+ }): Promise<WorkflowRun>;
205
+ triggerEvent({ runId, resourceId, eventName, data, options }: {
206
+ runId: string;
207
+ resourceId?: string;
208
+ eventName: string;
209
+ data?: Record<string, unknown>;
210
+ options?: {
211
+ expireInSeconds?: number;
212
+ };
213
+ }): Promise<WorkflowRun>;
214
+ pauseWorkflow({ runId, resourceId }: {
215
+ runId: string;
216
+ resourceId?: string;
217
+ }): Promise<WorkflowRun>;
218
+ resumeWorkflow({ runId, resourceId, options }: {
219
+ runId: string;
220
+ resourceId?: string;
221
+ options?: {
222
+ expireInSeconds?: number;
223
+ };
224
+ }): Promise<WorkflowRun>;
225
+ fastForwardWorkflow({ runId, resourceId, data }: {
226
+ runId: string;
227
+ resourceId?: string;
228
+ data?: Record<string, unknown>;
229
+ }): Promise<WorkflowRun>;
230
+ cancelWorkflow({ runId, resourceId }: {
231
+ runId: string;
232
+ resourceId?: string;
233
+ }): Promise<WorkflowRun>;
234
+ getRun({ runId, resourceId }: {
235
+ runId: string;
236
+ resourceId?: string;
237
+ }): Promise<WorkflowRun>;
238
+ checkProgress({ runId, resourceId }: {
239
+ runId: string;
240
+ resourceId?: string;
241
+ }): Promise<WorkflowRunProgress>;
242
+ getRuns({ resourceId, startingAfter, endingBefore, limit, statuses, workflowId }: {
243
+ resourceId?: string;
244
+ startingAfter?: string | null;
245
+ endingBefore?: string | null;
246
+ limit?: number;
247
+ statuses?: WorkflowStatus[];
248
+ workflowId?: string;
249
+ }): Promise<{
250
+ items: WorkflowRun[];
251
+ nextCursor: string | null;
252
+ prevCursor: string | null;
253
+ hasMore: boolean;
254
+ hasPrev: boolean;
255
+ }>;
256
+ private ensureStarted;
257
+ }
258
+ /**
259
+ * Create a lightweight workflow reference.
260
+ * Safe to import from `pg-workflows/client` - no engine or handler code.
261
+ */
262
+ declare function createWorkflowRef<TInput extends InputParameters = InputParameters>(id: string, options?: {
263
+ inputSchema?: TInput;
264
+ }): WorkflowRef<TInput>;
157
265
  declare const workflow: WorkflowFactory;
158
- import pg from "pg";
266
+ import pg2 from "pg";
159
267
  import { Db, PgBoss } from "pg-boss";
268
+ type StartWorkflowOptions2 = {
269
+ resourceId?: string;
270
+ timeout?: number;
271
+ retries?: number;
272
+ expireInSeconds?: number;
273
+ batchSize?: number;
274
+ idempotencyKey?: string;
275
+ };
160
276
  type WorkflowEngineOptions = {
161
277
  workflows?: WorkflowDefinition[];
162
278
  logger?: WorkflowLogger;
163
279
  boss?: PgBoss;
164
280
  } & ({
165
- pool: pg.Pool;
281
+ pool: pg2.Pool;
166
282
  connectionString?: never;
167
283
  } | {
168
284
  connectionString: string;
@@ -185,17 +301,13 @@ declare class WorkflowEngine {
185
301
  registerWorkflow(definition: WorkflowDefinition<InputParameters>): Promise<WorkflowEngine>;
186
302
  unregisterWorkflow(workflowId: string): Promise<WorkflowEngine>;
187
303
  unregisterAllWorkflows(): Promise<WorkflowEngine>;
188
- startWorkflow({ resourceId, workflowId, input, idempotencyKey, options }: {
304
+ startWorkflow<TInput extends InputParameters>(ref: WorkflowRef<TInput>, input: InferInputParameters<TInput>, options?: StartWorkflowOptions2): Promise<WorkflowRun>;
305
+ startWorkflow(params: {
189
306
  resourceId?: string;
190
307
  workflowId: string;
191
308
  input: unknown;
192
309
  idempotencyKey?: string;
193
- options?: {
194
- timeout?: number;
195
- retries?: number;
196
- expireInSeconds?: number;
197
- batchSize?: number;
198
- };
310
+ options?: StartWorkflowOptions2;
199
311
  }): Promise<WorkflowRun>;
200
312
  pauseWorkflow({ runId, resourceId }: {
201
313
  runId: string;
@@ -286,4 +398,4 @@ declare class WorkflowEngineError extends Error {
286
398
  declare class WorkflowRunNotFoundError extends WorkflowEngineError {
287
399
  constructor(runId?: string, workflowId?: string);
288
400
  }
289
- export { workflow, parseDuration, WorkflowStatus, WorkflowRunProgress, WorkflowRunNotFoundError, WorkflowPlugin, WorkflowOptions, WorkflowLogger, WorkflowInternalLoggerContext, WorkflowInternalLogger, WorkflowInternalDefinition, WorkflowFactory, WorkflowEngineOptions, WorkflowEngineError, WorkflowEngine, WorkflowDefinition, WorkflowContext, StepType, StepInternalDefinition, StepBaseContext, InputParameters, InferInputParameters, DurationObject, Duration };
401
+ 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 };
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { StandardSchemaV1 } from "@standard-schema/spec";
1
+ import pg from "pg";
2
2
  type WorkflowRun = {
3
3
  id: string;
4
4
  createdAt: Date;
@@ -20,6 +20,7 @@ type WorkflowRun = {
20
20
  jobId: string | null;
21
21
  idempotencyKey: string | null;
22
22
  };
23
+ import { StandardSchemaV1 } from "@standard-schema/spec";
23
24
  type DurationObject = {
24
25
  weeks?: number;
25
26
  days?: number;
@@ -136,6 +137,21 @@ type WorkflowInternalDefinition<TInput extends InputParameters = InputParameters
136
137
  interface WorkflowFactory<TStepExt = object> {
137
138
  <I extends InputParameters = InputParameters>(id: string, handler: (context: WorkflowContext<I, StepBaseContext & TStepExt>) => Promise<unknown>, options?: WorkflowOptions<I>): WorkflowDefinition<I>;
138
139
  use<TNewExt>(plugin: WorkflowPlugin<StepBaseContext & TStepExt, TNewExt>): WorkflowFactory<TStepExt & TNewExt>;
140
+ ref<TInput extends InputParameters = InputParameters>(id: string, options?: {
141
+ inputSchema?: TInput;
142
+ }): WorkflowRef<TInput>;
143
+ }
144
+ /**
145
+ * Lightweight workflow reference - carries the workflow ID and input type
146
+ * but no handler code. Safe to import in API services without pulling in
147
+ * heavy worker dependencies.
148
+ *
149
+ * Callable: pass a handler to create a full WorkflowDefinition.
150
+ */
151
+ interface WorkflowRef<TInput extends InputParameters = InputParameters> {
152
+ (handler: (context: WorkflowContext<TInput, StepBaseContext>) => Promise<unknown>, options?: Omit<WorkflowOptions<TInput>, "inputSchema">): WorkflowDefinition<TInput>;
153
+ readonly id: string;
154
+ readonly inputSchema?: TInput;
139
155
  }
140
156
  type WorkflowRunProgress = WorkflowRun & {
141
157
  completionPercentage: number;
@@ -154,15 +170,115 @@ interface WorkflowInternalLogger {
154
170
  log(message: string, context?: WorkflowInternalLoggerContext): void;
155
171
  error(message: string, error: Error, context?: WorkflowInternalLoggerContext): void;
156
172
  }
173
+ type WorkflowClientOptions = {
174
+ logger?: WorkflowLogger;
175
+ } & ({
176
+ pool: pg.Pool;
177
+ connectionString?: never;
178
+ } | {
179
+ connectionString: string;
180
+ pool?: never;
181
+ });
182
+ type StartWorkflowOptions = {
183
+ resourceId?: string;
184
+ timeout?: number;
185
+ retries?: number;
186
+ expireInSeconds?: number;
187
+ };
188
+ declare class WorkflowClient {
189
+ private boss;
190
+ private db;
191
+ private pool;
192
+ private _ownsPool;
193
+ private _started;
194
+ private logger;
195
+ constructor({ logger,...connectionOptions }: WorkflowClientOptions);
196
+ start(): Promise<void>;
197
+ stop(): Promise<void>;
198
+ startWorkflow<TInput extends InputParameters>(ref: WorkflowRef<TInput>, input: InferInputParameters<TInput>, options?: StartWorkflowOptions): Promise<WorkflowRun>;
199
+ startWorkflow(params: {
200
+ workflowId: string;
201
+ input: unknown;
202
+ resourceId?: string;
203
+ options?: StartWorkflowOptions;
204
+ }): Promise<WorkflowRun>;
205
+ triggerEvent({ runId, resourceId, eventName, data, options }: {
206
+ runId: string;
207
+ resourceId?: string;
208
+ eventName: string;
209
+ data?: Record<string, unknown>;
210
+ options?: {
211
+ expireInSeconds?: number;
212
+ };
213
+ }): Promise<WorkflowRun>;
214
+ pauseWorkflow({ runId, resourceId }: {
215
+ runId: string;
216
+ resourceId?: string;
217
+ }): Promise<WorkflowRun>;
218
+ resumeWorkflow({ runId, resourceId, options }: {
219
+ runId: string;
220
+ resourceId?: string;
221
+ options?: {
222
+ expireInSeconds?: number;
223
+ };
224
+ }): Promise<WorkflowRun>;
225
+ fastForwardWorkflow({ runId, resourceId, data }: {
226
+ runId: string;
227
+ resourceId?: string;
228
+ data?: Record<string, unknown>;
229
+ }): Promise<WorkflowRun>;
230
+ cancelWorkflow({ runId, resourceId }: {
231
+ runId: string;
232
+ resourceId?: string;
233
+ }): Promise<WorkflowRun>;
234
+ getRun({ runId, resourceId }: {
235
+ runId: string;
236
+ resourceId?: string;
237
+ }): Promise<WorkflowRun>;
238
+ checkProgress({ runId, resourceId }: {
239
+ runId: string;
240
+ resourceId?: string;
241
+ }): Promise<WorkflowRunProgress>;
242
+ getRuns({ resourceId, startingAfter, endingBefore, limit, statuses, workflowId }: {
243
+ resourceId?: string;
244
+ startingAfter?: string | null;
245
+ endingBefore?: string | null;
246
+ limit?: number;
247
+ statuses?: WorkflowStatus[];
248
+ workflowId?: string;
249
+ }): Promise<{
250
+ items: WorkflowRun[];
251
+ nextCursor: string | null;
252
+ prevCursor: string | null;
253
+ hasMore: boolean;
254
+ hasPrev: boolean;
255
+ }>;
256
+ private ensureStarted;
257
+ }
258
+ /**
259
+ * Create a lightweight workflow reference.
260
+ * Safe to import from `pg-workflows/client` - no engine or handler code.
261
+ */
262
+ declare function createWorkflowRef<TInput extends InputParameters = InputParameters>(id: string, options?: {
263
+ inputSchema?: TInput;
264
+ }): WorkflowRef<TInput>;
157
265
  declare const workflow: WorkflowFactory;
158
- import pg from "pg";
266
+ import pg2 from "pg";
159
267
  import { Db, PgBoss } from "pg-boss";
268
+ type StartWorkflowOptions2 = {
269
+ resourceId?: string;
270
+ timeout?: number;
271
+ retries?: number;
272
+ expireInSeconds?: number;
273
+ batchSize?: number;
274
+ idempotencyKey?: string;
275
+ };
160
276
  type WorkflowEngineOptions = {
161
277
  workflows?: WorkflowDefinition[];
162
278
  logger?: WorkflowLogger;
163
279
  boss?: PgBoss;
164
280
  } & ({
165
- pool: pg.Pool;
281
+ pool: pg2.Pool;
166
282
  connectionString?: never;
167
283
  } | {
168
284
  connectionString: string;
@@ -185,17 +301,13 @@ declare class WorkflowEngine {
185
301
  registerWorkflow(definition: WorkflowDefinition<InputParameters>): Promise<WorkflowEngine>;
186
302
  unregisterWorkflow(workflowId: string): Promise<WorkflowEngine>;
187
303
  unregisterAllWorkflows(): Promise<WorkflowEngine>;
188
- startWorkflow({ resourceId, workflowId, input, idempotencyKey, options }: {
304
+ startWorkflow<TInput extends InputParameters>(ref: WorkflowRef<TInput>, input: InferInputParameters<TInput>, options?: StartWorkflowOptions2): Promise<WorkflowRun>;
305
+ startWorkflow(params: {
189
306
  resourceId?: string;
190
307
  workflowId: string;
191
308
  input: unknown;
192
309
  idempotencyKey?: string;
193
- options?: {
194
- timeout?: number;
195
- retries?: number;
196
- expireInSeconds?: number;
197
- batchSize?: number;
198
- };
310
+ options?: StartWorkflowOptions2;
199
311
  }): Promise<WorkflowRun>;
200
312
  pauseWorkflow({ runId, resourceId }: {
201
313
  runId: string;
@@ -286,4 +398,4 @@ declare class WorkflowEngineError extends Error {
286
398
  declare class WorkflowRunNotFoundError extends WorkflowEngineError {
287
399
  constructor(runId?: string, workflowId?: string);
288
400
  }
289
- export { workflow, parseDuration, WorkflowStatus, WorkflowRunProgress, WorkflowRunNotFoundError, WorkflowPlugin, WorkflowOptions, WorkflowLogger, WorkflowInternalLoggerContext, WorkflowInternalLogger, WorkflowInternalDefinition, WorkflowFactory, WorkflowEngineOptions, WorkflowEngineError, WorkflowEngine, WorkflowDefinition, WorkflowContext, StepType, StepInternalDefinition, StepBaseContext, InputParameters, InferInputParameters, DurationObject, Duration };
401
+ 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 };