@addapptables/microservice
TypeScript icon, indicating that this package has built-in type declarations

2.0.1 • Public • Published

Addapptables logo

Addapptables microservices is a library for nodejs oriented to microservices with pattern design CQRS

npm version Package License CircleCI Coverage

Addapptables microservices with cqrs

  • Addapptables microservices is a library for nodejs oriented to microservices, this library is made to work with nestjs

  • Example code

Getting Started

To get started, let's install the package through npm:

npm i @addapptables/microservice

If you use rabbitmq install

npm i amqplib

How to use

  • Available adapters:

  • KafkaBusAdapter

  • LocalBusAdapter (rx)

  • MqttBusAdapter

  • NatsBusAdapter

  • RabbitMQBusAdapter

  • RedisBusAdapter

  • Example with rabbitmq

import { MicroserviceModule, ManagerAdapterBus, RabbitMQBusAdapter } from '@addapptables/microservice';
@Module({
  imports: [
    MicroserviceModule.withConfig({
      adapter: ManagerAdapterBus.getInstance(RabbitMQBusAdapter)
      .withConfig({
        exchange: 'search-service',
        host: process.env.BUS_URL
      })
      .build(),
      logger: {
        debug: false
      }
    })
  ],
  controllers: [
    ...
  ],
  providers: [
    ...
  ],
})
export class AppModule {}
  • Create commands
import { Command } from '@addapptables/microservice';

export class ClassCommandModel implements ICommandDto {
    id: string;
}

export class CreateUserCommand extends Command<ClassCommandModel> {
    public readonly action = 'action';
    public readonly context = 'context';

    constructor(public readonly data: ClassCommandModel) { super(data); }
}
  • Create command handlers
import { ICommandHandler, CommandHandler, ICommand } from '@addapptables/cqrs';

@CommandHandler(CreateUserCommand)
export class CommandHandler implements ICommandHandler<ClassCommandModel> {

    handle(event: ClassCommandModel): any {
      console.log(event);
      // call your domain service
    }

}

// ChildModule
@Module({
  imports: [
    ...
  ],
  controllers: [
    ...
  ],
  providers: [
    CommandHandler // very important
  ],
})
export class ChildModule {}
  • Create query
export class ClassQueryModel implements IQueryDto {
    id: string;
}

export class CreateUserQuery extends Query<ClassQueryModel> {
    public readonly action = 'action';
    public readonly context = 'context';

    constructor(public readonly data: ClassQueryModel) { super(data); }
}
  • Create query handlers
@QueryHandler(ClassQueryModel)
export class FindOneUserHandler implements IQueryHandler<ClassQueryModel> {

  constructor(private readonly userService: UserDomainService) { }

  handle(event: ClassQueryModel): any {
    return this.userService.findOneByQuery(event.data);
  }

}

// ChildModule
@Module({
  imports: [
    ...
  ],
  controllers: [
    ...
  ],
  providers: [
    FindOneUserHandler // very important
  ],
})
export class ChildModule {}
  • Create events
export class ClassEventModel implements IEventDto {
    id: string;
}

export class UserCreatedEvent extends Command<ClassEventModel> {
    public readonly action = 'action';
    public readonly context = 'context';

    constructor(public readonly data: ClassEventModel) { super(data); }
}
@EventHandler(UserCreatedEvent)
export class ActionHandler implements IEventHandler<UserCreatedEvent> {

    handle(event: UserCreatedEvent): any {
      console.log(event);
      // call your domain service
    }

}

// ChildModule
@Module({
  imports: [
    ...
  ],
  controllers: [
    ...
  ],
  providers: [
    ActionHandler // very important
  ],
})
export class ChildModule {}

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
2.0.10latest

Version History

VersionDownloads (Last 7 Days)Published
2.0.10
2.0.00
1.1.01
1.0.00
1.0.0-00
0.0.3-01
0.0.2-00
0.0.1-00
0.0.10

Package Sidebar

Install

npm i @addapptables/microservice

Weekly Downloads

2

Version

2.0.1

License

MIT

Unpacked Size

270 kB

Total Files

308

Last publish

Collaborators

  • teogl