1.0.5 • Published 4 years ago

@nest-kr/integration v1.0.5

Weekly downloads
38
License
MIT
Repository
github
Last release
4 years ago

Nest-kr/integration

This is for integration in micro services. we use SQS, SNS in AWS for messaging system, We provide IntegrationEventPublisherModule, IntegrationEventSubscriberModule and IntegrationSaverModule. You can use IntegrationEventSubscriberModule with IntegrationEventPublisherModule or IntegrationSaverModule.

You can publish integration event using IntegrationEventPublisherModule and subscribe it using IntegrationSubscriberModule.

When it comes to IntegrationSaverModule, As you can see by the name, it just save a integration event and doesn't publish it. Also If you want to use the module, you have to use Typeorm for saving a integration event and nest-kr-integration-event-publisher( Docker image) for publishing.

Installation

$ npm install --save @nest-kr/integration

How to use

IntegrationEventPublisher

Import IntegrationEventPublisherModule

This module provides IntegrationEventPublisher.

@Module({
    imports: [IntegrationEventPublisherModule],
    controllers: [],
})
export class AppModule {}

init IntegrationEventPublisher

integrationEventPublisher.init({
    accessKeyId: 'aws access key id',
    secretAccessKey: 'aws secret access key',
    region: 'aws region',
    endpoint: 'aws endpoint',
    caseConvention: 'snake or camel',
});

publish IntegrationEvent

@Injectable()
export class UserService {
    constructor(
        private integrationEventFactory: IntegrationEventFactory,
        private integrationEventPublisher: IntegrationEventPublisher,
    ) {}

    async created(user: User) {
        // ...
        const id = 'id';
        const topicArn = 'id';
        const subject = 'id';
        const headers = {};
        const data = {};

        const userCreatedEvent = this.integrationEventFactory.create(id, topicArn, subject, headers, data);
        // ...
        await this.integrationEventPublisher.publish(userCreatedEvent);
    }
}

IntegrationEventSubscriber

Import IntegrationEventSubscriberModule

This module provides IntegrationEventSubscriber.

@Module({
    imports: [IntegrationEventSubscriberModule],
    controllers: [],
})
export class AppModule {}

Create handlers

@Injectable()
export class TestService {
    constructor() {}

    @IntegrationEventHandler('user created')
    async integrationEventHandler(integrationEvent: IntegrationEvent) {
        // signed integration event with handlers, in this case subject is "user created".
    }

    @UnhandledIntegrationEventHandler()
    async unsignedIntegrationEventHandler(integrationEvent: IntegrationEvent) {
        // signed integration event with no handlers.
    }

    @UnsignedIntegrationEventHandler()
    async unsignedIntegrationEventHandler(message: any) {
        // unsigned integration event (all integration events made by this module have signature).
    }
}

init and run IntegrationEventSubscriber

integrationEventSubscriber.use(middleware);
integrationEventSubscriber.init(
    {
        region: configService.config.region,
        accessKeyId: configService.config.accessKeyId,
        secretAccessKey: configService.config.secretAccessKey,
        queueUrl: configService.config.sqsQueueUrl,
    },
    (err: Error) => {
        console.log(err);
    },
);
await integrationEventSubscriber.run();

IsRunning

const isRunning = integrationEventSubscriber.isRunning;

Stop IntegrationEventSubscriber

await integrationEventSubscriber.stop();

Middleware

as you already know you can use middleware for IntegrationEventSubscriber.

function someMiddleware(dto: IntegrationMessageDTO, next:Function) {
    ...
    next(); // or next(new Error());
}

IntegrationEventSaver

You can use IntegrationEventSaver instead of IntegrationEventPublisher We want to prevent publishing integration event when transaction is failed, So, we save integration event in transaction and will publish it later using publisher (nest-kr-integration-event-publisher docker image)

Add IntegrationEventEntity to Typeorm

import { IntegrationEventEntity } from '@nest-kr/integration'
import { createConnection } from 'typeorm';

async function bootstrap(){
    ...
    await createConnection({
        ...
        entities: [IntegrationEventEntity],
    });
    ...
}

Import IntegrationEventSaverModule

This module provide IntegrationEventFactory for creating IntegrationEvent and IntegrationEventSaver for publishing.

@Module({
    imports: [IntegrationEventSaverModule],
    controllers: [],
})
export class AppModule {}

Save IntegrationEvent

you can create integration event with factory and publish it.

@Injectable()
export class UserService {
    constructor(
        private integrationEventFactory: IntegrationEventFactory,
        private readonly integrationEventSaver: IntegrationEventSaver,
    ) {}

    async create User(createUserCommand: CreateUserCommand): Promise<void> {
        ...
        const id = uuid.v1();
        const topicArn = 'topic arn';
        const subject = 'user created';
        const headers = {}
        const data = {
            userId,
        }
        const integrationEvent: IntegrationEvent = this.integrationEventFactory.create(id, topicArn, subject, headers, data);
        await this.integrationEventSaver.save(entityManager, integrationEvent);
    }
}

Integration event publisher (docker image)

IntegrationEventSaver in @nest-kr/integration is just save integration event to database. so you need to run nest-kr-integration-event-publisher for publishing events saved.

// in docker-compose.yml

event-publisher:
    image: loveloper44/nest-kr-integration-event-publisher
    environment:
      - NODE_ENV=development
      - HTTP_PORT=3000
      - MYSQL_HOST=db
      - MYSQL_PORT=3306
      - MYSQL_USER=root
      - MYSQL_PASSWORD=password
      - MYSQL_DATABASE=Database
      - ORM_SYNC=false
      - AWS_ACCESS_KEY_ID=access_key_id
      - AWS_SECRET_ACCESS_KEY=secret_access_key
      - AWS_REGION=ap-northeast-2
      - AWS_SNS_ENDPOINT=endpoint
      - AWS_SNS_TOPIC_ARN=topic_arn
      - THROUGHPUT=30
      - INTERVAL=3000
      - CASE_CONVENTION="snake"
    ports:
      - "4001:3000"
    command: [
      "npm", "run", "start"
    ]
1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago

0.0.31

5 years ago

0.0.30

5 years ago

0.0.29

5 years ago

0.0.28

5 years ago

0.0.27

5 years ago

0.0.26

5 years ago

0.0.25

5 years ago

0.0.24

5 years ago

0.0.23

5 years ago

0.0.22

5 years ago

0.0.21

5 years ago

0.0.20

5 years ago

0.0.19

5 years ago

0.0.18

5 years ago

0.0.17

5 years ago

0.0.16

5 years ago

0.0.15

5 years ago

0.0.14

5 years ago

0.0.13

5 years ago

0.0.12

5 years ago

0.0.11

5 years ago

0.0.10

5 years ago

0.0.9

5 years ago

0.0.8

5 years ago

0.0.7

5 years ago

0.0.6

5 years ago

0.0.5

5 years ago

0.0.4

5 years ago

0.0.3

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago

0.0.0

5 years ago