dscape/nano — Issue #50: Implement Support for Document Update Handlers
Author: twilson63
Created: 2012-01-17 17:25:27+00:00
State: open

Hello,

I would like to implement support for CouchDb Document Update Handlers.

[http://wiki.apache.org/couchdb/Document_Update_Handlers](http://wiki.apache.org/couchdb/Document_Update_Handlers)

My thoughts are implementing a new function called update_doc:

``` 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_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.update = update_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.update("my_design_doc", "in-place", "", { field: "foo", value: "bar" }, function(e,b) { console.log(b); });
```

Thoughts?