@btravstack/outbox

中文文档
0.58.0发布于 2026/11/08MITTypeScriptESM + CJS零依赖构建来源已验证近 30 天下载 8076

The transactional outbox relay for @btravstack/core: a poll loop that publishes committed facts in outbox order, a per-tenant claim so replicas take turns rather than race for the same rows, and a Prisma 8 store

原始使用文档
文档目录5 节
  1. A worked example
  2. A standalone relay process
  3. Options
  4. What it decides, and what it does not
  5. License

@btravstack/outbox

The transactional outbox relay for @btravstack/core: a poll loop that publishes committed facts in outbox order, a per-tenant claim so replicas take turns rather than race for the same rows, and a Prisma 8 store.

📖 Documentation · API Reference

pnpm add @btravstack/outbox @btravstack/core @btravstack/config @btravstack/di unthrown temporal-polyfill

@prisma/orm-postgres is an optional peer, needed only if you compose the Prisma store from @btravstack/outbox/prisma. Node >=22.12.

Occurrence timestamps are Temporal.Instant, preserving database microseconds through reads and publication. Serialize them with toString(); use epochMilliseconds where a millisecond-only integration requires it.

A worked example

The application writes its outbox row in the same transaction as the row it describes — that stays its own. What it provides the relay is where the table is, and what publishing a row means:

import { OutboxPublisher, OutboxStore, outbox } from "@btravstack/outbox";
import { prismaOutboxStore } from "@btravstack/outbox/prisma";

const store = Provider(OutboxStore)({
  inject: { db: OrderDatabase },
  sync: ({ db }) => prismaOutboxStore(db, { schema: "orders" }),
});

const publisher = Provider(OutboxPublisher)({
  inject: { broker: Broker },
  sync: ({ broker }) => ({
    publish: (message) =>
      broker.send(`${message.kind}.changed`, {
        eventId: message.id,
        tenantId: message.tenantId,
        id: message.subjectId,
        payload: message.payload === null ? null : JSON.parse(message.payload),
      }),
  }),
});

export const Relay = Module("Relay")({
  imports: [OrderPersistenceModule, BrokerModule, outbox()],
  provides: [store, publisher],
  exports: [],
});

A standalone relay process

Use outboxRuntime() in place of outbox() and export OutboxRuntime and HealthChecks from the root passed to runMain. It starts polling at runtime startup, keeps the process alive, and stops new claims during SIGTERM drain. Drain waits for current claims up to the kernel's deadline; scope teardown waits for them before releasing database and publisher resources, bounded by the kernel's stop deadline. The same options and outbox lag check apply.

The standalone example shows the complete root. Keep outbox() when embedding a relay in an HTTP or worker process.

Options

OptionWhereWhat it is
OUTBOX_TENANTSenvironment, or tenantsthe tenants this relay serves, comma-separated — required
OUTBOX_POLL_MSenvironment, or pollMsthe idle sleep between sweeps (default 200)
OUTBOX_MAX_LAG_MSenvironment, or maxLagMsthe oldest pending age the outbox health check tolerates (default 60 s)
OUTBOX_RETAIN_MSenvironment, or retainMshow long a published row is kept before the relay deletes it (default: kept)
OUTBOX_CONCURRENCYenvironment, or concurrencyhow many tenants claim or prune, each on a pooled connection, at once (4)
clockoutbox({ clock })what the poll sleeps on and the lag is measured against (default: systemClock)
schema, tableprismaOutboxStore(db, {…})where the model lives (default public, outboxMessage)
columnsprismaOutboxStore(db, {…})the physical column names, for a model mapped with @map
tenantSettingprismaOutboxStore(db, {…})the setting each tenant's reads are pinned to, for @@rls (app.tenant_id)

The full table — defaults, semantics, the table's PSL and the reasoning — lives on the reference page, which is this list's one detailed home.

What it decides, and what it does not

It decides that delivery is at-least-once, that one relay publishes a tenant at a time while its claiming session lives — so replicas take turns rather than race for a tenant's rows, and a tenant's committed facts go out in outbox order (which is not commit order: the reference page says what that means for a subject); a session lost mid-batch, like a crash between a publish and its mark, re-publishes — that a refused publish stops its tenant's batch and backs that tenant off, and that a tenant falling behind is unhealthy on /healthz. A subscriber deduplicates on the outbox id, which the publisher puts on the wire.

It does not decide what a publish is, which transport it rides or how the payload is encoded — that is your OutboxPublisher — nor the transaction that writes the row, which is your adapter's. There is no exactly-once and no second store; the reasons are in AGENTS.md.

License

MIT

版本与发布记录

@btravstack/outbox 共发布 48 个版本,首次发布于 2026/10/08;过去 12 个月发布了 48 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.58.0latest2026/11/08今天
0.57.02026/11/071 天前
0.56.12026/11/062 天前
0.56.02026/11/062 天前
0.55.12026/11/035 天前
0.55.02026/11/035 天前

关于 @btravstack/outbox 的常见问题

@btravstack/outbox 是什么?

The transactional outbox relay for @btravstack/core: a poll loop that publishes committed facts in outbox order, a per-tenant claim so replicas take turns rather than race for the same rows, and a Prisma 8 store

如何安装 @btravstack/outbox?

在项目目录运行 npm install @btravstack/outbox,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:unthrown(^5.0.0)、@btravstack/di(^0.58.0)、@btravstack/core(^0.58.0)、temporal-polyfill(^1.0.4)、@btravstack/config(^0.58.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@btravstack/outbox 支持 TypeScript 吗?

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

@btravstack/outbox 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@btravstack/outbox 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=22.12。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@btravstack/outbox 可以商用吗?

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

@btravstack/outbox 的下载量有多少?

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

@btravstack/outbox 还在维护吗?

最新版本 0.58.0 发布于 2026/11/08(今天);至今共发布 48 个版本,其中过去 12 个月发布了 48 个;首次发布于 2026/10/08。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。