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.
npm install couchdb-global-changes
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()
})
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)
})
options
is a string, it is interpreted as the url to a couchdb instanceoptions.couch
http://127.0.0.1:5984
options.include
options.exclude
options.since
can be one of'now'
or an integer sequence id: all feeds will use this same value{ db1: 'now', db2: 1234 }
: provide a value for each db's name individually (good for managing resuming operations)0
for every feedoptions.include_docs
can be one oftrue
or false
: all feeds will use this same value{ db1: true, db2: false }
: provide a value for each db's name individuallyfalse
for every feedoptions.persist
can be'_local'
, which will persist the db's sequence ids to {db}/_local/couchdb-global-changes:{namespace}
options.persist_debounce
options.namespace
(mandatory if options.persist
is provided)feed.total_dbs()
returns the number of dbs that are being followedfeed.caught_up_dbs()
returns the number of dbs have emitted the catchup
eventprogress
| function(details)
details.progress
goes from 0 to 1details.caught_up_dbs
the number of caught up dbsdetails.total_dbs
the number of dbs being followedcatchup
| function()
db-catchup
| function(details)
details.db_name
has caught up to sequence id details.seq
(which is an alias to details.catchup
)db-progress
| function(details)
details.progress
goes from 0 to 1details.db_name
the name of the dbdb-removed
| function(details)
details.db_name
has been removed from the feed, probably due to deletion of the dbdb-*
| function(details)
*
is one of start
, confirm_request
, confirm
, change
, catchup
, wait
, timeout
, retry
, stop
, error
db-
prefixdetails.db_name
details.<eventname>
db-change
event gets passed { db_name: 'foo', change: <change-obj> }
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
options.since
caught_up_dbs
and total_dbs
info from the db-catchup
eventprogress
event's parameter to an object with progress
and caught_up_dbs
and total_dbs
options.filter
to options.include
(and add accordingly options.exclude
)"_local"
as an option to options.persist
options.namespace
(needed for namespacing in the persistence layer)db-persist
which gets emitted after the current sequence id has been persisted to disk/db.options.persist_debounce
caught_up_dbs
and total_dbs
info to the db-catchup
event's details