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)
})
- if
options
is a string, it is interpreted as the url to a couchdb instance
- the root url of the couchdb instance
- defaults to
http://127.0.0.1:5984
- if provided, will be evaluated as a regular expression
- each db's name must then match that regular expression
- if provided, will be evaluated as a regular expression
- each db's name must then not match that regular expression
'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)- numbers can be negative (e.g. -n) to replay the last n changes
- defaults to
0
for every feed
true
orfalse
: all feeds will use this same value{ db1: true, db2: false }
: provide a value for each db's name individually- defaults to
false
for every feed
- a string containg the path to a JSON file, where the db's sequences can be stored and later retrieved
'_local'
, which will persist the db's sequence ids to{db}/_local/couchdb-global-changes:{namespace}
- nothing else right now
- to avoid hammering the filesystem or the db with write queries, the actual call to persist sequence ids is debounced by this amount of milliseconds
- defaults to 200
- this allows the persistence layer to save sequence ids individually for every script that invokes the couchdb-global-changes module.
feed.total_dbs()
returns the number of dbs that are being followedfeed.caught_up_dbs()
returns the number of dbs have emitted thecatchup
event
- calculated global progress during the catchup-phase.
details.progress
goes from 0 to 1details.caught_up_dbs
the number of caught up dbsdetails.total_dbs
the number of dbs being followed
- all followed dbs have caught up
- the db
details.db_name
has caught up to sequence iddetails.seq
(which is an alias todetails.catchup
)
- calculated progress of a single db during the catchup-phase.
details.progress
goes from 0 to 1details.db_name
the name of the db
- the db
details.db_name
has been removed from the feed, probably due to deletion of the db
- where
*
is one ofstart
,confirm_request
,confirm
,change
,catchup
,wait
,timeout
,retry
,stop
,error
- each of iriscouch's follow events is forwarded with a
db-
prefix - the originating db's name is passed as
details.db_name
- arguments are passed as
details.<eventname>
- Example: the
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
- allow negative numbers for
options.since
- removed
caught_up_dbs
andtotal_dbs
info from thedb-catchup
event - extended the
progress
event's parameter to an object withprogress
andcaught_up_dbs
andtotal_dbs
- renamed
options.filter
tooptions.include
(and add accordinglyoptions.exclude
) - added
"_local"
as an option tooptions.persist
- added
options.namespace
(needed for namespacing in the persistence layer) - added event
db-persist
which gets emitted after the current sequence id has been persisted to disk/db. - added
options.persist_debounce
- added
caught_up_dbs
andtotal_dbs
info to thedb-catchup
event'sdetails
- (internal) persistence layers are now async