@effect-server-utils/unit-of-work

中文文档
0.1.0-beta.10发布于 2026/10/30MITTypeScriptESM + CJS零依赖构建来源已验证近 30 天下载 4316

The unit-of-work boundary for Effect: a re-entrant atomicity port over a host's transaction primitive, and the after-commit delivery it gives @effect-server-utils/cqrs's event bus

原始使用文档
文档目录8 节
  1. Why it is its own package
  2. What you get
  3. Supplying a driver
  4. Declaring the boundary
  5. After-commit events
  6. Testing
  7. Documentation
  8. License

@effect-server-utils/unit-of-work

The unit-of-work boundary for Effect: a re-entrant atomicity port over your own transaction primitive, and the after-commit delivery it gives @effect-server-utils/cqrs's event bus.

pnpm add @effect-server-utils/unit-of-work @effect-server-utils/cqrs effect

Both effect and @effect-server-utils/cqrs are peer dependencies.

Why it is its own package

The CQRS package has no opinion about transactions. Its event bus knows that some subscriptions want to run after whatever produced the event has finished, and it knows nothing about what "finished" means — it hands those events to a DeferralSink if one is in context, and runs them at the end of the dispatch if none is.

This package is that sink, and the boundary that gives it a commit to wait for. Install it when your host has a datastore whose atomicity you want a use case to be able to declare; leave it out and the bus still works, with subscribeAfterCommit degenerating to "after every immediate handler". Handlers do not change between the two.

What you get

  • withUnitOfWork — the combinator a write-side use case applies once, at the end of its pipe. Every repository write inside commits together or is discarded together.
  • UnitOfWork — the port behind it, so a use case can be unit-tested against a pass-through implementation without ever naming a datastore.
  • TransactionDriver — the one thing you supply. Re-entrancy, deferral, drain ordering and failure isolation are this package's; the SQL is yours.
  • PersistenceUnavailable — the transient-store failure a repository port names, so a module's domain/ can describe it without importing infrastructure.
  • @effect-server-utils/unit-of-work/testing — PassThroughUnitOfWork and in-memory drivers, for tests whose repositories are fakes.

Supplying a driver

import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";
import {
  PersistenceUnavailable,
  TransactionDriver,
  TransactionFailed,
} from "@effect-server-utils/unit-of-work";

// An adapter is responsible for making its own scope handle ambient to the effect
// it wraps, so a repository inside picks it up. Nothing about that handle appears
// in the port — if it did, every consumer's read path would have to name it.
export const TransactionDriverLive: Layer.Layer<TransactionDriver, never, Database> = Layer.effect(
  TransactionDriver,
  Effect.gen(function* () {
    const db = yield* Database;
    return TransactionDriver.of({
      withTransaction: (effect) => db.transaction(effect),
      withSavepoint: (effect) => db.savepoint(effect),
      isActive: db.hasOpenTransaction,
    });
  }),
);

Declaring the boundary

import { withUnitOfWork } from "@effect-server-utils/unit-of-work";

// Declared once and visibly, rather than buried in an inner block that dispatched
// event handlers would silently join.
const placeOrder = (input: Input) =>
  Effect.gen(function* () {
    const order = yield* orders.insert(input);
    yield* eventBus.dispatch([OrderPlaced.make({ orderId: order.id })]);
    return order.id;
  }).pipe(withUnitOfWork);

run is re-entrant: a bare call opens a scope, a call already inside one takes a savepoint instead of reaching for a second connection. So a command dispatched from inside a caller's transaction joins it, and a nested failure the caller catches discards only the nested scope.

withUnitOfWork demotes TransactionFailed to a defect — no use case can act on a rejected commit — and leaves PersistenceUnavailable standing, which is the one a transport boundary turns into a 503.

After-commit events

import { makeEventBus } from "@effect-server-utils/cqrs";
import { makeUnitOfWork } from "@effect-server-utils/unit-of-work";

// The boundary and the sink are installed together, because they are one decision:
// "after commit" has no meaning until something owns a commit.
const runtime = Layer.mergeAll(makeEventBus(), makeUnitOfWork()).pipe(
  Layer.provide(TransactionDriverLive),
);

With this wired, bus.subscribeAfterCommit(...) handlers run once the outermost unit of work commits, each in a fresh unit of work so its failure cannot undo the work it is reacting to, and the drain is uninterruptible so a client hanging up does not discard reactions to work that is already durable. A nested scope that rolls back discards what it deferred; the enclosing one still commits and still drains its own.

Dispatching with no unit of work open is a tagged defect (EventDispatchedOutsideUnitOfWork) rather than a quietly different delivery — once you have installed a boundary, a dispatch that forgot one is a bug.

Testing

import { PassThroughUnitOfWork } from "@effect-server-utils/unit-of-work/testing";

The real boundary over an in-memory driver: real re-entrancy, real after-commit ordering, real discard-on-rollback, no datastore. Prefer it to a hand-rolled pass-through, which tends to get the rollback case wrong.

Documentation

Full documentation: https://dataquail.github.io/effect-server-utils

License

MIT

版本与发布记录

@effect-server-utils/unit-of-work 共发布 9 个版本,首次发布于 2026/08/27;过去 12 个月发布了 9 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.1.0-beta.10latest2026/10/3014 天前
0.1.0-beta.92026/10/111 个月前
0.1.0-beta.82026/10/111 个月前
0.1.0-beta.72026/09/221 个月前
0.1.0-beta.62026/09/122 个月前
0.1.0-beta.52026/09/122 个月前

关于 @effect-server-utils/unit-of-work 的常见问题

@effect-server-utils/unit-of-work 是什么?

The unit-of-work boundary for Effect: a re-entrant atomicity port over a host's transaction primitive, and the after-commit delivery it gives @effect-server-utils/cqrs's event bus

如何安装 @effect-server-utils/unit-of-work?

在项目目录运行 npm install @effect-server-utils/unit-of-work,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:effect(^4.0.2)、@effect-server-utils/cqrs(^0.1.0-beta.10)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@effect-server-utils/unit-of-work 支持 TypeScript 吗?

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

@effect-server-utils/unit-of-work 支持 ESM 还是 CommonJS?

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

@effect-server-utils/unit-of-work 可以商用吗?

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

@effect-server-utils/unit-of-work 的下载量有多少?

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

@effect-server-utils/unit-of-work 还在维护吗?

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