couchbeam
couchbeam copied to clipboard
Maps support
Hi. How about maps support?
Some like this couchbeam:save_doc/couchbeam:open_doc
diff --git a/src/couchbeam.erl b/src/couchbeam.erl
index 8b3c967..808f6ca 100644
--- a/src/couchbeam.erl
+++ b/src/couchbeam.erl
@@ -441,7 +441,14 @@ open_doc(Db, DocId) ->
%% Params is a list of query argument. Have a look in CouchDb API
%% @spec open_doc(Db::db(), DocId::string(), Params::list())
%% -> {ok, Doc}|{error, Error}
-open_doc(#db{server=Server, options=Opts}=Db, DocId, Params) ->
+open_doc(#db{server=Server, options=Opts}=Db, DocId, ParamsWithMaps) ->
+
+ {ReturnMaps, Params} = case lists:keytake(return_maps, 1, ParamsWithMaps) of
+ {value,{return_maps,true}, RestPatams} -> {true, RestPatams};
+ {value,{return_maps,_}, RestPatams} -> {false, RestPatams};
+ _ -> {false, ParamsWithMaps}
+ end,
+
DocId1 = couchbeam_util:encode_docid(DocId),
%% is there any accepted content-type passed to the params?
@@ -474,7 +481,11 @@ open_doc(#db{server=Server, options=Opts}=Db, DocId, Params) ->
end},
{ok, {multipart, InitialState}};
_ ->
- {ok, couchbeam_httpc:json_body(Ref)}
+ Return = case ReturnMaps of
+ true -> couchbeam_httpc:json_body(Ref, [return_maps]);
+ _ -> couchbeam_httpc:json_body(Ref)
+ end,
+ {ok, Return}
end;
Error ->
Error
@@ -604,7 +615,26 @@ save_doc(#db{server=Server, options=Opts}=Db, {Props}=Doc, Atts, Options) ->
Error ->
Error
end
- end.
+ end;
+save_doc(#db{server=Server, options=Opts}=Db, Doc, [], Options) ->
+ case maps:get(<<"_id">>, Doc, undefined) of
+ undefined -> {error, <<"_id field not exsists">>};
+ DocId ->
+ Url = hackney_url:make_url(server_url(Server), doc_url(Db, DocId), Options),
+ JsonDoc = jiffy:encode(Doc),
+ Headers = [{<<"Content-Type">>, <<"application/json">>}],
+ case couchbeam_httpc:db_request(put, Url, Headers, JsonDoc, Opts, [200, 201]) of
+ {ok, _, _, Ref} ->
+ {JsonProp} = couchbeam_httpc:json_body(Ref),
+ NewRev = couchbeam_util:get_value(<<"rev">>, JsonProp),
+ NewDocId = couchbeam_util:get_value(<<"id">>, JsonProp),
+ Doc1 = Doc#{<<"_rev">> => NewRev, <<"_id">> := NewDocId},
+ {ok, Doc1};
+ Error ->
+ Error
+ end
+ end.
+
%% @doc delete a document
%% @equiv delete_doc(Db, Doc, [])
diff --git a/src/couchbeam_httpc.erl b/src/couchbeam_httpc.erl
index 194177d..f7a11ab 100644
--- a/src/couchbeam_httpc.erl
+++ b/src/couchbeam_httpc.erl
@@ -7,7 +7,7 @@
-export([request/5,
db_request/5, db_request/6,
- json_body/1,
+ json_body/1, json_body/2,
db_resp/2,
make_headers/4,
maybe_oauth_header/4]).
@@ -28,6 +28,10 @@ db_request(Method, Url, Headers, Body, Options, Expect) ->
json_body(Ref) ->
{ok, Body} = hackney:body(Ref),
couchbeam_ejson:decode(Body).
+json_body(Ref, [return_maps]) ->
+ {ok, Body} = hackney:body(Ref),
+ jiffy:decode(Body, [return_maps]).
+
make_headers(Method, Url, Headers, Options) ->
Headers1 = case couchbeam_util:get_value(<<"Accept">>, Headers) of
Oh sorry, work is being done now
i fully support that feature. Could you make it avaialble a s a PR? Also it should be made optional for now for legacy users imo. Thoughts?
I do not have a sufficient qualification, but I'll try
Thanks :)
On Wed, Feb 25, 2015 at 5:31 PM, stofel [email protected] wrote:
I do not have a sufficient qualification, but I'll try
— Reply to this email directly or view it on GitHub https://github.com/benoitc/couchbeam/issues/125#issuecomment-75993356.
It is impossible while travis check this otp_releases:
- R16B03-1
- 17.0
- 17.1
- 18.0
- 18.1
R16B03-1 not support maps.