diff --git a/README.md b/README.md index b28bd2c..68dddfe 100644 --- a/README.md +++ b/README.md @@ -335,6 +335,26 @@ is reserved for any exceptions that occurred (node.js style). * __options__ - _Object_ - (optional) Options for the bulk-save operation. * __callback(err,response)__ - _Function_ - A function to accept results and/or errors. Document update conflicts are reported in the results array. +#### DB.callUpdate(name, update, [docid], [q], callback) + +Call an update handler. +This are functions that clients can request to invoke server-side logic +that will create or update a document. + +README: http://wiki.apache.org/couchdb/Document_Update_Handlers + + + +**Parameters:** +* __name__ - _String_ - name of the design doc to use +* __update__ - _String_ - name of the update handler function +* __docid__ - _String_ - (optional) id of the document to apply the update function to +* __q__ - _Object_ - (optional) Options for the invoke update-handler operation. +* __callback(err, response)__ - _Function_ - A function to accept results and/or errors. Document update conflicts are reported in the results array. + +**q Options:** +* __params__ - the url query params +* __body__ - the POST/PUT body #### DB.bulkGet(keys, [q], callback) diff --git a/db.js b/db.js index d4983bd..d552c16 100644 --- a/db.js +++ b/db.js @@ -931,6 +931,62 @@ DB.prototype.bulkSave = function (docs, /*optional*/ options, callback) { exports.request(req, callback); }; +/** + * Call an update handler. + * This are functions that clients can request to invoke server-side logic + * that will create or update a document. + * + * README: http://wiki.apache.org/couchdb/Document_Update_Handlers + * + * __Options:__ + * * _params_ - the url query params + * * _body_ - the POST/PUT body + * + * @name DB.callUpdate(name, update, [docid], [q], callback) + * @param {String} name - name of the design doc to use + * @param {String} update - name of the update handler function + * @param {String} docid (optional) - id of the document to apply the update function to + * @param {Object} q (optional) + * @param {Function} callback(err, response) + */ +DB.prototype.callUpdate = function (name, update, /*optional*/ docid, /*optional*/ q, callback) { + if (!callback) { + if (!q) { + callback = docid; + docid = null; + q = {}; + } else { + if (typeof docid == 'object') { + callback = q; + q = docid; + docid = null; + } else { + callback = q; + q = {}; + } + } + } + try { + var data = exports.stringifyQuery(q.body || {}); + } + catch (e) { + return callback(e); + } + + var req_method = (docid ? 'PUT' : 'POST'); + var updatename = exports.encode(update); + + var update_url = this.url + '/_design/' + + exports.encode(name) + '/_update/' + exports.encode(updatename) + + (docid ? '/' + exports.encode(docid): ''); + + var req = { + type: req_method, + url: update_url + exports.escapeUrlParams(q.params), + data: data + }; + exports.request(req, callback); +}; /** * Requests a list of documents, using only a single HTTP request.