# @eventstore/db-client 中文文档

规范地址：https://npmcn.com/package/%40eventstore/db-client/

版本：6\.2\.1
npm 元数据更新时间：2026-10-31T12:24:31.373Z
中文译文更新时间：2026-11-14T01:12:46.528Z
软件包许可：Apache-2\.0

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40eventstore/db-client
原文对照：https://npmcn.com/package/%40eventstore/db-client/?lang=original
项目主页：https://developers.eventstore.com/clients/grpc/getting-started?codeLanguage=NodeJS
源码仓库：https://github.com/EventStore/EventStore-Client-NodeJS

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：2.4 MB，270 个文件
- 模块格式：CommonJS
- TypeScript 类型：包内自带
- Node.js 要求：^12\.18\.3 \|\| \>=14
- 运行依赖：7 个
- 首次发布：2020-09-23T15:11:53.856Z
- 最新版本发布：2024-05-10T16:49:19.956Z
- 近 30 天下载量：55898（2026-10-14 至 2026-11-12，npm 官方下载统计）
- 已发布版本数：44（近 12 个月 0 个）

## 中文简介

节点JS EventStoreDB版本20+,并使用gRPC作为通信协议.

## 安装

```sh
npm install @eventstore/db-client
```

也可以使用：`pnpm add @eventstore/db-client`、`yarn add @eventstore/db-client`、`bun add @eventstore/db-client`

## README

# @eventstore/db-客户端

