@cardano-sdk/projection

中文文档
0.13.21发布于 2026/09/25Apache-2.0TypeScriptESM + CJS近 30 天下载 2.7万

Chain Sync event projection

原始使用文档
文档目录9 节
  1. Prerequisites
  2. Types
  3. ProjectionEvent<ExtraProps>
  4. StabilityWindowBuffer
  5. ObservableCardanoNode
  6. Utilities
  7. Bootstrap.fromCardanoNode
  8. Mappers
  9. Other Utils

Cardano JS SDK | projection

This package is a library of generic projection types and utilities.

[!TIP] Guide to Projections and Read Models in Event-Driven Architecture

If you're interested in projecting into PostgreSQL database, see projection-typeorm package.

If you're interested in projector application as run by Lace, see setup and README in cardano-services package.

Prerequisites

In order to understand how cardano-js-sdk projections work, you'll first need basic understanding of:

Types

ProjectionEvent<ExtraProps>

All projections start by obtaining a source/producer1, which is an Observable<ProjectionEvent<{}>>. These events are very similar to Chain Sync events, but there are some important differences:

  1. Block format is compatible with types from @cardano-sdk/core package.
  2. Events include some additional properties: {eraSumaries, genesisParameters, epochNo, crossEpochBoundary}.
  3. RollBackward events include block data (instead of just specifying the rollback point), and are emitted once for each rolled back block.

ExtraProps (Generic Parameter)

Source observable can be piped through a series of RxJS operators, which may add some properties to the event. In a nutshell, ProjectionEvent<T> = ProjectionEvent<{}> & T. For more detail see Mappers.

StabilityWindowBuffer

Since ProjectionEvent{eventType=RollBackward} includes block data, we must store some number (at least k, which is the "security parameter") of latest blocks so that if a fork/rollback happens, we can call StabilityWindowBuffer.getBlock(hash) in order to build a valid ProjectionEvent.

ObservableCardanoNode

This is our interface to Chain Sync and Local State Query protocols of Cardano node. The only implementation available in cardano-js-sdk is OgmiosObservableCardanoNode.

Utilities

Bootstrap.fromCardanoNode

Creates a projection source (Observable<ProjectionEvent>) from:

---
title: Bootstrap Algorithm
---
flowchart LR
    subscribe[Subscribe] --> find-intersect{Find node\nintersection\nwith local tip}
    find-intersect --> |Found| start[Start ChainSync\nfrom intersection]
    start --> get-event{Get event\nfrom node,\ncheck type}
    get-event --> |RollBackward| get-block[Get block from StabilityWindowBuffer]
    get-event --> |RollForward| hydrate
    start --> hydrate[Hydrate event\nwith additional\npropertes]
    hydrate --> emit[Emit\nProjectionEvent]
    find-intersect --> |Not Found| get-block[Get block from\nStabilityWindowBuffer]
    get-block --> rollback[Create\nRollBackward\nevent]
    rollback --> hydrate
    rollback --> wait[Wait for\nlocal tip change]
    wait --> is-rollback-point{Is rollback point?}
    is-rollback-point --> |No| get-block
    is-rollback-point --> |Yes| started-sync{Already started\nChain Sync?}
    started-sync --> |Yes| get-event
    started-sync --> |No| find-intersect

Mappers

Projections are typically

  1. Interpreting the block (e.g. extracting some relevant properties from all transactions). Mappers is a namespace that contains a collection of RxJS operators that process the ProjectionEvent (block) and emit a new event object that has some additional properties.
  2. Doing something with it (e.g. store into the database).

Here's an example:

// source$: Observable<ProjectionEvent<{}>>
source$.pipe(
  // policyId is a parameter that should be provided
  // by handle issuer, e.g. ADA Handle
  Mappers.withHandles({ policyId }),
  // type WithHandles = { handles: HandleOwnership; }
  // Event type can also be inferred (no need for explicit type declaration)
  tap((event: ProjectionEvent<WithHandles>) => {
    console.log('Handles found by withHandles operator', event.handles);
  })
);

Other Utils

InMemoryStabilityWindowBuffer

If your application doesn't need persistence, you can use in-memory implementation of StabilityWindowBuffer, which can also be used as local tip tracker for Bootstrap (provides Observable<TipOrOrigin>).


1Currently our projection source observable is not a pure 'Producer', as events have a requestNext method that is used to control the source. This is subject to change in the future.

版本与发布记录

@cardano-sdk/projection 共发布 173 个版本,首次发布于 2022/11/23;过去 12 个月发布了 6 个版本。

版本发布日期距今
0.13.21latest2026/09/251 个月前
0.13.202026/07/084 个月前
0.13.192026/07/044 个月前
0.13.172026/06/204 个月前
0.13.152026/04/136 个月前
0.13.142026/01/319 个月前

其他发布通道:test → 0.6.1-test.0,nightly → 0.6.1-nightly.1,patch → 0.8.3-patch.0。

关于 @cardano-sdk/projection 的常见问题

@cardano-sdk/projection 是什么?

Chain Sync event projection

如何安装 @cardano-sdk/projection?

在项目目录运行 npm install @cardano-sdk/projection,或使用 pnpm add / yarn add / bun add。

@cardano-sdk/projection 支持 TypeScript 吗?

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

@cardano-sdk/projection 支持 ESM 还是 CommonJS?

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

@cardano-sdk/projection 需要什么版本的 Node.js?

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

@cardano-sdk/projection 可以商用吗?

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

@cardano-sdk/projection 的下载量有多少?

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

@cardano-sdk/projection 还在维护吗?

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