hakarabakara
Checking if records exist with Memento
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
Most Liked
Exadra37
I cannot test it now, but I think you need to call to_conversation/1 outside the Memento transaction.
This is how I do in my app…
To get a resource:
def get_todo!(uid) do
_read_transaction(uid)
end
To read it from Mnesia:
defp _read_transaction(uid) do
result = Memento.transaction fn -> Memento.Query.read(Todo, uid) end
_parse_fetch_result(result)
end
To parse the result of reading it from Mnesia:
defp _parse_fetch_result(result) do
case result do
{:ok, nil} ->
[]
{:ok, []} ->
[]
{:ok, todo} ->
todo
{:error, error} ->
Logger.error("_parse_resul/1: #{error}")
Todo.new_changeset()
end
end
To note that the returned values foreach case are specific to my app.
Also bear in my mind that is app serves as a Learning path for me to know more about Mnesia and Elixir, thus I may also not doing it in the best way 
1
Popular in Questions
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
Hi there, I'm working through my first release with elixir/phoenix. I've built a release with distillery and found that it crashes when I...
New
Using vs code and installed ElixirLS: support and debugger.
And I got an error popped up on start up says
Failed to run ‘elixir’ comma...
New
The Elixir Typespec docs show the following syntax for keyword lists in typespecs:
# ...
| [key: type] # keyword lis...
New
Hey all, just started picking up Elixir last week and am writing a scraper as a learning project.
Baby step #1 is extracting the number ...
New
Hey,
Just curious what are the main benefits of Elixir compared to Clojure?
When is Elixir more useful than Clojure and vice versa?
Th...
New
I will often find my self writing things similar to:
case some_value do
nil -> something()
"" -> something()
_ -> someth...
New
Can someone explain the settings of pool_size of Ecto in config file? and what is the recommend size?
Thanks
New
Hi,
I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
Other popular topics
Phoenix 1.4.0 released
Phoenix 1.4 is out! This release ships with exciting new features, most notably
with HTTP2 support, improved deve...
New
Here are few pieces of (common) Linux knowledge that we use for reasonably small one server apps. We use Ubuntu but this should work for ...
New
Hello all!
I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
Good day to you all.
I have been struggling to get a query involving like and ilike to work.
Can anyone assist me on this, please?
pro...
New
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this
"1000"
What is the ...
New
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
Hi all,
I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage.
I'm trying to use Postg...
New