[![npm](https://img.shields.io/npm/v/@eventstore/db-client.svg)](https://www.npmjs.com/package/@eventstore/db-client) [![Github action CI workflow](https://github.com/EventStore/EventStore-Client-NodeJS/workflows/CI/badge.svg?branch=master)](https://github.com/EventStore/EventStore-Client-NodeJS/actions) [![license](https://img.shields.io/npm/l/@eventstore/db-client.svg)](https://github.com/EventStore/EventStore-Client-NodeJS/blob/master/LICENSE)

这是用于事件StoreDB 20+的NodeJS客户端的软件包,并使用gRPC作为通信协议.

## 安装

```shell
# Yarn
$ yarn add @eventstore/db-client

# NPM
$ npm install --save @eventstore/db-client
```

## 事件StoreDB 服务器兼容性

此客户端与版本兼容 `20.6.1` 向上移动.

服务器设置指令可见于该软件的安装部分 [事件存储文档](https://developers.eventstore.com/server/v20.10/docs/installation/)。跟踪最简单配置的多克设置.

## 示例

以下片段展示了一个简单的例子,我们在那里组成连接,然后附加并读取服务器上的事件.

###### Javascript 示例 :

```javascript
const {
  EventStoreDBClient,
  jsonEvent,
  FORWARDS,
  START,
} = require('@eventstore/db-client');

const client = new EventStoreDBClient({
  endpoint: "localhost:2113",
});

async function simpleTest() {
  const streamName = "es_supported_clients";

  const event = jsonEvent({
    type: "grpc-client",
    data: {
      languages: ["typescript", "javascript"],
      runtime: "NodeJS",
    },
  });

  const appendResult = await client.appendToStream(streamName, [event]);

    // read the event
    const events = client.readStream(streamName, {
      fromRevision: START,
      direction: FORWARDS,
      maxCount: 10,
    });

    for await (const { event } of events) {
      console.log('Appended event: ', event);
    }
  } catch (error) {
    console.error('An error occured: ', error);
  } finally {
    await client.dispose();
  }
})();
```

###### 键入符示例 :

```typescript
import {
  EventStoreDBClient,
  jsonEvent,
  FORWARDS,
  START,
  JSONEventType,
} from '@eventstore/db-client';

const client = new EventStoreDBClient({
  endpoint: 'localhost:2113',
});

interface Reservation {
  reservationId: string;
  movieId: string;
  userId: string;
  seatId: string;
}

type SeatReservedEvent = JSONEventType<
  'seat-reserved',
  {
    reservationId: string;
    movieId: string;
    userId: string;
    seatId: string;
  }
>;

type SeatChangedEvent = JSONEventType<
  'seat-changed',
  {
    reservationId: string;
    newSeatId: string;
  }
>;

type ReservationEvents = SeatReservedEvent | SeatChangedEvent;

async function simpleTest(): Promise<void> {
  const streamName = 'booking-abc123';

  const event = jsonEvent<SeatReservedEvent>({
    type: 'seat-reserved',
    data: {
      reservationId: 'abc123',
      movieId: 'tt0368226',
      userId: 'nm0802995',
      seatId: '4b',
    },
  });

  const appendResult = await client.appendToStream<ReservationEvents>(
    streamName,
    event
  );

  // By reading the events in the stream, we can construct the current state of the booking

  interface Reservation {
    reservationId: string;
    movieId: string;
    userId: string;
    seatId: string;
  }

  const events = client.readStream<ReservationEvents>(streamName, {
    fromRevision: START,
    direction: FORWARDS,
    maxCount: 10,
  });

  const reservation: Partial<Reservation> = {};

  for await (const { event } of events) {
    switch (event.type) {
      case 'seat-reserved': {
        reservation.reservationId = event.data.reservationId;
        reservation.movieId = event.data.movieId;
        reservation.seatId = event.data.seatId;
        reservation.userId = event.data.userId;
        break;
      }
      case 'seat-changed': {
        reservation.seatId = event.data.newSeatId;
        break;
      }
      default: {
        const _exhaustiveCheck: never = event;
        break;
      }
    }
  }
}

// Do something with our reservation
console.log(reservation);
```

## 支助

关于支助的信息可在我们的网站上查阅: [事件存储支持](https://eventstore.com/support/)

## 社区

-   [讨论](https://discuss.eventstore.com/)
-   [Discord (Event Store) (在线存储)](https://discord.gg/Phn9pmCw3t)
-   [解析器( ddd- cqrs- es)](https://discord.com/invite/sEZGSHNNbH)

## 捐款

参考我们 [贡献准则](https://github.com/EventStore/EventStore-Client-NodeJS/blob/master/CONTRIBUTING.md)

## 常见问题

### @eventstore/db-client 是什么？

节点JS EventStoreDB版本20\+,并使用gRPC作为通信协议\.

### 如何安装 @eventstore/db-client？

在项目目录运行 npm install @eventstore/db-client，或使用 pnpm add / yarn add / bun add。

### @eventstore/db-client 支持 TypeScript 吗？

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

### @eventstore/db-client 支持 ESM 还是 CommonJS？

以 CommonJS 形式发布，可以直接 require\(\)；在 ES Module 项目中也可以用 import 引入（Node\.js 会把 module\.exports 作为默认导出）。

### @eventstore/db-client 需要什么版本的 Node\.js？

package\.json 的 engines 字段要求 Node\.js ^12\.18\.3 \|\| \>=14。版本不符时 npm 默认只给出警告，开启 engine-strict 后会拒绝安装。

### @eventstore/db-client 可以商用吗？

@eventstore/db-client 使用 Apache-2\.0 许可。宽松许可：允许商业使用、修改和再分发，并附带明确的专利授权；分发时需保留许可与 NOTICE 文件，修改过的文件要注明改动。以上为概括，不构成法律意见，请以许可原文为准。

### @eventstore/db-client 的下载量有多少？

根据 npm 官方下载统计，@eventstore/db-client 在 2026/10/14 – 2026/11/12 这 30 天内被下载 55,898 次，日均约 1,863 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入，适合横向比较热度，不能直接等同于使用人数。

### @eventstore/db-client 还在维护吗？

最新版本 6\.2\.1 发布于 2024/05/11（2 年前）；至今共发布 44 个版本，其中过去 12 个月发布了 0 个；首次发布于 2020/09/23。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

