boss_db
boss_db copied to clipboard
Proposal for alternatives to delete/1
I'm currently doing some development on the DynamoDB-adapter and since the introduction of alternative primary_key I'm having some troubles using delete/1.
I suggest that a new function is added. I'm having two suggestion of how such a function could look like:
Alternative1: Conditional deletion
I think this is a good solution. Today you can get the "same" functionality with a list comprehension, but you'll have to do N+1 calls. (Eg [ boss_db:delete(X:id()) || X <- boss_db:find(some_model, [{some_condition, ...}]) ].
My propsal would look something like this:
delete(ModelName :: atom(), [{Field1 :: atom(), Val :: any()}]) -> ok || {error, Reason}
Alternative2: Delete with models This solution is more a replacement of the existing delete/1 function. The idea is that you send in an existing model and the function determines how to remove it (Based on the primary_key). The function would look something like this:
-spec delete(Model :: tuple) -> ok || {error, Reason}
delete(Model) when is_tuple(Model) ->
Rec = element(1, Model),
[PrimaryKey] = proplists:get_value(primary_key, Rec:module_info(attributes), [id]),
...
What do you think?
I like Alternative1. It has a nice symmetry with find/2.