You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by Apache Wiki <wi...@apache.org> on 2012/09/17 21:16:03 UTC

[Couchdb Wiki] Update of "Document_Update_Handlers" by RobinBerjon

Dear Wiki user,

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

The "Document_Update_Handlers" page has been changed by RobinBerjon:
http://wiki.apache.org/couchdb/Document_Update_Handlers?action=diff&rev1=24&rev2=25

    }
  }
  }}}
- 
  The handler function takes the most recent version of the document from the database and the http request environment as parameters. It returns a two-element array: the first element is the (updated or new) document, which is committed to the database. If the first element is null no document will be committed to the database. If you are updating an existing, it should already have an `_id` set, and if you are creating a new document, make sure to set its `_id` to something, either generated based on the input or the `req.uuid` provided. The second element is the response that will be sent back to the caller.
  
  If you want the client to get the changed document (without an additional request) you can return it as the second element of the return array, but you need to turn it into a string first.
+ 
  {{{#!highlight javascript
  {
    updates: {
@@ -99, +99 @@

  '''Note''' The document returned in this manner will have the "old" _rev, see the response section for how retrieve the new/current _rev.
  
  == Request ==
- 
  The request parameter will look something like this for a update function designed to create a new document:
  
  {{{#!highlight javacript
@@ -122, +121 @@

    "userCtx": {"db": "loot", "name": null, "roles": []}
  }
  }}}
- 
  Since no ID was passed, the request doesn't have an ID.  However, the CouchDB server helpfully provides a UUID so you can create a new document with a unique ID and be sure it won't conflict with any document in the database already.
  
  The server also parses the POST body into a Javascript object called `form` and does the same with the query string, in `query`.
@@ -140, +138 @@

    "body" : doc.xml
  };
  }}}
+ or just a plain string:
  
- or just a plain string:
  {{{
      <p>Update function complete!</p>
  }}}
- Though you can set the headers, right now the status code for an update function response is hardcoded to be 200/201 unless an error occurs. See [[https://issues.apache.org/jira/browse/COUCHDB-648|this issue]] in JIRA.
+ In addition to setting headers, you may set a code attribute on the object in order to control the HTTP status.
  
  == Usage ==
  To invoke a handler, use one of:
  
+  *
-  * a PUT request against the handler function with a document id: `/<database>/_design/<design>/_update/<function>/<docid>`
+  a PUT request against the handler function with a document id: `/<database>/_design/<design>/_update/<function>/<docid>`
+ 
+  *
-  * a POST request agasint the handler function without a document id: `/<database>/_design/<design>/_update/<function>`
+  a POST request agasint the handler function without a document id: `/<database>/_design/<design>/_update/<function>`
+ 
  
  The document id specified in a PUT request URI is available in the update handler as id property on the request object (req.id).