@datarster/workflow-engine

中文文档
0.0.21发布于 2025/07/14MITTypeScriptCommonJS近 30 天下载 5497

The Workflow Engine is a powerful tool designed to automate and manage complex workflows. It allows users to define, execute, and monitor workflows with ease.

原始使用文档
文档目录25 节
  1. Overview
  2. Features
  3. Installation
  4. Usage
  5. Import and Initialize the Engine
  6. Define a Workflow
  7. Register Workers
  8. Execute the Workflow
  9. Resume Workflow with Event
  10. Fetch Workflow Logs
  11. Supported Workflow Step Types
  12. Public API Methods
  13. WorkflowStep Interface (Advanced)
  14. Advanced Workflow Examples
  15. Switch/Case Workflow
  16. Parallel Execution
  17. Event-Driven Workflow
  18. Configuration Options
  19. Redis Configuration
  20. MongoDB Configuration
  21. Error Handling and Rollback
  22. State Management
  23. TypeScript Support
  24. Contributing
  25. License

Workflow Engine

Overview

@datarster/workflow-engine is a powerful and extensible workflow management library for Node.js. It enables the execution of workflows using a JSON-based workflow definition. This engine supports sequential, parallel, conditional, looping, switch-case, and event-based workflows, with optional rollback functionality in case of failure.

Features

  • Supports various workflow step types: task, decision, parallel, loop, wait, commit, execute_then_wait, and switch.
  • Event-driven execution with workflow resumption capabilities.
  • Integration with Redis for state management and workflow caching.
  • MongoDB logging for workflow execution history.
  • Rollback support for failed steps with custom rollback workers.
  • Dynamic worker registration with timeout support.
  • Switch/case logic for conditional execution.
  • Flexible configuration for Redis and MongoDB connections.

Installation

npm install @datarster/workflow-engine

Usage

Import and Initialize the Engine

import { WorkflowEngine } from "@datarster/workflow-engine";

const config = {
    redis: { host: "localhost", port: 6379 },
    useLogging: true,
    mongo: { uri: "mongodb://localhost:27017", database: "workflowDB", collection: "logs" }
};

const engine = new WorkflowEngine();
await engine.init(config);

Define a Workflow

const workflow = {
    name: "Order Processing",
    steps: [
        { 
            id: "1", 
            type: "task", 
            name: "Validate Order", 
            worker: "validateOrder",
            workerTimeout: 30000,
            rollback: {
                id: "1_rollback",
                name: "Rollback Validation",
                type: "task",
                worker: "validateOrderRollback"
            }
        },
        { id: "2", type: "task", name: "Process Payment", worker: "processPayment" },
        { id: "3", type: "task", name: "Ship Order", worker: "shipOrder" }
    ]
};

await engine.registerWorkflow("order_processing", workflow);

Register Workers

engine.registerWorker("validateOrder", async (data) => {
    console.log("Validating order", data);
    // Your validation logic here
});

engine.registerWorker("validateOrderRollback", async (data) => {
    console.log("Rolling back order validation", data);
    // Your rollback logic here
});

engine.registerWorker("processPayment", async (data) => {
    console.log("Processing payment", data);
    // Your payment logic here
});

engine.registerWorker("shipOrder", async (data) => {
    console.log("Shipping order", data);
    // Your shipping logic here
});

Execute the Workflow

const executionId = await engine.executeWorkflow({
    workflowId: "order_processing",
    inputData: { orderId: 12345 },
    callbackOptions: {
        onStepComplete: (step, result) => {
            console.log(`Step ${step.name} completed:`, result);
        },
        onError: (step, error) => {
            console.error(`Step ${step.name} failed:`, error);
        }
    }
});
console.log("Workflow execution started with ID:", executionId);

Resume Workflow with Event

// Resume a waiting workflow with an event
await engine.resumeWorkflow(executionId, "external_approval_received", {
    approved: true,
    approver: "[email protected]"
});

Fetch Workflow Logs

const log = await engine.fetchWorkflowLog(executionId);
console.log("Execution Log:", log);

Supported Workflow Step Types

Step TypeDescription
taskExecutes a registered worker function.
decisionEvaluates a condition and executes a true/false branch.
parallelRuns multiple steps in parallel.
loopRepeats execution based on a condition.
waitPauses execution until an event is received.
execute_then_waitExecutes a task and then waits for an external event.
switchEvaluates a field value and executes matching case.
commitMarks a workflow step as a commit step.
switch_caseRepresents a case within a switch step (used internally in switch logic).

Public API Methods

The following methods are available on the WorkflowEngine class:

MethodDescription
init(config)Initializes the engine with Redis and/or MongoDB configuration.
registerWorkflow(id, workflow)Registers a workflow definition with a unique ID.
registerWorker(name, worker)Registers a worker function by name.
executeWorkflow(options)Executes a registered workflow. Returns an execution ID.
resumeWorkflow(executionId, eventName, inputData, options?)Resumes a waiting workflow when an event is received.

Note: The fetchWorkflowLog method is shown in usage examples, but is not implemented in the main class. For workflow logs, use your own MongoDB queries or extend the engine as needed.

WorkflowStep Interface (Advanced)

