2.0.1 • Published 1 year ago

@addapptables/microservice v2.0.1

Weekly downloads
5
License
MIT
Repository
github
Last release
1 year ago

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

1 year ago

2.0.0

2 years ago

1.1.0

4 years ago

1.0.0

4 years ago

1.0.0-0

4 years ago

0.0.3-0

4 years ago

0.0.2-0

4 years ago

0.0.1-0

4 years ago

0.0.1

4 years ago