@brandboostinggmbh/observable-workflows 0.2.2 → 0.3.1

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.ts CHANGED
@@ -252,10 +252,11 @@ declare function defineWorkflow<I extends {} | null>(workflow: {
252
252
  workflowType: string;
253
253
  metadata?: Record<string, any>;
254
254
  }, callback: (input: I, ctx: WorkflowContext) => Promise<any>): WorkflowFunction<I>;
255
+ declare function defineWorkflow<I extends {} | null>(workflowType: string, callback: (input: I, ctx: WorkflowContext) => Promise<any>): WorkflowFunction<I>;
255
256
 
256
257
  //#endregion
257
258
  //#region src/observableWorkflows/createWorkflowContext.d.ts
258
- declare function createWorkflowContext(options: WorkflowContextOptions): Promise<{
259
+ declare function createWorkflowContext(options: WorkflowContextOptions): {
259
260
  call: <I>({
260
261
  workflow,
261
262
  input,
@@ -270,7 +271,7 @@ declare function createWorkflowContext(options: WorkflowContextOptions): Promise
270
271
  parentInstanceId?: string | undefined;
271
272
  }) => Promise<void>;
272
273
  retry: <I>(workflow: WorkflowFunction<I>, retryInstanceId: string) => Promise<void>;
273
- }>;
274
+ };
274
275
 
275
276
  //#endregion
276
277
  //#region src/observableWorkflows/createQueueWorkflowContext.d.ts
