You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@couchdb.apache.org by GitBox <gi...@apache.org> on 2018/04/12 15:55:06 UTC

[GitHub] wohali opened a new issue #1280: Add PATCH/partial update support

wohali opened a new issue #1280: Add PATCH/partial update support
URL: https://github.com/apache/couchdb/issues/1280
 
 
   A mechanism that allowed server-side field updating of a document would be lovely. Ideally this would use [RFC 5789 - HTTP PATCH](http://tools.ietf.org/html/rfc5789).
   
   ## Expected Behaviour
   The PATCH request provides a partial object that is merged with the original doc and stored in the database.
   
   ```
   $ curl -X PUT http://127.0.0.1:5984/foo/bar -d '{"user": { "firstname" : "Walter", "lastname": "Carlos" } }'
   {"ok":true,"id":"bar","rev":"1-0f958fa6ab68dffc4ac5b76efb229e39"}
   $ curl -X PATCH -H "Content-Type: application/json" http://127.0.0.1:5984/foo/bar -d '{"user": { "firstname": "Wendy" }, "profession": "Musician" }'
   {"ok":true,"id":"bar","rev":"2-900ae13b0644ed3a92be1a79f54d59d6"}
   $ curl http://127.0.0.1:5984/foo/bar
   {"_id":"bar","_rev":"2-900ae13b0644ed3a92be1a79f54d59d6","user":{"firstname":"Wendy","lastname":"Carlos"},"profession":"Musician"}
   ```
   
   It would be nice to allow `PATCH` to avoid providing the `rev` token, as shown above. This supports performing the entire `GET-MODIFY-PUT` cycle with a single HTTP request.
   
   ## Current Behaviour
   It is possible to implement this with a carefully crafted VDU, as written by Mark Hahn, contributed to the old wiki and licensed under ALv2.
   
   coffeescript version:
   ```coffeescript
     partialUpdate: (doc, req) ->
       if not doc then return [null, JSON.stringify status: 'nodoc']
       for k, v of JSON.parse req.body
         if k[0] is '/'
           nestedDoc = doc
           nestedKeys = k.split '/'
           for nestedKey in nestedKeys[1..-2]
             nestedDoc = (nestedDoc[nestedKey] ?= {})
           k = nestedKeys[-1..-1][0]
           if v is '__delete__' then delete nestedDoc[k]
           else nestedDoc[k] = v
           continue
         if v is '__delete__' then delete doc[k]
         else doc[k] = v
       [doc, JSON.stringify {doc, status: 'updated'}]
   ```
   javascript version:
   ```javascript
   partialUpdate: function(doc, req) {
     if (!doc) {
       return [
         null, JSON.stringify({
           status: 'nodoc'
         })
       ];
     }
     _ref = JSON.parse(req.body);
     for (k in _ref) {
       v = _ref[k];
       if (k[0] === '/') {
         nestedDoc = doc;
         nestedKeys = k.split('/');
         _ref1 = nestedKeys.slice(1, -1);
         for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
           nestedKey = _ref1[_i];
           nestedDoc = ((_ref2 = nestedDoc[nestedKey]) != null ? _ref2 : nestedDoc[nestedKey] = {});
         }
         k = nestedKeys.slice(-1)[0];
         if (v === '__delete__') {
           delete nestedDoc[k];
         } else {
           nestedDoc[k] = v;
         }
         continue;
       }
       if (v === '__delete__') {
         delete doc[k];
       } else {
         doc[k] = v;
       }
     }
     return [
       doc, JSON.stringify({
         doc: doc,
         status: 'updated'
       })
     ];
   }
   ```
   
   The above VDU doesn't take a partial object as in the proposal, but a specially formatted object to merge:
   
   ```
   {
     field_one: "AAA",
     "/topLevelObject/nestedField_one": "__delete__",
     "/topLevelObject/nestedField_two": 99,
     "/topLevelObject/nestedField_three/dblNest": 73
   }
   ```
   
   ## Context
   Avoiding the entire `GET-MODIFY-PUT` cycle reduces latency.
   
   ## Your Environment
   <!--- Include as many relevant details about the environment you experienced the bug in -->
   * Version used:
   * Browser Name and version:
   * Operating System and version (desktop or mobile):
   * Link to your project:

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services