oplog-emitter

1.3.1 • Public • Published

oplog-emitter NPM version Build Status Dependency Status Greenkeeper badge

Process your mongodb oplog as events

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

Package Sidebar

Install

npm i oplog-emitter

Weekly Downloads

2

Version

1.3.1

License

Apache-2.0

Unpacked Size

16.7 kB

Total Files

4

Last publish

Collaborators

  • wdullaer