@@ -287,7 +288,7 @@ declare function createQueueWorkflowContext(options: QueueWorkflowContextOptions
287
288
  declare const createLogAccessor: (context: {
288
289
  D1: D1Database;
289
290
  tenantId: string;
290
- serializer: Serializer;
291
+ serializer?: Serializer;
291
292
  }) => {
292
293
  listSteps: (limit: number, offset: number, instanceId?: string | undefined) => Promise<Step[]>;
293
294
  getStep: (instanceId: string, stepName: string) => Promise<Step | null>;
@@ -298,4 +299,15 @@ declare const createLogAccessor: (context: {
298
299
  };
299
300
 
300
301
  //#endregion
301
- export { ConsoleWrapper, InternalWorkflowContextOptions, Log, PossibleValueTypeNames, PossibleValueTypes, QueueWorkflowContextOptions, Serializer, Step, StepContextOptions, StepCtx, StepWorkflowStatus, ValueTypeMap, WorkflowContext, WorkflowContextOptions, WorkflowFunction, WorkflowProperty, WorkflowPropertyDefinition, WorkflowQueueMessage, WorkflowRun, createLogAccessor, createQueueWorkflowContext, createStepContext, createWorkflowContext, defineWorkflow, ensureTables, finalizeWorkflowRecord, insertStepRecordFull, insertWorkflowRecord, pushLogToDB, tryDeserializeObj, updateWorkflowName, upsertWorkflowProperty, workflowTableRowToWorkflowRun };
302
+ //#region src/observableWorkflows/defaultImplementations.d.ts
303
+ declare const defaultSerializer: {
304
+ serialize: {
305
+ (value: any, replacer?: (this: any, key: string, value: any) => any, space?: string | number): string;
306
+ (value: any, replacer?: (number | string)[] | null, space?: string | number): string;
307
+ };
308
+ deserialize: (text: string, reviver?: (this: any, key: string, value: any) => any) => any;
309
+ };
310
+ declare const defaultIdFactory: () => string;
311
+
312
+ //#endregion
313
+ export { ConsoleWrapper, InternalWorkflowContextOptions, Log, PossibleValueTypeNames, PossibleValueTypes, QueueWorkflowContextOptions, Serializer, Step, StepContextOptions, StepCtx, StepWorkflowStatus, ValueTypeMap, WorkflowContext, WorkflowContextOptions, WorkflowFunction, WorkflowProperty, WorkflowPropertyDefinition, WorkflowQueueMessage, WorkflowRun, createLogAccessor, createQueueWorkflowContext, createStepContext, createWorkflowContext, defaultIdFactory, defaultSerializer, defineWorkflow, ensureTables, finalizeWorkflowRecord, insertStepRecordFull, insertWorkflowRecord, pushLogToDB, tryDeserializeObj, updateWorkflowName, upsertWorkflowProperty, workflowTableRowToWorkflowRun };
package/dist/index.js CHANGED
@@ -177,9 +177,11 @@ async function upsertWorkflowProperty({ context, instanceId, key, valueType, val
177
177
  //#endregion
178
178
  //#region src/observableWorkflows/defineWorkflow.ts
179
179
  function defineWorkflow(workflow, callback) {
180
+ const metadata = typeof workflow === "string" ? void 0 : workflow.metadata;
181
+ const workflowType = typeof workflow === "string" ? workflow : workflow.workflowType;
180
182
  return {
181
- workflowType: workflow.workflowType,
182
- metadata: workflow.metadata || {},
183
+ workflowType,
184
+ metadata: metadata || {},
183
185
  _callback: callback
184
186
  };
185
187
  }
@@ -300,19 +302,28 @@ async function createStepContext(context) {
300
302
  return step;
301
303
  }
302
304
 
305
+ //#endregion
306
+ //#region src/observableWorkflows/defaultImplementations.ts
307
+ const defaultSerializer = {
308
+ serialize: JSON.stringify,
309
+ deserialize: JSON.parse
310
+ };
311
+ const defaultIdFactory = () => crypto.randomUUID();
312
+
303
313
  //#endregion
304
314
  //#region src/observableWorkflows/createWorkflowContext.ts
305
- async function createWorkflowContext(options) {
306
- await ensureTables(options.D1);
315
+ function createWorkflowContext(options) {
316
+ let ensuredTables = false;
307
317
  const internalContext = {
308
318
  ...options,
309
- serializer: options.serializer ?? {
310
- serialize: JSON.stringify,
311
- deserialize: JSON.parse
312
- },
313
- idFactory: options.idFactory ?? (() => crypto.randomUUID())
319
+ serializer: options.serializer ?? defaultSerializer,
320
+ idFactory: options.idFactory ?? defaultIdFactory
314
321
  };
315
322
  const call = async ({ workflow, input, workflowName, tenantId, parentInstanceId }) => {
323
+ if (!ensuredTables) {
324
+ await ensureTables(options.D1);
325
+ ensuredTables = true;
326
+ }
316
327
  const instanceId = internalContext.idFactory();
317
328
  const startTime = Date.now();
318
329
  await insertWorkflowRecord(internalContext, {
@@ -402,6 +413,10 @@ async function createWorkflowContext(options) {
402
413
  }
403
414
  };
404
415
  const retry = async (workflow, retryInstanceId) => {
416
+ if (!ensuredTables) {
417
+ await ensureTables(options.D1);
418
+ ensuredTables = true;
419
+ }
405
420
  const oldRun = await options.D1.prepare(
406
421
  /* sql */
407
422
  `SELECT input, workflowName, tenantId FROM WorkflowTable WHERE instanceId = ? `
@@ -477,6 +492,7 @@ function createQueueWorkflowContext(options) {
477
492
  //#endregion
478
493
  //#region src/observableWorkflows/createLogAccessor.ts
479
494
  const createLogAccessor = (context) => {
495
+ const internalSerializer = context.serializer ?? defaultSerializer;
480
496
  const listSteps = async (limit, offset, instanceId) => {
481
497
  const result = await context.D1.prepare(
482
498
  /* sql */
@@ -487,12 +503,12 @@ const createLogAccessor = (context) => {
487
503
  return result.results ? result.results.map((row) => ({
488
504
  instanceId: row.instanceId,
489
505
  name: row.stepName,
490
- metadata: tryDeserializeObj(row.stepMetadata, context.serializer),
506
+ metadata: tryDeserializeObj(row.stepMetadata, internalSerializer),
491
507
  status: row.stepStatus,
492
508
  startTime: row.startTime,
493
509
  endTime: row.endTime,
494
- result: row.result ? tryDeserializeObj(row.result, context.serializer) : null,
495
- error: row.error ? tryDeserializeObj(row.error, context.serializer) : null
510
+ result: row.result ? tryDeserializeObj(row.result, internalSerializer) : null,
511
+ error: row.error ? tryDeserializeObj(row.error, internalSerializer) : null
496
512
  })) : [];
497
513
  };
498
514
  const listWorkflows = async (limit, offset) => {
@@ -503,14 +519,14 @@ const createLogAccessor = (context) => {
503
519
  WHERE tenantId = ?
504
520
  ORDER BY startTime DESC LIMIT ? OFFSET ?`
505
521
  ).bind(context.tenantId, limit, offset).all();
506
- return result.results ? result.results.map((row) => workflowTableRowToWorkflowRun(row, context.serializer)) : [];
522
+ return result.results ? result.results.map((row) => workflowTableRowToWorkflowRun(row, internalSerializer)) : [];
507
523
  };
508
524
  const getWorkflowByParentId = async (parentInstanceId) => {
509
525
  const result = await context.D1.prepare(
510
526
  /* sql */
511
527
  `SELECT * FROM WorkflowTable WHERE parentInstanceId = ? AND tenantId = ?`
512
528
  ).bind(parentInstanceId, context.tenantId).all();
513
- return result.results ? result.results.map((row) => workflowTableRowToWorkflowRun(row, context.serializer)) : null;
529
+ return result.results ? result.results.map((row) => workflowTableRowToWorkflowRun(row, internalSerializer)) : null;
514
530
  };
515
531
  const getWorkflowTypesByTenantId = async (tenantId) => {
516
532
  const result = await context.D1.prepare(
@@ -529,7 +545,7 @@ const createLogAccessor = (context) => {
529
545
  return result.results.map((row) => ({
530
546
  key: row.key,
531
547
  valueType: row.valueType,
532
- value: tryDeserializeObj(row.value, context.serializer)
548
+ value: tryDeserializeObj(row.value, internalSerializer)
533
549
  }));
534
550
  };
535
551
  /** This function gets the basic data of a workflow, without populating any of it's complex fields */
@@ -539,7 +555,7 @@ const createLogAccessor = (context) => {
539
555
  `SELECT * FROM WorkflowTable WHERE instanceId = ? AND tenantId = ?`
540
556
  ).bind(instanceId, context.tenantId).first();
541
557
  if (!result) return null;
542
- const workflow = workflowTableRowToWorkflowRun(result, context.serializer);
558
+ const workflow = workflowTableRowToWorkflowRun(result, internalSerializer);
543
559
  return workflow;
544
560
  };
545
561
  const getWorkflow = async (instanceId) => {
@@ -584,12 +600,12 @@ const createLogAccessor = (context) => {
584
600
  const step = {
585
601
  instanceId: row.instanceId,
586
602
  name: row.stepName,
587
- metadata: tryDeserializeObj(row.stepMetadata, context.serializer),
603
+ metadata: tryDeserializeObj(row.stepMetadata, internalSerializer),
588
604
  status: row.stepStatus,
589
605
  startTime: row.startTime,
590
606
  endTime: row.endTime,
591
- result: row.result ? tryDeserializeObj(row.result, context.serializer) : null,
592
- error: row.error ? tryDeserializeObj(row.error, context.serializer) : null,
607
+ result: row.result ? tryDeserializeObj(row.result, internalSerializer) : null,
608
+ error: row.error ? tryDeserializeObj(row.error, internalSerializer) : null,
593
609
  logs: []
594
610
  };
595
611
  const logResult = await context.D1.prepare(
@@ -630,4 +646,4 @@ const createLogAccessor = (context) => {
630
646
  };
631
647
 
632
648
  //#endregion
633
- export { createLogAccessor, createQueueWorkflowContext, createStepContext, createWorkflowContext, defineWorkflow, ensureTables, finalizeWorkflowRecord, insertStepRecordFull, insertWorkflowRecord, pushLogToDB, tryDeserializeObj, updateWorkflowName, upsertWorkflowProperty, workflowTableRowToWorkflowRun };
649
+ export { createLogAccessor, createQueueWorkflowContext, createStepContext, createWorkflowContext, defaultIdFactory, defaultSerializer, defineWorkflow, ensureTables, finalizeWorkflowRecord, insertStepRecordFull, insertWorkflowRecord, pushLogToDB, tryDeserializeObj, updateWorkflowName, upsertWorkflowProperty, workflowTableRowToWorkflowRun };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@brandboostinggmbh/observable-workflows",
3
- "version": "0.2.2",
3
+ "version": "0.3.1",
4
4
  "description": "My awesome typescript library",
5
5
  "type": "module",
6
6
  "license": "MIT",