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
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
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
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
I have an umbrella app.
Some of the apps inside depend on other apps in the umbrella, unsurprisingly.
I'm writing a test for one of t...
New
Hi,
I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
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
Hey all,
I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
After calling mix ecto.create I get this error:
17:00:32.162 [error] GenServer #PID<0.412.0> terminating
** (Postgrex.Error) FATAL...
New
To simplify some tasks at work, I wrote and published this package yesterday. It’s a simple macro that enables Access behaviour on struct...
New
Mix format works fine if run from the cmd. I’ve followed this to facilitate the implementation into VSC which involves downloading an ext...
New
Other popular topics
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
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 couldn’t find any guides that worked well with Phoenix 1.6.0 and esbuild. I hope this helps people test the waters and eases you into t...
New
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service.
Currently when I de...
New
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
Hello, I have map which I want to convert it to string like this:
the map:
%{last_name: "tavakkoli", name: "shahryar"}
the string I ne...
New
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
New
After calling mix ecto.create I get this error:
17:00:32.162 [error] GenServer #PID<0.412.0> terminating
** (Postgrex.Error) FATAL...
New
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
Hi everyone!
I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New








