@eventstore/db-client

中文文档
6.2.1发布于 2024/05/11Apache-2.0TypeScriptCommonJS近 30 天下载 5.6万

NodeJS EventStoreDB version 20+ and uses gRPC as the communication protocol.

原始使用文档
文档目录6 节
  1. Installation
  2. EventStoreDB Server Compatibility
  3. Example
  4. Support
  5. Communities
  6. Contributing

@eventstore/db-client

npm Github action CI workflow license

This is the package for the NodeJS client for EventStoreDB 20+ and uses gRPC as the communication protocol.

Installation

# Yarn
$ yarn add @eventstore/db-client

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

EventStoreDB Server Compatibility

This client is compatible with version 20.6.1 upwards.

Server setup instructions can be found under the installation section of the Event Store Docs. Follow the Docker setup for the simplest configuration.

Example

The following snippet showcases a simple example where we form a connection, then append and read events from the server.

Javascript example:
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 example:
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);

Support

Information on support can be found on our website: Event Store Support

Communities

Contributing

Refer to our Contribution Guidelines

版本与发布记录

@eventstore/db-client 共发布 44 个版本,首次发布于 2020/09/23;过去 12 个月发布了 0 个版本。

版本发布日期距今
6.2.1latest2024/05/112 年前
6.2.02024/05/102 年前
6.1.02023/12/192 年前
6.0.02023/08/253 年前
5.0.12023/02/163 年前
5.0.02022/11/044 年前

关于 @eventstore/db-client 的常见问题

@eventstore/db-client 是什么?

NodeJS EventStoreDB version 20+ and uses gRPC as the communication protocol.

如何安装 @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 和提交记录判断。