Checking if records exist with Memento
Closed this issue · 0 comments
marvoh commented
I am using memento to wrap Mnesia functionality into my application.
I want to check if a record with a certain id exists and if not perform certain actions.
Documentation says read should return nil when record doesn’t exist. Howerver, the Memento.transation wrapper aborts with a:not_exists
exception therefore my application cannot proceed.
Here’s how my function looks like:
def find_conversation(user_id) do
Memento.transaction! fn ->
user_id
|> read
|> to_conversation
end
end
defp to_conversation(nil), do: nil
defp to_conversation(%@store{} = conversation) do
struct(Conversation, Map.from_struct(conversation))
end