Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added purge command for purging a document #261

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions lib/cradle/database/documents.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,3 +235,40 @@ Database.prototype.remove = function (id, rev) {

remove();
};

Database.prototype.purge = function(id, rev) {
var that = this, doc, args = new(Args)(arguments);
function purge() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move the purge function to the outer scope, and pass the arguments using function parameters.

var body = {};
body[id] = [rev];
that.query({
method: 'POST',
path: '_purge',
body: body
}, function (err, res) {
if (! err) { that.cache.purge(id); }
args.callback(err, res);
});
}

if (typeof(rev) !== 'string') {
if (doc = this.cache.get(id)) {
rev = doc._rev;
}
else {
return this.get(id, function (err, _doc) {
if (err) {
return args.callback(err);
}
else if (!_doc._rev) {
return args.callback(new Error('No _rev found for ' + id));
}

rev = _doc._rev;
purge();
});
}
}

purge();
};
16 changes: 14 additions & 2 deletions test/database-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,20 @@ function shouldQueryCouch(name) {
return promise;
},
"returns a 200": macros.status(200)
}
}
},
"purging a document (PURGE)": {
topic: function (db) {
var promise = new(events.EventEmitter);
db.get('deleteme', function (e, res) {
db.purge('deleteme', res.rev, function (e, res) {
promise.emit('success', res);
});
});
return promise;
},
"returns a 200": macros.status(200)
},
};
}

var cradle = require('../lib/cradle');
Expand Down