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

hi! added function to invoke database update handler #8

Open
wants to merge 3 commits 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
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
56 changes: 56 additions & 0 deletions db.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down