@nest-kr/integration
v1.0.5
Published
```cli npm i @nest-kr/integration ```
Downloads
66
Maintainers
Readme
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"
]