Dear Wiki user,

You have subscribed to a wiki page or wiki category on "Couchdb Wiki" for 
change notification.

The 
"How_to_intercept_document_updates_and_perform_additional_server-side_processing"
 page has been changed by SebastianCohnen.
The comment on this change is: added toc; syntax hl; cleanup.
http://wiki.apache.org/couchdb/How_to_intercept_document_updates_and_perform_additional_server-side_processing?action=diff&rev1=4&rev2=5

--------------------------------------------------

+ = Document Update Handlers =
+ <<TableOfContents()>>
+ 
  == Basics ==
  
  CouchDB (0.10 and up) has the ability to allow server-side processing of an 
incoming document before it's committed. This feature allows a range of use 
cases such as providing a server-side last modified timestamp, etc.
@@ -8, +11 @@

  
  This functionality is implemented via document update handlers defined in a 
design doc. Specifically, in a design doc one defines an "updates" attribute 
that contains any number of document update handlers. The follow handlers 
should be self-explanatory as to what they accomplish. 
  
- {{{
+ {{{#!highlight javascript
+ {
-     updates: {
+   updates: {
  
-       "hello" : function(doc, req) {
+     "hello" : function(doc, req) {
-         if (!doc) {
+       if (!doc) {
-           if (req.docId) {
+         if (req.docId) {
-             return [{
+           return [{
-               _id : req.docId
+             _id : req.docId
-             }, "New World"]
+           }, "New World"]
-           }
-           return [null, "Empty World"];          
          }
+         return [null, "Empty World"];          
+       }
-         doc.world = "hello";
+       doc.world = "hello";
-         doc.edited_by = req.userCtx;
+       doc.edited_by = req.userCtx;
-         return [doc, "hello doc"];
+       return [doc, "hello doc"];
-       },
+     },
  
-       "in-place" : function(doc, req) {
+     "in-place" : function(doc, req) {
-         var field = req.query.field;
+       var field = req.query.field;
-         var value = req.query.value;
+       var value = req.query.value;
-         var message = "set "+field+" to "+value;
+       var message = "set "+field+" to "+value;
-         doc[field] = value;
+       doc[field] = value;
-         return [doc, message];
+       return [doc, message];
-       },
+     },
  
-       "bump-counter" : function(doc, req) {
+     "bump-counter" : function(doc, req) {
-         if (!doc.counter) doc.counter = 0;
+       if (!doc.counter) doc.counter = 0;
-         doc.counter += 1;
+       doc.counter += 1;
-         var message = "<h1>bumped it!</h1>";
+       var message = "<h1>bumped it!</h1>";
-         return [doc, message];
+       return [doc, message];
-       },
+     },
  
-       "error" : function(doc, req) {
+     "error" : function(doc, req) {
-         superFail.badCrash;
+       superFail.badCrash;
-       },
+     },
  
-       "xml" : function(doc, req) {
+     "xml" : function(doc, req) {
-         var xml = new XML('<xml></xml>');
+       var xml = new XML('<xml></xml>');
-         xml.title = doc.title;
+       xml.title = doc.title;
-         var posted_xml = new XML(req.body);
+       var posted_xml = new XML(req.body);
-         doc.via_xml = posted_xml.foo.toString();
+       doc.via_xml = posted_xml.foo.toString();
-         var resp =  {
+       var resp =  {
-           "headers" : {
+         "headers" : {
-             "Content-Type" : "application/xml"
+           "Content-Type" : "application/xml"
-           },
+         },
-           "body" : xml
+         "body" : xml
-         };
+       };
-          
+      
-          return [doc, resp];
+        return [doc, resp];
-        }
-     }
+      }
+   }
+ }
  }}}
  
  The handler function takes the document and the http request as parameters. 
It returns a two-element array: the first element is the (updated) document, 
which is committed to the database. The second element is the response that 
will be sent back to the caller.

Reply via email to