3 notes

Nano v1.2.x Document Update Handler Support v1.2.x


nano and server side document updates

I would like to announce that nano a nodejs couchdb driver now supports CouchDb Document Update Handlers

nano is a minimalistic couchdb driver for nodejs


CouchDb Document Update Handler is a way to add partial updates to couchdb documents via server side updates. This means that you do not have to first get the whole document make the change and then make a change and finally put the document on the server, if a changed occurred during this process, you would need to get the latest revision and attempt the update again. This process is great for most operations, but I needed a way to perform server side updates. The project is called cloudq, which is a high performance atomic and persistent job queue completely on nodejs and couchdb.

cloudq needs each job to move from queued to reserved and then return to the worker for action. And it is important to get the next document in the queue and update the state from queued to reserved as fast as possible.

CouchDb has the ability to build a Document Update Handler, similar to a View, you can add this handler to a design document. Then you can execute the Document Updater by performing a put and passing the document id, and a querystring. Here is an example of an in-place document updater from the CouchDb Wiki:


"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];

This in-place Document Handler allows you to pass a field and value to update a document. Now in nano you can simply run:

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

You can do much more with document handlers, like bumping counters, or return xml see more.

Thanks again to @dscape for the nano https://github.com/dscape/nano project, it is the perfect couchdb driver for NodeJs.

Thanks again to @mikeal for the request https://github.com/mikeal/request module.

Thanks to @ryah for the creation of nodejs https://github.com/joyent/node

  1. driverupdate-dk reblogged this from jackhq
  2. jackhq posted this