@brandboostinggmbh/observable-workflows 0.2.2 → 0.3.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.ts CHANGED
@@ -252,6 +252,7 @@ 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
@@ -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,17 +302,22 @@ 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
315
  async function createWorkflowContext(options) {
306
316
  await ensureTables(options.D1);
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 }) => {
316
323
  const instanceId = internalContext.idFactory();
@@ -477,6 +484,7 @@ function createQueueWorkflowContext(options) {
477
484
  //#endregion
478
485
  //#region src/observableWorkflows/createLogAccessor.ts
479
486
  const createLogAccessor = (context) => {
487
+ const internalSerializer = context.serializer ?? defaultSerializer;
480
488
  const listSteps = async (limit, offset, instanceId) => {
481
489
  const result = await context.D1.prepare(
482
490
  /* sql */
@@ -487,12 +495,12 @@ const createLogAccessor = (context) => {
487
495
  return result.results ? result.results.map((row) => ({
488
496
  instanceId: row.instanceId,
489
497
  name: row.stepName,
490
- metadata: tryDeserializeObj(row.stepMetadata, context.serializer),
498
+ metadata: tryDeserializeObj(row.stepMetadata, internalSerializer),
491
499
  status: row.stepStatus,
492
500
  startTime: row.startTime,
493
501
  endTime: row.endTime,
494
- result: row.result ? tryDeserializeObj(row.result, context.serializer) : null,
495
- error: row.error ? tryDeserializeObj(row.error, context.serializer) : null
502
+ result: row.result ? tryDeserializeObj(row.result, internalSerializer) : null,
503
+ error: row.error ? tryDeserializeObj(row.error, internalSerializer) : null
496
504
  })) : [];
497
505
  };
498
506
  const listWorkflows = async (limit, offset) => {
@@ -503,14 +511,14 @@ const createLogAccessor = (context) => {
503
511
  WHERE tenantId = ?
504
512
  ORDER BY startTime DESC LIMIT ? OFFSET ?`
505
513
  ).bind(context.tenantId, limit, offset).all();
506
- return result.results ? result.results.map((row) => workflowTableRowToWorkflowRun(row, context.serializer)) : [];
514
+ return result.results ? result.results.map((row) => workflowTableRowToWorkflowRun(row, internalSerializer)) : [];
507
515
  };
508
516
  const getWorkflowByParentId = async (parentInstanceId) => {
509
517
  const result = await context.D1.prepare(
510
518
  /* sql */
511
519
  `SELECT * FROM WorkflowTable WHERE parentInstanceId = ? AND tenantId = ?`
512
520
  ).bind(parentInstanceId, context.tenantId).all();
513
- return result.results ? result.results.map((row) => workflowTableRowToWorkflowRun(row, context.serializer)) : null;
521
+ return result.results ? result.results.map((row) => workflowTableRowToWorkflowRun(row, internalSerializer)) : null;
514
522
  };
515
523
  const getWorkflowTypesByTenantId = async (tenantId) => {
516
524
  const result = await context.D1.prepare(
@@ -529,7 +537,7 @@ const createLogAccessor = (context) => {
529
537
  return result.results.map((row) => ({
530
538
  key: row.key,
531
539
  valueType: row.valueType,
532
- value: tryDeserializeObj(row.value, context.serializer)
540
+ value: tryDeserializeObj(row.value, internalSerializer)
533
541
  }));
534
542
  };
535
543
  /** This function gets the basic data of a workflow, without populating any of it's complex fields */
@@ -539,7 +547,7 @@ const createLogAccessor = (context) => {
539
547
  `SELECT * FROM WorkflowTable WHERE instanceId = ? AND tenantId = ?`
540
548
  ).bind(instanceId, context.tenantId).first();
541
549
  if (!result) return null;
542
- const workflow = workflowTableRowToWorkflowRun(result, context.serializer);
550
+ const workflow = workflowTableRowToWorkflowRun(result, internalSerializer);
543
551
  return workflow;
544
552
  };
545
553
  const getWorkflow = async (instanceId) => {
@@ -584,12 +592,12 @@ const createLogAccessor = (context) => {
584
592
  const step = {
585
593
  instanceId: row.instanceId,
586
594
  name: row.stepName,
587
- metadata: tryDeserializeObj(row.stepMetadata, context.serializer),
595
+ metadata: tryDeserializeObj(row.stepMetadata, internalSerializer),
588
596
  status: row.stepStatus,
589
597
  startTime: row.startTime,
590
598
  endTime: row.endTime,
591
- result: row.result ? tryDeserializeObj(row.result, context.serializer) : null,
592
- error: row.error ? tryDeserializeObj(row.error, context.serializer) : null,
599
+ result: row.result ? tryDeserializeObj(row.result, internalSerializer) : null,
600
+ error: row.error ? tryDeserializeObj(row.error, internalSerializer) : null,
593
601
  logs: []
594
602
  };
595
603
  const logResult = await context.D1.prepare(
@@ -630,4 +638,4 @@ const createLogAccessor = (context) => {
630
638
  };
631
639
 
632
640
  //#endregion
633
- export { createLogAccessor, createQueueWorkflowContext, createStepContext, createWorkflowContext, defineWorkflow, ensureTables, finalizeWorkflowRecord, insertStepRecordFull, insertWorkflowRecord, pushLogToDB, tryDeserializeObj, updateWorkflowName, upsertWorkflowProperty, workflowTableRowToWorkflowRun };
641
+ 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.0",
4
4
  "description": "My awesome typescript library",
5
5
  "type": "module",
6
6
  "license": "MIT",