Skip to content

Instantly share code, notes, and snippets.

@ierceg
Last active August 29, 2015 14:09
Show Gist options
  • Save ierceg/d7540d6519303f2b55b3 to your computer and use it in GitHub Desktop.
Save ierceg/d7540d6519303f2b55b3 to your computer and use it in GitHub Desktop.
Clean-up non-design-docs from a CouchDb at the beginning of each test
var db = require('nano');
var debug = require('debug')('cleanDb');
var async = require('async');
function cleanDb(callback) {
debug('Cleaning up db from all non-design documents');
db.list(function(err, body) {
if(err) {
return callback(err);
}
debug('Marking all non-design documents out of', body.rows.length, 'as deleted');
var docs = [];
return async.each(
body.rows,
function(doc, next) {
if(!doc) {
return next('doc undefined');
}
// Don't destroy the design docs.
if(doc.id.indexOf('_design/') === 0) {
return next();
}
docs.push({
_id: doc.id,
_rev: doc.value.rev,
_deleted: true
});
return next();
},
function(err) {
if(err) {
return callback(err);
}
debug('Bulk uploading changes to', docs.length, 'deleted docs.');
return db.bulk({ docs: docs }, function(err) {
debug('Done deleting non-design docs.');
callback(err);
});
}
);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment