Skip to content

Instantly share code, notes, and snippets.

@bradfordw
Created June 11, 2009 02:33
Show Gist options
  • Select an option

  • Save bradfordw/127661 to your computer and use it in GitHub Desktop.

Select an option

Save bradfordw/127661 to your computer and use it in GitHub Desktop.
%couchdb connection information
-record(couch_config,{host="0.0.0.0",port=5984,db="mydatabase"}).
update_couchdb_property(OrigProps, {Key, _Value} = ChngProp) when is_list(OrigProps), is_tuple(ChngProp) ->
case lists:keymember(Key, 1, OrigProps) of
true ->
NewProps = lists:keyreplace(Key, 1, OrigProps, ChngProp);
false -> %append
NewProps = lists:append(OrigProps,[ChngProp])
end,
{ok, NewProps}.
update_couchdb_doc(OrigDoc, []) ->
CouchInfo = #couch_config{}, %Load CouchDB info defaults
{_DocIdKey,DocIdValue} = lists:keyfind(<<"_id">>,1,OrigDoc), %Get and then remove the ID
Doc = lists:keydelete(<<"_id">>,1,OrigDoc),
erlang_couchdb:update_document({CouchInfo#couch_config.host, CouchInfo#couch_config.port}, CouchInfo#couch_config.db,
binary_to_list(DocIdValue), Doc);
update_couchdb_doc(OrigDoc,[PropHead|PropTail]) when is_list(OrigDoc)->
{ok, NewDoc} = update_couchdb_property(OrigDoc, PropHead),
update_couchdb_doc(NewDoc,PropTail);
update_couchdb_doc({struct, NewDoc}, DocProps) when is_list(DocProps) ->
update_couchdb_doc(NewDoc,DocProps).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment