dscape/nano — pull request #51 by twilson63 (2012-01-17 22:32:41+00:00)
Title: #50 Implement Support for Document Update Handlers

``` javascript
/*
* calls document update handler
*
* @param {design_name:string} design document namd
* @param {update_name:string} update method to call
* @param {doc_name:string} document name to update
* @param {params:object} additions to the querystring
*/
function update_with_handler_doc(design_name, update_name, doc_name, params, callback) {
if(typeof params === "function") {
callback = params;
params = {};
}
var update_path = '_design/' + design_name + '/_update/' + update_name + '/' + doc_name;
return relax({db: db_name, path: update_path, method: "PUT", params: params}, callback);
}
```

Then a public db alias called "update"

``` javascript
public_functions.updateWithHandler = update_with_handler_doc;
```

## usage

This should provide the ability to easily execute atomic updates on the couchdb server:

Document Update Handler in Design Document called my_design_doc:

``` javascript
"updates": {
"in-place" : "function(doc, req) {
var field = req.query.field;
var value = req.query.value;
var message = 'set '+field+' to '+value;
doc[field] = value;
return [doc, message];
}"
}
```

Then using nano:

``` javascript
@db.updateWithHandler("my_design_doc", "in-place", "", { field: "foo", value: "bar" }, function(e,b) { console.log(b); });
```