@addapptables/microservice

addapptables microservice

Usage no npm install needed!

<script type="module">
  import addapptablesMicroservice from 'https://cdn.skypack.dev/@addapptables/microservice';
</script>

README

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 {}