0.1.3 • Published 5 years ago

tokki v0.1.3

Weekly downloads
2
License
MIT
Repository
github
Last release
5 years ago

Tokki

A RabbitMQ/AMQP Handler

Table of Contents

Installation

# Production
npm install tokki -P
# Development
npm install tokki -D

Quick start

Examples are stored in the examples folder.

Create a connection

const tokki require('tokki');
// or with TypeScript
import tokki from 'tokki'

// create a connection
const amqp = tokki({
  url: 'amqp://user:pwd@localhost:5672'
});

Using workers

The worker service distributes the events to the various workers. Each event only goes to one worker. See the example on RabbitMQ.

// trigger

const trigger = await amqp.worker({ name: 'worker-queue-name' });

// send a payload to queue
trigger.send({ message: 'imported' });

// consumer

const worker = await amqp.worker({ name: 'worker-queue-name' });

// or with error queue
const error = await amqp.worker({ name: 'error-queue-name' });
const worker = await amqp.worker({ name: 'worker-queue-name', errorService: error });

// Optionally, a schema ([JOI](https://github.com/hapijs/joi)) for the payload can also be passed as a parameter.

// define a consumer
worker.setConsumer(async (data) => {
  // data.payload.message = imported

  // ...

  // job is successful
  data.next();

  // or

  // job is failed (but requeue)
  data.defer();

  // or

  // job is failed
  data.discard();
});

Using publishers

The Publish Service distributes the events to all publishers. See the example on RabbitMQ.

const trigger = await amqp.publisher({ name: 'publish-queue-name' });

const publisher1 = await amqp.publisher({ name: 'publish-queue-name' });

// publisher with error queue
const error = await amqp.worker({ name: 'error-publish-queue-name' });
const publisher2 = await amqp.publisher({ name: 'publish-queue-name', errorService: error });

// Optionally, a schema ([JOI](https://github.com/hapijs/joi)) for the payload can also be passed as a parameter.

publisher1.setConsumer(async (data) => {
  // see worker section
  //
  // data.payload.message = imported
});
publisher2.setConsumer(async (data) => {
  // see worker section
  //
  // data.payload.message = imported
});

// send a payload to queue
trigger.send({ message: 'imported' });

Building

Compile the library from TypeScript to JavaScript.

The following command is available:

  • npm run build

    Builds the library

Tests

There are three types of tests:

  • Unit Tests

    These tests have no dependencies outside the tested file (exception: class inheritance). All dependencies are mocked.

    A test coverage of 100% should be achieved.

  • Integration Tests

    These tests have no dependencies outside the project. All dependencies in the package.json file are mocked. Small libraries, e.g. lodash or luxon, don't need to be mocked.

    A test coverage between 50% and 75% should be achieved.

  • Functional Tests

    These tests are performed with all dependencies and take a long time. External services, e.g. MySQL, will/must be provided via docker.

    No dependency should be mocked.

    A test coverage between 50% and 75% should be achieved.

The following commands are available:

CommandTypeDescription
npm run testunitRun all unit tests
npm run test:watchunitWatching mode from unit test
npm run coverageunitCreates a coverage report from unit test
npm run test:integrationintegrationRun all integration tests
npm run test:integration:watchintegrationWatching mode from integration test
npm run coverage:integrationintegrationCreates a coverage report from integration test
npm run test:functionalfunctionalRun all functional tests
npm run test:functional:watchfunctionalWatching mode from functional test
npm run coverage:functionalfunctionalCreates a coverage report from functional test

Prettier and Lint

Ensures that the code is formatted uniformly and that the coding standards are adhered to.

The following commands are available:

  • npm run prettier

    Changes the code formatting as defined in the Prettier setting.

  • npm run lint

    Checks if the lint rules are followed. It calls the prettier command first.