diff options
author | John Christopher Anderson <jchris@apache.org> | 2009-08-11 18:50:08 +0000 |
---|---|---|
committer | John Christopher Anderson <jchris@apache.org> | 2009-08-11 18:50:08 +0000 |
commit | 36bbc72b6b0992639a0ba7a2077d75ec9f7cf03d (patch) | |
tree | 3e4e0d1e46a7fe04a9b185455a025e8ff5fa3e62 /src/couchdb/couch_query_servers.erl | |
parent | ea95901fe52df11338134bd86f9bc8f028c5444b (diff) |
Initial commit of _update handler. Thanks to Paul Davis, Jason Davies for code and others for discussion.
The _update handler accepts POSTs to paths like: /db/_design/foo/_update/bar and PUTs which include docids, like: /db/_design/foo/_update/bar/docid
The function signature:
function(doc, req) {
doc.a_new_field = req.query.something;
return [doc, "<h1>added something to your doc</h1>"];
}
The tests in update_documents.js are fairly complete and include examples of bumping a counter, changing only a single field, parsing from (and returning) XML, and creating new documents.
git-svn-id: https://svn.apache.org/repos/asf/couchdb/trunk@803245 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/couchdb/couch_query_servers.erl')
-rw-r--r-- | src/couchdb/couch_query_servers.erl | 20 |
1 files changed, 19 insertions, 1 deletions
diff --git a/src/couchdb/couch_query_servers.erl b/src/couchdb/couch_query_servers.erl index bb0cc853..fca7c85a 100644 --- a/src/couchdb/couch_query_servers.erl +++ b/src/couchdb/couch_query_servers.erl @@ -18,7 +18,7 @@ -export([init/1, terminate/2, handle_call/3, handle_cast/2, handle_info/2,code_change/3,stop/0]). -export([start_doc_map/2, map_docs/2, stop_doc_map/1]). -export([reduce/3, rereduce/3,validate_doc_update/5]). --export([render_doc_show/6, start_view_list/2, +-export([render_doc_show/6, render_doc_update/6, start_view_list/2, render_list_head/4, render_list_row/4, render_list_tail/1]). -export([start_filter/2, filter_doc/4, end_filter/1]). % -export([test/0]). @@ -170,6 +170,7 @@ validate_doc_update(Lang, FunSrc, EditDoc, DiskDoc, Ctx) -> after ok = ret_os_process(Lang, Pid) end. +% todo use json_apply_field append_docid(DocId, JsonReqIn) -> [{<<"docId">>, DocId} | JsonReqIn]. @@ -190,6 +191,23 @@ render_doc_show(Lang, ShowSrc, DocId, Doc, Req, Db) -> ok = ret_os_process(Lang, Pid) end. +render_doc_update(Lang, UpdateSrc, DocId, Doc, Req, Db) -> + Pid = get_os_process(Lang), + {JsonReqIn} = couch_httpd_external:json_req_obj(Req, Db), + + {JsonReq, JsonDoc} = case {DocId, Doc} of + {nil, nil} -> {{JsonReqIn}, null}; + {DocId, nil} -> {{append_docid(DocId, JsonReqIn)}, null}; + _ -> {{append_docid(DocId, JsonReqIn)}, couch_doc:to_json_obj(Doc, [revs])} + end, + try couch_os_process:prompt(Pid, + [<<"update">>, UpdateSrc, JsonDoc, JsonReq]) of + FormResp -> + FormResp + after + ok = ret_os_process(Lang, Pid) + end. + start_view_list(Lang, ListSrc) -> Pid = get_os_process(Lang), true = couch_os_process:prompt(Pid, [<<"add_fun">>, ListSrc]), |