4.0.5 • Published 2 years ago

mongo-to-rabbit v4.0.5

Weekly downloads
2
License
ISC
Repository
github
Last release
2 years ago

mongo-to-rabbit v4.0.1

npm NPM Snyk Vulnerabilities for npm package npm

An npm package designed for listening to MongoDB and notifying a RabbitMQ server on changes.

MTR logo

Using MenashMQ for connection to RabbitMQ.

Installation

  1. npm i --save mongo-to-rabbit

In order to use the package, you must send two object parameters:

1- Rabbit Data:

contains 2 fields: | # | field | type | info | default | |---|---|---|---|---| | 1 | queues | QueueObjectType | array of destination queues names to send the information to and the middlewareFunc| | 2 | rabbitURI | string | the connection string for the rabbitMQ server | | 3 | healthCheckInterval? | number | the health check interval for rabbit |30000 ms| | 4 | rabbitRetries? | number | amount of retries to connect to rabbit |5|

2- Mongo Data:

contains 2 fields: | # | field | type | info | default | |---|---|---|---|---| | 1 | collectionName | string | the name of the mongo collection you want to listen to | | 2 | connectionString | string | the connection string of the mongo server | | 3 | healthCheckInterval? | number | the health check interval for mongo | 30000 ms|

MTROptions - optional:

contains 3 fields: | # | field | type | info | default | |---|---|---|---|---| | 1 | silent | boolean | if false, logs connection and changes to the console | true | | 2 | prettify | boolean | if true, will filter the result and send it in a specific format | true|

Types:

#fieldtypeinfodefault
1QueueObjectTypename: string, middleware?: MiddlewareFuncType exchange?: ExchangeObjectTypequeue name, middleware parser and exchange-
2MiddlewareFuncType(DataObjectType, collectionName) => (null | string | Object | Buffer | string[] | Object[] | Buffer[] | undefined)A function for manipulating the prettified data received from the listener before sending it to the queue. will only work with a prettify:true.identity function
3ExchangeObjectTypename: string, type: ExchangeType, routingKey?: stringexchange implementation-
4ExchangeTypefanout, topic, direct, headersdifferent types of exchanges, read more at - exchange types-

Example:

import { watchAndNotify } from 'mongo-to-rabbit';

let rabbitData = {
  queues: [{ name: 'MyQueueName' }],
  rabbitURI: 'amqp://localhost',
};

let mongoData = {
  collectionName: 'files',
  connectionString: 'mongodb://localhost:27017/devDB?replicaSet=rs0',
};

watchAndNotify(mongoData, rabbitData);
  • For a more specific example, look at the src/example folder.

The prettified type format:

type DataObjectType = {
  id: string,
  operation: string,
  fullDocument: object,
  updateDescription: {
    updatedFields: object,
    removedFields: string[],
  },
};
  • prettify will only work on operations related to documents in the collection: insert, replace, update, delete

To run the package locally with the example:

  1. run rabbit locally: docker run -it --rm --name rabbitmq -p 5672:5672 -p 15672:15672 rabbitmq:3-management
  2. Initiate the mongo replica: sudo mongod --replSet rs0
  3. npm run connect
  4. npm run receive
  5. mongo
  6. >use devDB
  7. db.files.insertOne({ "name": "it works!"})
  • The connect script connects with two different configurations to mongo and rabbit.
  • The receive script creates two consumers to Rabbit.
  • If succeeded, you should see the result sent at the console.log of the receiver.

running example

logo credit: Vector image by VectorStock / ade01