An npm package designed for listening to MongoDB and notifying a RabbitMQ server on changes.
Using MenashMQ for connection to RabbitMQ.
npm i --save mongo-to-rabbit
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 |
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 |
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 |
# | field | type | info | default |
---|---|---|---|---|
1 | QueueObjectType |
name: string, middleware?: MiddlewareFuncType exchange?: ExchangeObjectType |
queue name, middleware parser and exchange | - |
2 | MiddlewareFuncType |
`(DataObjectType, collectionName) => (null | string | Object |
3 | ExchangeObjectType |
name: string, type: ExchangeType, routingKey?: string |
exchange implementation | - |
4 | ExchangeType |
fanout, topic, direct, headers |
different types of exchanges, read more at - exchange types | - |
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.
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
- run rabbit locally:
docker run -it --rm --name rabbitmq -p 5672:5672 -p 15672:15672 rabbitmq:3-management
- Initiate the mongo replica:
sudo mongod --replSet rs0
npm run connect
npm run receive
mongo
>use devDB
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.
logo credit: Vector image by VectorStock / ade01