- cqrs is a library for nodejs oriented to microservices, this library is made to work with [nestjs](https://docs.nestjs.com/)
npm install @addapptables/cqrs
npm i @addapptables/cqrs --S
`
How to use
- Create commands
`typescript
import { Command } from '@addapptables/cqrs';
export class ClassCommandModel implements ICommandDto, IEventDto {
id: string;
}
export class CreateUserCommand extends Command {
public readonly action = 'action';
public readonly context = 'context';
constructor(public readonly data: ClassCommandModel) { super(data); }
}
`
- Create command handlers
`typescript
import { ICommandHandler, CommandHandler, ICommand } from '@addapptables/cqrs';
@CommandHandler({ context: 'context', action: 'action' })
export class CommandHandler implements ICommandHandler {
handle = async (command: ICommand): Promise => {
// Save in event store
}
}
`
- Create event handlers
`typescript
import { IEventHandler, IEvent, EventHandler } from '@addapptables/cqrs';
@EventHandler({ context: 'context', action: 'action' })
export class ActionHandler implements IEventHandler {
handle = async ({ data }: IEvent): Promise => {
try {
console.log(data);
} catch (error) {
console.log('error', error);
}
}
}
``