From 457e56f3123304547dfa4f623ed9c2e61ec7b754 Mon Sep 17 00:00:00 2001 From: jfwittmann Date: Thu, 17 Jan 2013 02:26:40 +0100 Subject: [PATCH 1/3] add callUpdate function to invoke database update handler --- db.js | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/db.js b/db.js index d4983bd..27465cd 100644 --- a/db.js +++ b/db.js @@ -931,6 +931,61 @@ 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') { + 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. From be03e55355667120eb237930f4ccfd7f96c8fd09 Mon Sep 17 00:00:00 2001 From: jfwittmann Date: Thu, 17 Jan 2013 02:50:37 +0100 Subject: [PATCH 2/3] update README --- README.md | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) 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) From 46f7bd982d3163f28a83463a715ad5f41780c6bf Mon Sep 17 00:00:00 2001 From: jfwittmann Date: Thu, 17 Jan 2013 21:07:57 +0100 Subject: [PATCH 3/3] fix optional argument handling in callUpdate --- db.js | 1 + 1 file changed, 1 insertion(+) diff --git a/db.js b/db.js index 27465cd..d552c16 100644 --- a/db.js +++ b/db.js @@ -957,6 +957,7 @@ DB.prototype.callUpdate = function (name, update, /*optional*/ docid, /*optional q = {}; } else { if (typeof docid == 'object') { + callback = q; q = docid; docid = null; } else {