Each workflow step supports the following fields:

  • id (string): Unique identifier for the step.
  • type (string): Step type (see Supported Workflow Step Types).
  • name (string): Name of the step.
  • worker (string, optional): Name of the worker function to execute.
  • condition (string, optional): Condition for decision/loop steps.
  • true_branch / false_branch (array, optional): Steps for decision branches.
  • parallel_steps (array, optional): Steps to run in parallel.
  • switch_cases (array, optional): Cases for switch steps.
  • switch_case_steps (array, optional): Steps for a specific switch case.
  • event_name (string, optional): Event to wait for (wait/execute_then_wait).
  • rollback (object, optional): Step to execute for rollback.
  • status (string, optional): Status of the step.
  • createdAt / updatedAt (string, optional): Timestamps.
  • workerTimeout (number, optional): Timeout for the worker in ms.
  • field / value (any, optional): Used in switch/case logic.
  • default_case (object, optional): Default step for switch.

Advanced Workflow Examples

Switch/Case Workflow

const switchWorkflow = {
    name: "Order Status Processing",
    steps: [
        {
            id: "1",
            type: "switch",
            name: "Process Based on Status",
            switch_cases: [
                {
                    id: "case1",
                    field: "status",
                    value: "COMMITTED",
                    switch_case_steps: [
                        { id: "1.1", type: "task", name: "Handle Committed", worker: "handleCommitted" }
                    ]
                },
                {
                    id: "case2", 
                    field: "status",
                    value: "CANCELLED",
                    switch_case_steps: [
                        { id: "1.2", type: "task", name: "Handle Cancelled", worker: "handleCancelled" }
                    ]
                }
            ],
            default_case: {
                id: "default",
                type: "task",
                name: "Handle Default",
                worker: "handleDefault"
            }
        }
    ]
};

Parallel Execution

const parallelWorkflow = {
    name: "Parallel Processing",
    steps: [
        {
            id: "1",
            type: "parallel",
            name: "Process Multiple Tasks",
            parallel_steps: [
                { id: "1.1", type: "task", name: "Task A", worker: "workerA" },
                { id: "1.2", type: "task", name: "Task B", worker: "workerB" },
                { id: "1.3", type: "task", name: "Task C", worker: "workerC" }
            ]
        }
    ]
};

Event-Driven Workflow

const eventWorkflow = {
    name: "Event-Driven Process",
    steps: [
        { id: "1", type: "task", name: "Initial Task", worker: "initialWorker" },
        {
            id: "2",
            type: "wait",
            name: "Wait for Approval",
            event_name: "approval_received"
        },
        { id: "3", type: "task", name: "Final Task", worker: "finalWorker" }
    ]
};

Configuration Options

Redis Configuration

const redisConfig = {
    host: "localhost",
    port: 6379,
    // OR provide your own Redis client
    redisClient: existingRedisClient
};

MongoDB Configuration

const mongoConfig = {
    uri: "mongodb://localhost:27017",
    database: "workflowDB",
    collection: "logs",
    // OR provide your own MongoDB client
    mongoClient: existingMongoClient
};

Error Handling and Rollback

  • If a task fails, the workflow engine triggers rollback steps if they are defined.
  • Rollback steps are executed in reverse order (LIFO - Last In, First Out).
  • Errors during rollback steps are logged but do not halt the rollback process.
  • Each step can have its own rollback logic defined in the rollback property.

State Management

  • Redis: Used for caching workflow execution state and registered workflows.
  • MongoDB: Stores detailed workflow execution history and logs (optional).
  • In-Memory: Falls back to in-memory storage if Redis is not configured.

TypeScript Support

The library is written in TypeScript and provides full type definitions:

import { WorkflowEngine, WorkflowStepType, WorkflowDefinition } from "@datarster/workflow-engine";

Contributing

Contributions are welcome! Feel free to submit issues or pull requests on GitHub.

License

MIT

版本与发布记录

@datarster/workflow-engine 共发布 21 个版本,首次发布于 2025/02/25;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.0.21latest2025/07/141.3 年前
0.0.202025/07/141.3 年前
0.0.192025/06/261.4 年前
0.0.182025/06/261.4 年前
0.0.172025/06/121.4 年前
0.0.162025/06/121.4 年前

关于 @datarster/workflow-engine 的常见问题

@datarster/workflow-engine 是什么?

The Workflow Engine is a powerful tool designed to automate and manage complex workflows. It allows users to define, execute, and monitor workflows with ease.

如何安装 @datarster/workflow-engine?

在项目目录运行 npm install @datarster/workflow-engine,或使用 pnpm add / yarn add / bun add。

@datarster/workflow-engine 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@datarster/workflow-engine 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@datarster/workflow-engine 可以商用吗?

@datarster/workflow-engine 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@datarster/workflow-engine 的下载量有多少?

根据 npm 官方下载统计,@datarster/workflow-engine 在 2026/10/12 – 2026/11/10 这 30 天内被下载 5,497 次,日均约 183 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@datarster/workflow-engine 还在维护吗?

最新版本 0.0.21 发布于 2025/07/14(1.3 年前);至今共发布 21 个版本,其中过去 12 个月发布了 0 个;首次发布于 2025/02/25。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。