dbos-typescript

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

DBOS TypeScript Best Practices

DBOS TypeScript 最佳实践

Guide for building reliable, fault-tolerant TypeScript applications with DBOS durable workflows.
本指南介绍如何使用 DBOS 持久化工作流构建可靠、容错的 TypeScript 应用。

When to Apply

适用场景

Reference these guidelines when:
  • Adding DBOS to existing TypeScript code
  • Creating workflows and steps
  • Using queues for concurrency control
  • Implementing workflow communication (events, messages, streams)
  • Configuring and launching DBOS applications
  • Using DBOSClient from external applications
  • Testing DBOS applications
在以下场景中可参考本指南:
  • 为现有 TypeScript 代码集成 DBOS
  • 创建工作流与步骤
  • 使用队列进行并发控制
  • 实现工作流通信(事件、消息、流)
  • 配置与启动 DBOS 应用
  • 从外部应用调用 DBOSClient
  • 测试 DBOS 应用

Rule Categories by Priority

按优先级划分的规则类别

PriorityCategoryImpactPrefix
1LifecycleCRITICAL
lifecycle-
2WorkflowCRITICAL
workflow-
3StepHIGH
step-
4QueueHIGH
queue-
5CommunicationMEDIUM
comm-
6PatternMEDIUM
pattern-
7TestingLOW-MEDIUM
test-
8ClientMEDIUM
client-
9AdvancedLOW
advanced-
优先级类别影响程度前缀
1生命周期严重(CRITICAL)
lifecycle-
2工作流严重(CRITICAL)
workflow-
3步骤高(HIGH)
step-
4队列高(HIGH)
queue-
5通信中(MEDIUM)
comm-
6模式中(MEDIUM)
pattern-
7测试中低(LOW-MEDIUM)
test-
8客户端中(MEDIUM)
client-
9高级特性低(LOW)
advanced-

Critical Rules

核心规则

Installation

安装

Always install the latest version of DBOS:
bash
npm install @dbos-inc/dbos-sdk@latest
请始终安装最新版本的 DBOS:
bash
npm install @dbos-inc/dbos-sdk@latest

DBOS Configuration and Launch

DBOS 配置与启动

A DBOS application MUST configure and launch DBOS before running any workflows:
typescript
import { DBOS } from "@dbos-inc/dbos-sdk";

async function main() {
  DBOS.setConfig({
    name: "my-app",
    systemDatabaseUrl: process.env.DBOS_SYSTEM_DATABASE_URL,
  });
  await DBOS.launch();
  await myWorkflow();
}

main().catch(console.log);
DBOS 应用必须在运行任何工作流之前完成配置与启动:
typescript
import { DBOS } from "@dbos-inc/dbos-sdk";

async function main() {
  DBOS.setConfig({
    name: "my-app",
    systemDatabaseUrl: process.env.DBOS_SYSTEM_DATABASE_URL,
  });
  await DBOS.launch();
  await myWorkflow();
}

main().catch(console.log);

Workflow and Step Structure

工作流与步骤结构

Workflows are comprised of steps. Any function performing complex operations or accessing external services must be run as a step using
DBOS.runStep
:
typescript
import { DBOS } from "@dbos-inc/dbos-sdk";

async function fetchData() {
  return await fetch("https://api.example.com").then(r => r.json());
}

async function myWorkflowFn() {
  const result = await DBOS.runStep(fetchData, { name: "fetchData" });
  return result;
}
const myWorkflow = DBOS.registerWorkflow(myWorkflowFn);
工作流由多个步骤组成。任何执行复杂操作或访问外部服务的函数都必须通过
DBOS.runStep
作为步骤运行:
typescript
import { DBOS } from "@dbos-inc/dbos-sdk";

async function fetchData() {
  return await fetch("https://api.example.com").then(r => r.json());
}

async function myWorkflowFn() {
  const result = await DBOS.runStep(fetchData, { name: "fetchData" });
  return result;
}
const myWorkflow = DBOS.registerWorkflow(myWorkflowFn);

Key Constraints

关键约束

  • Do NOT call, start, or enqueue workflows from within steps
  • Do NOT use threads or uncontrolled concurrency to start workflows - use
    DBOS.startWorkflow
    or queues
  • Workflows MUST be deterministic - non-deterministic operations go in steps
  • Do NOT modify global variables from workflows or steps
  • 请勿在步骤内部调用、启动或入队工作流
  • 请勿使用线程或无控制的并发来启动工作流 - 请使用
    DBOS.startWorkflow
    或队列
  • 工作流必须是确定性的 - 非确定性操作应放在步骤中
  • 请勿在工作流或步骤中修改全局变量

How to Use

使用方法

Read individual rule files for detailed explanations and examples:
references/lifecycle-config.md
references/workflow-determinism.md
references/queue-concurrency.md
请阅读单个规则文件以获取详细说明与示例:
references/lifecycle-config.md
references/workflow-determinism.md
references/queue-concurrency.md

References

参考资料