#mongodb-tailor
Easy MongoDB oplog tailing
with optional document lookups after updates.
Usage
const tailor = require('mongodb-tailor');
const tail = tailor.tail({
uri: 'mongodb://localhost/',
db: 'my-database',
collections: 'users,votes',
fullDoc: true
});
tail.on('change', (change) => {
console.log('do something with %j', change);
});
tail.on('connected', () => {
console.log('connected to mongodb');
});
tail.on('error', console.error);
tail.on('end', () => {
console.log('tail ended');
});
setTimeout(() = {
tail.destroy();
}, 5000);
Change events
Each change
event includes a Payload
object containing the following properties:
log
: the oplog object provided by mongodbdoc
: when fullDoc
is true, this value is the document as found in the
database after the update
Examples
updated documents
{ log:
{ ts: Timestamp { _bsontype: 'Timestamp', low_: 10, high_: 1457738334 },
h: Long { _bsontype: 'Long', low_: 329911832, high_: -1131257351 },
v: 2,
op: 'u',
ns: 'test_mongo_tailor.testing',
o2: { _id: 'someid' },
o: { '$set': { n: 3 } }
},
doc: undefined
}
{ log:
{ ts: Timestamp { _bsontype: 'Timestamp', low_: 10, high_: 1457738334 },
h: Long { _bsontype: 'Long', low_: 329911832, high_: -1131257351 },
v: 2,
op: 'u',
ns: 'test_mongo_tailor.testing',
o2: { _id: 'someid' },
o: { '$set': { n: 3 } }
},
doc: { _id: 'someid', n: 3 }
}
inserted documents
{ log:
{ ts: Timestamp { _bsontype: 'Timestamp', low_: 2, high_: 1457737253 },
h: Long { _bsontype: 'Long', low_: -1470957526, high_: 287724487 },
v: 2,
op: 'i',
ns: 'test_mongo_tailor.testing',
o: { _id: 'someid', some: 'value' }
},
doc: undefined
}
deleted documents
{ log:
{ ts: Timestamp { _bsontype: 'Timestamp', low_: 2, high_: 1457737509 },
h: Long { _bsontype: 'Long', low_: 946730008, high_: -1703118155 },
v: 2,
op: 'd',
ns: 'test_mongo_tailor.testing',
b: true,
o: { _id: 'someid' }
},
doc: undefined
}
dropped collections
{ log:
{ ts: Timestamp { _bsontype: 'Timestamp', low_: 3, high_: 1457740592 },
h: Long { _bsontype: 'Long', low_: -1205550711, high_: 462207525 },
v: 2,
op: 'c',
ns: 'test_mongo_tailor.$cmd',
o: { drop: 'some_collection_name' }
},
doc: undefined
}
Development
Running tests
make test
runs testsmake test-cov
runs tests + test coveragemake open-cov
opens test coverage results in your browser
Pebble Technology!
LICENSE
MIT