Simple sequence based document versioning for CouchDB. Requires a Cradle connection to CouchDB for the Changes API. It can be used to publish version based datasets to clients, without having to resend the entire dataset each time an update is requested.
The module will subscribe to a changes feed via the CouchDB Changes API and monitor all the documents within the 'watchList' array.
npm install node-couch-sequence
var opts = {
db: [CRADLE_CONNECTION],
versioningDoc: 'NAME_OF_DOC_TO_STORE_VERSIONS',
watchList: ['products', 'categories']
};
var SequenceHandler = require('node-couch-sequence')(opts);
You can then query the sequence handler as much as you like:
var latest = SequenceHandler.getLatestSequenceIndex();
SequenceHandler.getChangesSince([SEQUENCE NUMBER], function(err, changes) {
console.log(changes);
});
"1": {
"products": [
{
"action": "UPDATE",
"key": "FirstProduct",
"data": "New product name"
}
]
},
"2": {
"products": [
{
"action": "INSERT",
"key": "AnotherProduct",
"data": null
},
{
"action": "DELETE",
"key": "FirstProduct"
}
]
}
As in,
"SEQUENCE_NUMBER": {
"DOCUMENT_NAME": [
// ARRAY_OF_CHANGES
{
"action": "INSERT/UPDATE/DELETE",
"key": "KEY THAT HAS BEEN MODIFIED",
"data": "THE NEW DATA"
},
///
]
}