Skip to content

Listening to MongoDB live changes using oplog [DEPRECATED]

Notifications You must be signed in to change notification settings

cayasso/mongo-live

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Mongo Live [DEPRECATED] use mongo-oplog instead

Build Status NPM version

Listening to MongoDB live changes using oplog.

Instalation

$ npm install mongo-live

Usage

var MongoLive = require('mongo-live');
var live = new MongoLive({
  host: '127.0.0.1',
  port: 27017,
  database: 'blog'
});

var posts = live
.query('posts')
.select('title body comments')
.exec(function (error, stream) {

  stream.on('data', function (data) {

    if (error) {
      // handle error
      return;
    }
      
    if ('insert' === data.operation) {
      console.log('inserted', data);
    }

    if ('update' === data.operation) {
      console.log('updated', data);
    }

    if ('remove' === data.operation) {
      console.log('removed', data);
    }

    console.log('======== result ======>', data);

  });

});

You can also listen to events like this:

var posts = live
.query('posts')
.select('title body')
.exec();

posts.on('insert', function(data){
  console.log('inserted', data);
});

posts.on('update', function(data){
  console.log('updated', data);
});

posts.on('remove', function(data){
  console.log('removed', data);
});

posts.on('data', function(data){
  if ('insert' === data.operation) {
    console.log('inserted', data);
  }

  if ('update' === data.operation) {
    console.log('updated', data);
  }

  if ('remove' === data.operation) {
    console.log('removed', data);
  }
});

Then later any find query will be cached for 60 seconds.

You can also enable caching programatically by using the cache method directly from the query instance:

var Person = mongoose.model('Person');

Person.find({ active: true })
.cache(50000) // cache for 50 seconds
.exec(function (err, docs) { /* ... */
  
  if (err) throw error;

  console.log(docs.ttl) // time left for expiration in ms
  console.log(docs.stored); // timestamp this query was cached
  console.log(docs);

});

TODO

Add tests.

Credits

This module use mongo-watcher for watching the mongodb oplog.

Run tests

$ make test

License

(The MIT License)

Copyright (c) 2013 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.

About

Listening to MongoDB live changes using oplog [DEPRECATED]

Resources

Stars

Watchers

Forks

Packages

No packages published