Twfo326
How to update a key's value with Mnesia and Memento without overwriting the other values?
I’ve created a module of CRUD operations, mostly one-liners, but I’m stuck at creating a general purpose update function.
Goals:
- Update the value of any key or keys in the row, independently of each other.
- Insert and or update the
:updated_atkey. - Only update the keys specified by the update functions arguments
My approach so far has been something along these lines (pseudo code):
......
defmodule Todo do
use Memento.Table,
attributes: [:id, :name, :priority, :updated_at],
type: :ordered_set,
autoincrement: true
.....
def update(id, changes = %{}) do
Memento.transaction do
case get_by_id(id) do
%Todo{} = todo ->
todo
|> adjust( changes)
|> Map.put!(:updated_at, NaiveDateTime.utc_now()
_ ->
{:error, :not_found}
end
end
defp adjust(%Todo{} = todo, changes) do
todo
|> Map.update!(changes))
|> Todo.write()
end
def get_by_id(id) do
Memento.transaction(fn -> Memento.Query.read(Todo, id) end)
end
Elixir school demonstrates updates with Mnesia.write/1 but this overwrites the whole row.
The other solutions I found are either over my head or in Erlang:
https://stackoverflow.com/questions/63048415/elixir-mnesia-what-is-the-most-concise-way-to-update-a-set-of-value-in-an-el
https://stackoverflow.com/questions/1820996/erlang-mnesia-updating-a-single-field-value-in-a-row
Marked As Solved
Twfo326
Thanks to @sheharyarn for providing the solution over at github:
https://github.com/sheharyarn/memento/issues/30
def update(id, %{} = changes) do
Memento.transaction(fn ->
case Memento.Query.read(Todo, id, lock: :write) do
%Todo{} = todo ->
todo
|> struct(changes)
|> Map.put(:updated_at, NaiveDateTime.utc_now())
|> Memento.Query.write()
|> then(&{:ok, &1})
nil ->
{:error, :not_found}
end
end)
end
1
Popular in Questions
I have followed this StackOverflow post to install the specific version of Erlang.
And When I am running mix ecto.setup then getting fol...
New
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set?
Thanks.
New
I would like to know what is the best IDE for elixir development?
New
Hey,
I have a NanoPi-M3 and try to install Elixir on their Ubuntu image. I followed the Raspberry Pi installation instructions from the ...
New
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible.
total = 10
while total != 0
...
New
I’m currently trying to understand how to join three tables using Ecto. All the examples I’ve seen use 2, so maybe I’m just missing somet...
New
I will often find my self writing things similar to:
case some_value do
nil -> something()
"" -> something()
_ -> someth...
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
I am trying to run a deploy with docker and I successfully runned with this command:
docker build -t romenigld/blog-prod .
but when I t...
New
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
Other popular topics
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1]
15:22:35.803 [error] gen_event {lager_file_backend...
New
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!)
This post collects co...
New
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode.
The solution seems to be, in a hyphena...
New
Hi,
I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
I tried installing
elixir 1.11.2
erlang 23.3.4
via asdf in my zsh shell. Enabled the versions locally and globally.
When I list them ...
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
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
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
Hello guys,
I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
Hey :wave:t3: Elixir community,
I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New







