How about instead of the update function passing back a doc with no
ID, if no docid is specified, then a uuid is generated and passed to
the update handler in the req? That way it can pass the auto-generated
ID back to the client as well.
See the attached patch.
At first I was going to just suggest passing a new uuid in the DocID
attribute to json_req_obj, but the problem with that would be the
inability to distinguish between a non-existing user-supplied ID and
an auto-generated ID, so I instead added it to a new "uuid" attribute
on the request parameter to the update function.
On Sat, Jun 12, 2010 at 6:24 AM, Jason Smith <[email protected]> wrote:
> I enjoy using _show and _update functions to mimic the HTTP doc API,
> adding conveniences such as auto-timestamping, etc.
>
> One thing _update cannot duplicate vs. the normal API is automatic UUID
> generation when _id is unspecified
>
> The only solution is to generate a UUID in the update function; but this
> sacrifices reference locality using the (default) sequential internal
> UUID algorithm.
>
> Is this just something we have to endure, or can couchdb be made to fill
> it in? In principle, I feel like _update is a convenience around the
> doc API. Auto-ID should be possible.
>
> Attached is a patch as food for thought; however it has a bug: the query
> server already computed the response (with arbitrary content-type and
> response body). It seems too late for the couch server to go inserting
> fields in the doc. But of course, the doc is invalid in the first place
> if _id is undefined.
>
> --
> Jason Smith
> Couchio Hosting
>
--
Paul Bonser
http://probablyprogramming.com
Index: src/couchdb/couch_httpd_show.erl
===================================================================
--- src/couchdb/couch_httpd_show.erl (revision 953193)
+++ src/couchdb/couch_httpd_show.erl (working copy)
@@ -123,7 +123,14 @@
send_error(Req, 404, <<"update_error">>, <<"Invalid path.">>).
send_doc_update_response(Req, Db, DDoc, UpdateName, Doc, DocId) ->
- JsonReq = couch_httpd_external:json_req_obj(Req, Db, DocId),
+ JsonReq0 = couch_httpd_external:json_req_obj(Req, Db, DocId),
+ JsonReq = case DocId of
+ null ->
+ {ReqProps} = JsonReq0,
+ {[{<<"uuid">>, couch_uuids:new()} | ReqProps]};
+ _Else ->
+ JsonReq0
+ end,
JsonDoc = couch_query_servers:json_doc(Doc),
{Code, JsonResp1} = case couch_query_servers:ddoc_prompt(DDoc,
[<<"updates">>, UpdateName], [JsonDoc, JsonReq]) of