Listening to MongoDB live changes using oplog.
- Support start and stop tailing the MongoDB
oplog
at any time. - Support filtering
oplog
events bynamespaces
(database and collections). - Built on top of the native NodeJS MongoDB driver.
- First class
Promise
support which enable the use ofasync
andawait
. - The package has a very small footprint and requires just a few dependencies including
mongodb
,debug
andeventemitter3
. - Uses
eventemitter3
for high performance event emitting. - Strict and readable code enforced with xo
- Unit tested with
mocha
and built withbabel
for backward compatibility with older versions of NodeJS likev4.x
andv5.x
.
2.0.x is a major rewrite taking advantage of es6
and adding support for promises
and async/await
. Callbacks are still supported for backward compatibility.
This version has minimum API changes, but these changes might affect your code, so please take a look at the upgrading guide before installing.
Check the upgrading guide here
Go here for the old 1.x readme
Go here for the old 0.x readme
$ npm install mongo-oplog
import MongoOplog from 'mongo-oplog'
const oplog = MongoOplog('mongodb://127.0.0.1:27017/local', { ns: 'test.posts' })
oplog.tail();
oplog.on('op', data => {
console.log(data);
});
oplog.on('insert', doc => {
console.log(doc);
});
oplog.on('update', doc => {
console.log(doc);
});
oplog.on('delete', doc => {
console.log(doc.o._id);
});
oplog.on('error', error => {
console.log(error);
});
oplog.on('end', () => {
console.log('Stream ended');
});
oplog.stop(() => {
console.log('server stopped');
});
uri
: Valid MongoDB uri or a MongoDB server instance.options
MongoDB connection options.
Start tailing.
This method support both Promise
and callback
.
oplog.tail().then(() => {
console.log('tailing started')
}).catch(err => console.error(err))
// or with async/await
async function tail() {
try {
await oplog.tail()
console.log('tailing started')
} catch (err) {
console.log(err)
}
}
Stop tailing and disconnect from server.
This method support both Promise
and callback
.
oplog.stop().then(() => {
console.log('tailing stopped')
}).catch(err => console.error(err))
// or with async/await
async function stop() {
try {
await oplog.stop()
console.log('tailing stopped')
} catch (err) {
console.log(err)
}
}
Destroy the mongo-oplog
object by stop tailing and disconnecting from server.
This method support both Promise
and callback
.
oplog.destroy.then(() => {
console.log('destroyed')
}).catch(err => console.error(err))
// or with async/await
async function destroy() {
try {
await oplog.destroy()
console.log('destroyed')
} catch (err) {
console.log(err)
}
}
Pause and resume oplog events.
oplog.ignore = true; // to pause
oplog.ignore = false // to resume
Create and return a filter object.
const filter = oplog.filter('*.posts')
filter.on('op', fn)
oplog.tail()
Destroy filter object.
filter.destroy()
Pause and resume filter events.
filter.ignore = true; // to pause
filter.ignore = false // to resume
Events supported by oplog
and filter
;
op
: All bellow operations (oplog/filter).insert
: Document insert (oplog/filter).update
: Document update (oplog/filter).delete
: Document delete (oplog/filter).end
: Cursor stream ended (oplog).error
: Error (oplog).
Configure MongoDB for active oplog:
Start MongoDB with:
$ mongod --replSet test
Start a mongo
shell and configure mongo as follows:
$ mongo
> var config = {_id: "test", members: [{_id: 0, host: "127.0.0.1:27017"}]}
> rs.initiate(config)
Once configuration is initiated then you can run the test:
$ npm install
$ npm run test
(The MIT License)
Copyright (c) 2015 Jonathan Brumley <cayasso@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.