npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@nest-kr/integration

v1.0.5

Published

```cli npm i @nest-kr/integration ```

Downloads

4

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"
    ]