# nestjs-native-eventstore

> Event Store connector for Nest js

Latest version **3.0.1** (published 2025-02-09) · MIT license · 0 weekly downloads

## Install

```sh
npm install nestjs-native-eventstore
pnpm add nestjs-native-eventstore
yarn add nestjs-native-eventstore
bun add nestjs-native-eventstore
```

## Health

**Score 35/100 (D)** — status: maintenance-mode.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 3.0.1 |
| Published | 2025-02-09 |
| First published | 2022-12-20 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 7 |
| Unpacked size | 262.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Zhorov Dmitry |
| Maintainers | d.z |
| Keywords | nestjs, eventstore, native, GRCP, connector |

## Links

- npm: https://www.npmjs.com/package/nestjs-native-eventstore
- Repository: https://github.com/xagrh/nestjs-native-eventstore
- Homepage: https://github.com/xagrh/nestjs-native-eventstore#readme
- Issues: https://github.com/xagrh/nestjs-native-eventstore/issues
- npm.io page: https://npm.io/package/nestjs-native-eventstore

## Dependencies (7)

- [rxjs](https://npm.io/package/rxjs.md) ^7.8.0
- [nanoid](https://npm.io/package/nanoid.md) ^3.3.4
- [@nestjs/core](https://npm.io/package/@nestjs/core.md) ^11.0.8
- [@nestjs/cqrs](https://npm.io/package/@nestjs/cqrs.md) ^11.0.1
- [@nestjs/common](https://npm.io/package/@nestjs/common.md) ^11.0.8
- [reflect-metadata](https://npm.io/package/reflect-metadata.md) ^0.1.13
- [@eventstore/db-client](https://npm.io/package/@eventstore/db-client.md) ^6.2.1

## Alternatives

- [async-exit-hook](https://npm.io/package/async-exit-hook.md) — 3.7M weekly downloads
- [evnty](https://npm.io/package/evnty.md) — 7.2K weekly downloads
- [eleventy-plugin-asciidoc](https://npm.io/package/eleventy-plugin-asciidoc.md) — 3.5K weekly downloads
- [@jswork/next-get2get](https://npm.io/package/@jswork/next-get2get.md) — 945 weekly downloads
- [@dashersw/axon](https://npm.io/package/@dashersw/axon.md) — 934 weekly downloads

## Recent versions

- 3.0.1 (latest) — 2025-02-09
- 3.0.0 — 2023-06-19
- 2.2.0 — 2023-04-05
- 2.1.1 — 2023-03-16
- 2.1.0 — 2023-03-15
- 2.0.2 — 2023-03-07
- 2.0.1 — 2023-03-03
- 2.0.0 — 2023-03-03
- 1.1.1 — 2022-12-24
- 1.1.0 — 2022-12-23
- 1.0.9 — 2022-12-23
- 1.0.8 — 2022-12-23
- 1.0.7 — 2022-12-23
- 1.0.6 — 2022-12-23
- 1.0.5 — 2022-12-23
- … 5 more at https://npm.io/package/nestjs-native-eventstore/versions

## README

<h1 align="center">
  <a href="https://github.com/EventStore/EventStore-Client-NodeJS" target="blank">NestJS + EventStore-Client-NodeJS</a>
</h1>
  
<p align="center">
  NestJS CQRS module with support EventStore-Client-NodeJS. It requires @nestjs/cqrs.
</p>

<p align="center">
<a href="https://www.npmjs.com/package/nestjs-native-eventstore" target="_blank"><img src="https://img.shields.io/npm/v/nestjs-native-eventstore?style=flat-square" alt="NPM Version"/></a>
<a href="https://img.shields.io/npm/l/nestjs-native-eventstore?style=flat-square" target="_blank"><img src="https://img.shields.io/npm/l/nestjs-native-eventstore?style=flat-square" alt="License"/></a>
<a href="https://img.shields.io/github/languages/code-size/xagrh/nestjs-native-eventstore?style=flat-square" target="_blank"><img src="https://img.shields.io/github/languages/code-size/xagrh/nestjs-native-eventstore?style=flat-square" alt="Code Size"/></a>
<a href="https://img.shields.io/github/languages/top/xagrh/nestjs-native-eventstore?style=flat-square" target="_blank"><img src="https://img.shields.io/github/languages/top/xagrh/nestjs-native-eventstore?style=flat-square" alt="Top Language"/></a>
<a href="https://github.com/xagrh/nestjs-native-eventstore"><img alt="GitHub Actions status" src="https://github.com/actions/setup-node/workflows/Main%20workflow/badge.svg"></a>
</p>




## Description
Injects eventstore connector modules, components, bus and eventstore config into a nestjs application. An example is provided in the examples folder.
It works with current latest version of nestjs along with latest version of EventStore-Client-NodeJS. via GRCP

### Installation
`npm i --save nestjs-native-eventstore`

### Usage

#### Using the EventStoreCoreModule

`EventStoreCoreModule` uses `@nestjs/cqrs` module under the hood. It overrides the default eventbus of `@nestjs/cqrs` and pushes the event to the eventstore rather than the internal eventBus.
Therefore the `eventStoreBus.publish(event, streamName)` method takes two arguments instead of one. The first one is the event itself, and the second one is the stream name. 

Once the event is pushed to the eventStore all the subscriptions listening to that event are pushed that event from the event store. Event handlers can then be triggered to cater for those events.

**app.module.ts**

```typescript
import { EventStoreSubscriptionType } from 'nestjs-native-eventstore';

//linking of events from EventStore to local events
const EventInstantiators = [
  SomeEvent: (_id: any, data: any, loggedInUserId: any) => new SomeEvent(_id, data, loggedInUserId);
];

export const eventStoreBusConfig: EventStoreBusConfig = {
  subscriptions: [
    { // persistanct subscription
      type: EventStoreSubscriptionType.Persistent,
      stream: '$ce-persons',
      persistentSubscriptionName: 'contacts',
    },
    { // Catchup subscription
      type: EventStoreSubscriptionType.CatchUp,
      stream: '$ce-users',
    },
  ],
  events: {
    ...EventInstantiators
  },
};

const eshost = process.env.EVENT_STORE_GRCP_HOST || 'localhost';
const esport = process.env.EVENT_STORE_GRCP_PORT || '2113';
const connectionString = `esdb://admin:changeit@${eshost}:${esport}?tls=false`;
const options:
  | EventStoreConnectionStringOptions
  | EventStoreDnsClusterOptions
  | EventStoreGossipClusterOptions
  | EventStoreSingleNodeOptions = {
    connectionString: connectionString,
  }

@Module({
  imports: [
    ConfigModule.load(path.resolve(__dirname, 'config', '**/!(*.d).{ts,js}')),
    EventStoreCoreModule.forRootAsync(
      {
        useFactory: async (config: ConfigService) => {
          return {
            options: options,
          };
        },
        inject: [ConfigService],
      },
      [eventStoreBusConfig],
    ),
  ],
})
export class AppModule {}

```

**custom.command.handler.ts**

This following is a way to use the command handlers that push to the custom eventBus to the eventstore using aggregate root.

```typescript
import { ICommandHandler, CommandHandler } from '@nestjs/cqrs';
import { SomeCommand } from '../impl/some.command';
import { ObjectAggregate } from '../../models/object.aggregate';
import { EventStorePublisher } from '../../es';
@CommandHandler(SomeCommand)
export class SomeCommandHandler
  implements ICommandHandler<SomeCommand> {
  constructor(private readonly publisher: EventStorePublisher) {}

  async execute(command: SomeCommand) {
    const { object, loggedInUserId } = command;
    const objectAggregate = this.publisher.mergeObjectContext(
      new ObjectAggregate(object._id, object),
    );
    objectAggregate.add(loggedInUserId);
    objectAggregate.commit();
  }
}

```

## Notice
release inspired by [nestjs-eventstore](https://github.com/daypaio/nestjs-eventstore)

## License

  This project is [MIT licensed](LICENSE).

---
_Source: https://npm.io/package/nestjs-native-eventstore · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
