Uploaded image for project: 'CouchDB'
  1. CouchDB
  2. COUCHDB-802

Doc ID should auto-generate if not provided, before sending to _update function [PATCH]

    XMLWordPrintableJSON

Details

    Description

      The main bug is this: _show and _update functions should be able to mimic the standard HTTP/JSON API. A common pattern people are moving to is rewriting to _show and _update, so the client thinks it is hitting normal couch, however additional logic happens (e.g. auto-timestamping).

      Unfortunately, _update cannot return an auto-generated ID for POST to /db/_design/ddoc/_update. The semantics should match POST to /db/ – If an _id is provided, use that; otherwise auto-generate one. The best an _update function can do now is Math.random() or similar; however one loses the advantage of sequential UUID generation from couch's internals.

      The fix is for couch to send a random UUID if the update URL did not include the final /Id component. The function itself in the view server can decide whether to use it. Assuming that change, the update function could at least be capable of duplicating the direct API using the following Javascript logic:

      function(doc, req) {
      if(doc && doc._id == req.id)

      { // To be pedantic, I could confirm req.method == "PUT" log("I am an update by id"); }

      else if(doc === null && req.id) {
      if(req.method == "POST")

      { log("I am a create, id was auto-generated"); }

      else if(req.method == "PUT")

      { log("I am a create, id was supplied by client"); }

      }
      }

      Attachments

        1. new_id.diff
          0.6 kB
          Jason Smith
        2. COUCHDB-802-with-test.diff
          1 kB
          Zachary Zolton
        3. COUCHDB-802-with-test_take2.diff
          2 kB
          Dmitry Unkovsky
        4. COUCHDB-802-with-test_take3.diff
          4 kB
          Dmitry Unkovsky

        Activity

          People

            Unassigned Unassigned
            jhs Jason Smith
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Time Tracking

                Estimated:
                Original Estimate - 24h
                24h
                Remaining:
                Remaining Estimate - 24h
                24h
                Logged:
                Time Spent - Not Specified
                Not Specified