@nest-kr/cqrs
v0.0.7
Published
```cli npm i @nest-kr/cqrs ```
Downloads
13
Readme
Nest-kr/cqrs
This library is for CQRS
Installation
$ npm install --save @nest-kr/cqrs
How to use
Command
Import CqrsModule This module provides CommandBus and QueryBus
import {CqrsModule} from '@nest-kr/cqrs';
@Module({
imports: [CqrsModule],
controllers: [],
})
export class AppModule {}
Create Command
import { Command } from '@nest-kr/cqrs';
export class CreateUserCommand implements Command {
...
}
Create CommandResult (Optional)
import { CommandResult } from '@nest-kr/cqrs';
export class CreateUserResult implements CommandResult {
...
}
Create CommandHandler
@Injectable()
export class UserCommandService {
...
@CommandHandler(CreateUserCommand)
async create(command: CreateUserCommand): Promise<CreateUserResult> {
return new CreateUserResult();
}
Execute command
@Controller('users')
export class UserController {
constructor(private readonly commandBus: CommandBus) {}
@Post('/')
async create() {
const result: CreateUserResult = await this.commandBus.execute(new CreateUserCommand());
return { message: 'user created', result };
}
...
}
Query
Import CqrsModule This module provides CommandBus and QueryBus
import {CqrsModule} from '@nest-kr/cqrs';
@Module({
imports: [CqrsModule],
controllers: [],
})
export class AppModule {}
Create Query
import { Query } from '@nest-kr/cqrs';
export class GetUserQuery implements Query {
...
}
Create QueryResult
import { QueryResult } from '@nest-kr/cqrs';
export class GetUserQueryResult implements QueryResult {
...
}
Create QueryHandler
@Injectable()
export class UserQueryService {
...
@QueryHandler(GetUserQuery)
async create(query: GetUserQuery): Promise<GetUserQueryResult> {
return new GetUserQueryResult();
}
Execute query
@Controller('users')
export class UserController {
constructor(private readonly queryBus: QueryBus) {}
@Get('/:id')
async get() {
...
const result: GetUserQueryResult = await this.queryBus.execute(new GetUserQuery());
return { message: 'get user', result };
}
...
}