cqrs-proxy
v0.0.9
Published
The Nest.js CQRS bus helping to communicate with remote workers.
Readme
Nest.js CQRS Proxy Bus
The package provides a CQRS Proxy Bus for Nest.js applications. It allows you to create a proxy bus that can be used to send commands to different handlers by @nestjs/microservices workflow and handle them by workers.
Useful Links
Installation
You can install it using yarn or any other package manager:
yarn add cqrs-proxyDon't forget to setup peer dependencies:
yarn add @nestjs/cqrs @nestjs/schematics @nestjs/common @nestjs/core @nestjs/microservices rxjs uuid class-transformerUsage
Importing the Module
To use the CQRS Proxy Bus, you need to import the CqrsProxyClientModule into your Nest.js module as client:
import { CqrsProxyClientModule } from 'cqrs-proxy'
@Module({
imports: [
CqrsProxyClientModule.register({
clientToken: 'AMQP_CLIENT', // The name of the microservice
useSyncMode: false, // Set to true if you want to use synchronous mode for DEBUG
}),
],
})
export class MyModule {}Async mode
You can also use the async mode to register the module:
import { CqrsProxyClientModule } from 'cqrs-proxy'
@Module({
imports: [
CqrsProxyClientModule.registerAsync({
imports: [ConfigModule],
inject: [ConfigService],
useFactory: (config: ConfigService) => ({
clientToken: config.get('clientToken', 'AMQP_CLIENT'),
useSyncMode: config.get('useWorker', true),
}),
}),
],
})
export class MyModule {}Sending Commands
You can send commands using the ProxyBus service. Here's an example of how to send a command:
import { Injectable } from '@nestjs/common'
import { ICommand } from '@nestjs/cqrs'
import { ProxyBus } from 'cqrs-proxy'
class MyCommand implements ICommand {
constructor(public readonly data: string) {}
}
@Injectable()
export class MyService {
constructor(private readonly proxyBus: ProxyBus) {}
public async run(): Promise<void> {
const command = new MyCommand('Hello, World!')
await this.proxyBus.execute(command) // returns metadata with id
}
}Handling Commands
All magic happens in the worker. You need to import CqrsProxyWorkerModule into your root module and create a handler for your command and register it in the worker module using default CqrsModule and command handler matching to MyCommand from example above:
import { Module } from '@nestjs/common'
import { CqrsModule } from '@nestjs/cqrs'
import { CqrsProxyWorkerModule } from 'cqrs-proxy'
import { MyCommandHandler } from './my-command.handler'
import { MyCommand } from './my-command.command'
import { MyService } from './my.service'
@CommandHandler(MyCommand)
export class MyCommandHandler implements ICommandHandler<MyCommand> {
public async execute(command: MyCommand): Promise<void> {
console.log('Handling command:', command.data) // Handling command: Hello, World!
}
}
@Module({
imports: [CqrsModule, CqrsProxyWorkerModule.forRoot()],
providers: [MyService, MyCommandHandler],
})
export class MyWorkerModule {}Contributing
I welcome contributions! Follow these steps to contribute:
- Setup environment
- Fork the repository.
- Clone your forked repository.
- Install dependencies by running
yarn install.
- Making Changes
- Create a new branch for your changes.
- Make your changes and commit them.
- Push your changes to your forked repository.
- Submitting a Pull Request
- Go to the original repository on GitHub and click the "New pull request" button.
- Select your branch and submit the pull request.
- Wait for the review and address any feedback.
You are the star of this project! 🌟
