Skip to content

Instantly share code, notes, and snippets.

@pdaoust
Created November 23, 2011 23:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pdaoust/1390208 to your computer and use it in GitHub Desktop.
Save pdaoust/1390208 to your computer and use it in GitHub Desktop.
augmenting Resourceful's Couchdb engine to allow insertion of new records without ID
Couchdb.prototype.post = function (doc, callback) {
return this.request('post', doc, function (e, res) {
if (e) return callback(e);
res.status = 201;
callback(null, res);
});
};
Couchdb.prototype.save = function (id, doc, callback) {
var args = Array.prototype.slice.call(arguments, 0);
var callback = args.pop();
var doc = args.pop();
// if there's an ID left in args after popping off the callback and
// the doc, then we need to PUT, otherwise create a new record thru POST
if (args.length) {
return this.put.apply(this, arguments);
} else {
return this.post.call(this, doc, callback);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment