A simple Node API for dealing with the couch _replicator database
You can fetch status for a database replication, get a replication doc, delete a replication doc, and put a replication doc.
const CouchReplicator = require('couch-replicator-api')
, couchUrl = 'http://npm.nodejs.org.au:5984'
, couchUser = 'admin'
, couchPass = 'super$ecure'
, db = 'registry'
CouchReplicator.status(couchUrl, couchUser, couchPass, db, printStatus)
// also:
// - CouchReplicator.get(url, user, pass, db, callback)
// - CouchReplicator.del(url, user, pass, db, [rev, ] callback)
// - CouchReplicator.put(url, user, pass, db, doc, callback)
function printStatus (err, data) {
if (err)
throw err
console.log('status:', data)
}
Will print the replication status:
status: { pid: '<0.26018.0>',
checkpointed_source_seq: 93275,
continuous: true,
doc_id: 'registry',
doc_write_failures: 0,
docs_read: 450,
docs_written: 450,
missing_revisions_found: 450,
progress: 98,
replication_id: 'cb249c0a32a9c3c289c29efbea91fd92+continuous',
revisions_checked: 533,
source: 'https://fullfatdb.npmjs.com/registry/',
source_seq: 94787,
started_on: 1391948768,
target: 'registry',
type: 'replication',
updated_on: 1392021201 }
Alternatively you can use the CouchReplicator
as a constructor and store the Couch URL, admin credentials and database id in the object to make calls even simpler:
const CouchReplicator = require('couch-replicator-api')
, replicator = new CouchReplicator(
'http://npm.nodejs.org.au:5984'
, 'admin'
, 'super$ecure'
, 'registry'
)
replicator.status(function (err, data) {
if (err)
throw err
console.log('status:', data)
}
// also:
// - replicator.get(callback)
// - replicator.del([rev, ] callback)
// - replicator.put(doc, callback)
Get the status doc from CouchDB for this replication. The doc comes from /_active_tasks and has the type
of 'replication'
and 'target'
of the db specified.
CouchReplicator.status(url, user, pass, db, callback)
// or on an existing CouchReplicator object:
replicator.status(callback)
Gets the replication doc from /_replicator/.
CouchReplicator.get(url, user, pass, db, callback)
// or on an existing CouchReplicator object:
replicator.get(callback)
Deletes the replication doc from /_replicator/?rev=revision, immediately stopping replication. Note that if rev
is not supplied then a get()
will be performed to look up the latest rev
for this replication doc.
CouchReplicator.del(url, user, pass, db, [rev, ] callback)
// or on an existing CouchReplicator object:
replicator.del([rev, ] callback)
Put a new replication doc at /_replicator/ as specified by the doc
parameter.
CouchReplicator.put(url, user, pass, db, doc, callback)
// or on an existing CouchReplicator object:
replicator.put(doc, callback)
Generally a replication doc should look something like this:
{
"source": "https://fullfatdb.npmjs.com/registry",
"target": "registry",
"continuous": true,
"user_ctx": {
"name": "adminuser",
"roles": [
"_admin"
]
}
}
See this gist for details about the _replicator database.
node-couch-replicator-api is Copyright (c) 2014 Rod Vagg @rvagg and licenced under the MIT licence. All rights not explicitly granted in the MIT license are reserved. See the included LICENSE file for more details.