Skip to content
This repository has been archived by the owner on May 12, 2019. It is now read-only.

wdullaer/oplog-emitter

Repository files navigation

oplog-emitter NPM version Build Status Dependency Status Greenkeeper badge

Process your mongodb oplog as events

The mongodb driver has added this feature in version 3. There's no reason any more to use this library, so it has therefore been archived.

The module connects to a mongodb oplog and emits all of the transactions as a nodejs event emitter. This provides a much nicer high level API to work with. All events are emitted as an op event. Insert, update and delete events are available as insert, update and delete respectively.

The module can authenticate in the admin database using username/password and allows you to pass in a custom last processed timestamp. If this is not provided, it will only emit events that occurred after the module connected to mongodb.

Installation

$ npm install oplog-emitter

Usage

let OplogEmitter = require('oplog-emitter');

let emitter = new OplogEmitter('mongodb://myuser:password@localhost:27000/local?authSource=admin')
emitter.on('insert', (op) => console.log(`${op} was inserted`))

TODO

  • Allow more mongodb authentication mechanisms

License

Apache-2.0 © Wouter Dullaert

API

OplogOptions

An object containing the configuration options of the OplogEmitter

Properties

  • oplogURL string A mongodb connection string to the oplog
  • getLastTimestamp ?TimestampGenerator A function returning a mongodb Timestamp with the starting offset in the oplog
  • database ?string Filter oplog events by this database name
  • collection ?string Filter oplog events by this collection name
  • credentials ?Credentials An object of mongodb credentials
  • retries ?number The amount of times to retry connecting to the database (with exponential-backoff)
  • log ?function A function which this library can use to log
  • timestampTimeout ?number The number of milliseconds we should wait for getLastTimestamp to return a result

Credentials

An object with mongodb credentials of a user in the admin database

Properties

TimestampGenerator

A function returning a promise to a mongodb Timestamp

Returns Promise<Timestamp> A promise resolving to a mongodb timestamp

OplogEmitter

Extends EventEmitter

An event emitter that fires for every oplog entry

Parameters

Examples

let OplogEmitter = require('oplog-emitter');
let emitter = new OplogEmitter('mongodb://localhost:27017/local?authSource=admin');

emitter.on('op', () => console.log('A transaction was added to the oplog'));
emitter.on('insert', () => console.log('An insert was done in the database'));
emitter.on('delete', () => console.log('A document was deleted in the database'));
emitter.on('update', () => console.log('A document was updated in the database'));
emitter.on('error', () => console.log('Something went wrong when reading'));
  • Throws TypeError when constructor arguments are not valid

About

Process your mongodb oplog as events

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 3

  •  
  •  
  •