benoitc / couchbeam

Apache CouchDB client in Erlang
Other
242 stars 113 forks source link

Return ok status on successful operations along with the Document #24

Closed jeraymond closed 13 years ago

jeraymond commented 14 years ago

Currently when saving a document, couchbeam returns the saved document directly:

save_doc(Db::pid(), Doc::json_object()) -> json_object()

Or it returns an error if something bad happens. However at times, after saving a document, it is desirable to only know that the operation was successful if no further operations to the document are required. Right now I do something this like this:

SavedDoc = couchbeam_db:save_doc(Doc),
% do some validation on the doc itself like checking
% the ID to see if the save was OK
Id = couchbeam_doc:get_id(SavedDoc), % crash here on error
ExpectedId = Id % or crash here

What would be nice is if all operations would return something like:

save_doc(Db::pid(), Doc::json_object()) -> 
    {ok, json_object()} | {error, Error}

so that validations could be done like:

{ok, _} = couchbeam_db:save_doc(Doc)

In this case we'd crash immediately if an error occurred, and it's a lot more straightforward to verify if an operation was successful or not.

benoitc commented 13 years ago

Fixed in last head. couchbeam no longer throw. Ex :

 {ok, Doc} = couchbeam:open_db(Db, "someid")

Thanks for the feedback :)