Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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.
- Host: GitHub
- URL: https://github.com/socketsomeone/nestjs-cqrs-extra
- Owner: SocketSomeone
- License: mit
- Created: 2024-05-30T12:55:56.000Z (8 months ago)
- Default Branch: master
- Last Pushed: 2024-10-22T07:12:51.000Z (3 months ago)
- Last Synced: 2024-10-22T19:08:04.791Z (3 months ago)
- Topics: cqrs, nats, nestjs
- Language: TypeScript
- Homepage: https://npmjs.com/package/nestjs-cqrs-extra
- Size: 1.68 MB
- Stars: 4
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
- Code of conduct: .github/CODE_OF_CONDUCT.md
- Codeowners: .github/CODEOWNERS
- Security: .github/SECURITY.md
Awesome Lists containing this project
README
A module for that provides additional features for the NestJS CQRS module.## 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)