Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Node.js class that watches the CouchDB _changes API and emits events on new changes.
- */
- var EventEmitter = require('events').EventEmitter;
- var cradle = require('cradle');
- CouchWatcher = function(options) {
- this.userid = options.userid;
- this.password = options.password;
- this.dbname = options.dbname;
- this.host = options.host || '127.0.0.1';
- this.port = options.port || 5984;
- this.heartbeat = options.heartbeat || 1000;
- };
- CouchWatcher.prototype = new EventEmitter;
- CouchWatcher.prototype.watch = function(filter, since) {
- var self = this;
- // Create new connection to CouchDB instance.
- var db = new (cradle.Connection)(self.host, self.port,
- {
- auth : {
- username : self.userid,
- password : self.password
- }
- }).database(self.dbname);
- // Watch changes API and emit event with document.
- var options = { include_docs : true, feed : 'continuous', heartbeat : self.heartbeat};
- if(filter) {
- options.filter = filter;
- }
- if(since) {
- options.since = since;
- }
- db.changes(options).on('response', function(res) {
- res.on('data', function(change) {
- self.emit('doc', change.doc);
- });
- res.on('end', function() {
- self.emit('end');
- });
- });
- };
- exports.CouchWatcher = CouchWatcher;
Add Comment
Please, Sign In to add comment