Giter VIP home page Giter VIP logo

mongodb-tailor's Introduction

#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/', // a mongodb URI
  db: 'my-database',           // database name
  collections: 'users,votes',  // comma seperated list of collections to tail, use "*" to represent watching all collections
  fullDoc: true                // (default: false) if true, include the full document which was changed
});

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(); // closes the stream
}, 5000);

Change events

Each change event includes a Payload object containing the following properties:

  • log: the oplog object provided by mongodb
  • doc: when fullDoc is true, this value is the document as found in the database after the update

Examples

updated documents

// fullDoc: false
{ 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
}

// fullDoc: true
{ 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 } // the entire object in the database
}

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 tests
  • make test-cov runs tests + test coverage
  • make open-cov opens test coverage results in your browser

Sponsored by

Pebble Technology!

LICENSE

MIT

mongodb-tailor's People

Contributors

aheckmann avatar aheckmann-pebble avatar

Watchers

[0] avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.