skiqh / couchdb-global-changes

Event emitter for _changes to _all_dbs of a couchdb instance, including newly added dbs
GNU General Public License v2.0
8 stars 3 forks source link

couchdb-global-changes

Does what it says on the tin: emit a db-change event for each document change in each db of a couchdb instance. Can also persist its progress (sequence id-wise) to a file or in the monitored databases.

Installation

npm install couchdb-global-changes

Simple usage

var couchdb_changes = require('couchdb-global-changes')
var feed = couchdb_changes('http://user:pass@127.0.0.1:5984')

feed.on('db-change', function(details) {
    console.log('document changed: %s / %s', details.db_name, details.change.id)
})
feed.on('error', function(err) {
    console.error('something is wrong:', err)
    process.exit()
})

Advanced usage

var couchdb_changes = require('couchdb-global-changes')
var twilio = require('twilio')('ACCOUNT_SID', 'AUTH_TOKEN')

var options = 
    {   couch: 'http://user:pass@127.0.0.1:5984'
    ,   include: '^userdb-'
    ,   persist: '_local'
    ,   namespace: 'sms-daemon'
    ,   include_docs: true
    }

var feed = couchdb_changes(options)

feed.on('db-change', function(details) {

    var doc = details.change.doc
    if(doc.type !== 'sms-message')
        return

    twilio.sendMessage(
            {   to: doc.recipient
            ,   from: doc.sender
            ,   body: doc.message
            }
        ,   function(err, responseData) {
                if (!err)
                    console.log('Successfully sent an sms from %s to %s', doc.sender, doc.recipient)
                else
                    console.error('Could not send sms from %s to %s:\n', doc.sender, doc.recipient, err)
        }
    )
})

feed.on('db-persist', function(details) {
    // after this, we can be sure the update sequence has been saved.
    // when this script is started again, it will pick up just where
    // it left off and not process any document a second time.
    console.log("%s processed all documents in the database %s (up to update-sequence %s)", feed.namespace, details.db_name, details.seq)
})

Details

Options

options.couch

options.include

options.exclude

options.since can be one of

options.include_docs can be one of

options.persist can be

options.persist_debounce

options.namespace (mandatory if options.persist is provided)

Functions

Events

progress | function(details)

catchup | function()

db-catchup | function(details)

db-progress | function(details)

db-removed | function(details)

db-* | function(details)

Tests

The tests are quite incomplete, as this still is a work in progress.

# *nix
export COUCH=http://admin:passw0rd@127.0.0.1:5984
# windows
set COUCH=http://admin:passw0rd@127.0.0.1:5984

# the tests will create a few dbs prefixed with 'cgc-tests-'
# these can be removed afterwards

cd node_modules/couchdb-global-changes
npm install
npm test

Changes

3.0.1

3.0.0

2.0.0