Version: 0.1.0.
Updated: Feb 11, 2018. 12:31 pm UTC-6
Status: completed
Listening to MongoDB live changes using oplog in a simple way.
- The package has a very small footprint and requires just a few dependencies including
mongodb
,debug
andeventemitter3
. - Built on top of the native NodeJS MongoDB driver.
- Support start and stop tailing the MongoDB
oplog
at any time. - Support filtering
oplog
events bynamespaces
(database and collections). - First class
Promise
support which enable the use ofasync
andawait
. - 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 likev6.x
andv7.x
.
$ npm install https://github.com/jasancheg/x-mongo-db-live-changes.git
You need to configure your MongoDB instance (local instance) to have access to the oplog, here are some quick steps on how to do so:
-
Shutdown your existing mongo instance if its running.
-
Restart the instance. Use the
--replSet
option to specify the name of the replica set.
$ sudo mongod --replSet rs0
- Connect to the mongo instance by executing
mongo
in your terminal:
$ mongo
- In the mongo shell run
rs.initiate()
to initiate the new replica set:
> rs.initiate()
Once it is initiated then you are ready to start using x-db-oplog
.
And here is the official MongoDB documentation if you need additional help on MongoDB replica set.
const MongoOplog = require('x-db-oplog');
const oplog = MongoOplog('mongodb://127.0.0.1:27017/local', { ns: 'test.news' });
const { log } = console;
oplog.tail();
oplog.on('op', data => log(data));
oplog.on('insert', doc => log(doc));
oplog.on('update', doc => log(doc));
oplog.on('delete', doc => log(doc.o._id));
oplog.on('error', error => log(error));
oplog.on('end', () => log('Stream ended'));
oplog.stop(() => 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
.
const { log, error } = console
oplog.tail()
.then(() => log('tailing started'))
.catch(err => error(err))
// or with async/await
async function tail() {
try {
await oplog.tail()
log('tailing started')
} catch (err) {
log(err)
}
}
Stop tailing and disconnect from server.
This method support both Promise
and callback
.
const { log, error } = console
oplog.stop()
.then(() => log('tailing stopped'))
.catch(err => error(err))
// or with async/await
async function stop() {
try {
await oplog.stop()
log('tailing stopped')
} catch (err) {
log(err)
}
}
Destroy the x-db-oplog
object by stop tailing and disconnecting from server.
This method support both Promise
and callback
.
const { log, error } = console
oplog.destroy
.then(() => log('destroyed'))
.catch(err => error(err))
// or with async/await
async function destroy() {
try {
await oplog.destroy()
log('destroyed')
} catch (err) {
error(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('*.news')
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, once this is done then you can run the test:
$ npm install
$ npm run test