Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/socketsomeone/nestjs-cqrs-extra

🚀 Library that provides additional features for the NestJS CQRS module.
https://github.com/socketsomeone/nestjs-cqrs-extra

cqrs nats nestjs

Last synced: 3 months ago
JSON representation

🚀 Library that provides additional features for the NestJS CQRS module.

Awesome Lists containing this project

README

        


Nest Logo


A module for that provides additional features for the NestJS CQRS module.


NPM Version
NPM License
NPM Downloads
Last commit

## About

Nest CQRS Extra is a module that provides additional features for the [NestJS CQRS module](https://docs.nestjs.com/recipes/cqrs). It
provides a way to send commands and queries through a message broker (e.g. NATS, Redis, MQTT) to the appropriate handlers in microservices.
It also provides a way to create sagas that can be used to handle events.

**Features**

- **Typings** - Provides typings for commands, queries, and events. (Request & Response)
- **Message Broker** - Provides a way to send commands and queries to the appropriate handlers in microservices.
- **Saga** - Provides a way to create sagas that can be used to handle events.

## Installation

```bash
$ npm install nestjs-cqrs-extra @nestjs/microservices nats
```

## Usage

### Import the module

```typescript
import { Module } from '@nestjs/common';
import { CqrsModule } from 'nestjs-cqrs-extra';

@Module({
imports: [
CqrsModule.forRoot({
options: { servers: ['nats://localhost:4222'] }
})
]
})
export class AppModule {
}
```

You can change adapter to `redis` or `mqtt` by changing the `adapter` option.

### Create a command

```typescript
import { BaseCommand } from 'nestjs-cqrs-extra';

export class CreateUserCommand extends BaseCommand {
constructor(public readonly name: string) {
super();
}
}
```

### Create a command handler

```typescript
import { CommandHandler } from 'nestjs-cqrs-extra';
import { CreateUserCommand } from './create-user.command';
import { Controller } from '@nestjs/common';

@Controller()
export class UserCommandsController {
@CommandHandler(CreateUserCommand)
public createUser(command: CreateUserCommand): string {
return `User ${command.name} created`;
}
}
```

Also you can use `@QueryHandler` and `@EventHandler` decorators to handle queries and events.

### Send a command

```typescript
import { CommandBus } from 'nestjs-cqrs-extra';
import { Injectable } from '@nestjs/common';

@Injectable()
export class UserService {
constructor(private readonly commandBus: CommandBus) {
}

async createUser(name: string): Promise {
return this.commandBus.execute(new CreateUserCommand(name));
}
}
```

## Stay in touch

* Author - [Alexey Filippov](https://t.me/socketsomeone)
* Twitter - [@SocketSomeone](https://twitter.com/SocketSomeone)

## License

[MIT](https://github.com/SocketSomeone/nestjs-cqrs-extra/blob/master/LICENSE) © [Alexey Filippov](https://github.com/SocketSomeone)