@godspeedai/cognate-projections

中文文档
0.10.0发布于 2026/11/16Apache-2.0ES Module构建来源已验证近 30 天下载 4970

Checkpointed, idempotent, rebuildable projections over the durable event log (spec §11).

原始使用文档
文档目录6 节
  1. Installation
  2. Quick start
  3. How the runner behaves
  4. Limitations
  5. Related packages
  6. License

@godspeedai/cognate-projections

Checkpointed, idempotent, rebuildable projections over the Cognate durable event log. A projection is a deterministic derivation from events; ProjectionRunner keeps its derived state and its checkpoint in sync, batch by batch, and never skips a failing event.

When to use this package: you want a read model or index derived from committed events — counts, listings, per-tenant views — that survives restarts, can be rebuilt from scratch, and stays consistent under concurrent writers. The @godspeedai/cognate-runtime-bun runtime uses this package to keep registered projections current; you can also run a ProjectionRunner directly over any @godspeedai/cognate-events store.

Installation

bun add @godspeedai/cognate-projections

Requires Bun >= 1.4.0. Needs a store implementing the @godspeedai/cognate-events DurableStore contract — for example @godspeedai/cognate-store-sqlite or @godspeedai/cognate-store-memory.

Quick start

import { ProjectionRunner } from "@godspeedai/cognate-projections";
import type { Projection } from "@godspeedai/cognate-projections";

const ledger: Projection = {
  name: "ledger",
  version: "1", // bump to force a rebuild rather than reinterpret old state
  apply(event, state) {
    const count = (state.get(`count:${event.eventType}`) as number | undefined) ?? 0;
    const total = (state.get("total") as number | undefined) ?? 0;
    const amount = (event.payload as { amount?: number }).amount ?? 0;
    return [
      { key: `count:${event.eventType}`, value: count + 1 },
      { key: "total", value: total + amount },
    ];
  },
};

const runner = new ProjectionRunner(store, ledger);
const result = await runner.catchUp();
// { status: "ok", position: <last applied store position>, applied: 3 }

const total = await store.readProjection("ledger", "total");

apply returns the keys it wants to write (value: null deletes a key) and may read state written earlier in the same batch through state.get.

How the runner behaves

  • Checkpointed and idempotent. Each batch's writes and its checkpoint commit atomically through one commitProjection call, guarded by the previous position. Running catchUp() twice applies each event exactly once.
  • Failures stop the world — correctly. On an apply error the runner commits the successful prefix, records the error at the failing event, and returns { status: "error", ... }. catchUp() keeps returning that stored error until you clear it with retry(), which reattempts from the checkpoint. The failing event is never skipped.
  • Version changes require rebuilds. A stored checkpoint whose version differs from the projection's own yields { status: "rebuild_required" }; rebuild() deletes the derived state and replays from position 0.
  • Live mode. start() follows committed events as they happen; stop() unsubscribes after the in-flight run finishes. All runs — live, catchUp, retry, rebuild — are serialized so they never overlap. waitFor(position) resolves once the checkpoint reaches a given store position.
  • Concurrent runners are safe. Two runners racing on one projection resolve through the store's checkpoint conflict: the loser re-reads the checkpoint and continues from it without double-applying.

Limitations

  • Projections are derived state: nothing here is authoritative, and rebuild() will happily delete and recompute everything. Never store the only copy of data in a projection.
  • apply must be deterministic given the event and the state — no clock, randomness, or network. Record nondeterministic inputs as events first.

License

Apache-2.0

版本与发布记录

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

版本发布日期距今
0.10.0latest2026/11/16今天
0.9.22026/11/152 天前
0.9.12026/11/143 天前
0.9.02026/11/0610 天前
0.8.02026/10/2720 天前
0.7.12026/10/2523 天前

关于 @godspeedai/cognate-projections 的常见问题

@godspeedai/cognate-projections 是什么?

Checkpointed, idempotent, rebuildable projections over the durable event log (spec §11).

如何安装 @godspeedai/cognate-projections?

在项目目录运行 npm install @godspeedai/cognate-projections,或使用 pnpm add / yarn add / bun add。

@godspeedai/cognate-projections 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/godspeedai__cognate-projections 是否存在。

@godspeedai/cognate-projections 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@godspeedai/cognate-projections 可以商用吗?

@godspeedai/cognate-projections 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@godspeedai/cognate-projections 的下载量有多少?

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

@godspeedai/cognate-projections 还在维护吗?

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