ganymede

ganymede

How do you handle multiple changes without using multiple database requests?

How do y’all handle multiple changes without using multiple database requests. This is something I came up with but I am wondering if there’s already a solution I just haven’t found.

I made a fancy diagram but I am not allowed to post it. :frowning:

The Draft module implements an optimistic UI pattern where changes are buffered in memory and persisted as a single, atomic unit.

Component Description
%Draft{} The Draft stores the original data in source and keeps track of mutations in the changes field.
Draft.Diff Compares the difference between the original source (initial state) and changes (current state).
Draft.Validations Use Ecto Changesets to validate data before they are applied to the changes map. Batched operations are checked as a group, if any fail, the entire batch is rejected with errors collected for the UI.
Draft.Commit Atomic persistence, translates the list of ordered mutations into an Ecto.Multi. Handles tmp_id resolution for newly inserted entities.

Most Liked Responses

felix-starman

felix-starman

I apologize if this seems rude, I’m just trying to gauge what it is that you’re trying to solve or get advice on , but the shape of what you describe seems basically the same as a changeset in ecto, or any of the other parsing or validation or schema libraries such as Drops, or Zoi, or …

If it’s something that needs to be persisted before it’s committed to the main entity, then I have a concept of it in the database and it’s just a matter of whether I store a map of some embedded schema, or something more formal.

If it does not need to be persisted or a process dying like a liveview refreshed for example, then I just store it in the socket or whatever mechanism of process state I’m using and updated or recompute it with every new set of changes until it’s ready to be applied. I tend to only use an extra changeset for very simple one or two step flows, as it’s way too easy to reuse a changeset twice which is a No-No. Usually if it’s just a little bit bigger than that and I want to protect myself from messing up. I will receive the form, turn it into a changeset if it’s valid, and if they don’t want to apply it yet, then I will just capture the params they sent or the cast attributes and then just append when they want to add another thing. So I basically I don’t keep the changeset. I just use it to make a decision and then I throw it away or keep it only for calling to_form.

Once I get into UI “wizard” territory or some sort of state machine, I make a custom reducer module and then decide how to save and restore the state if I need to.

Where Next?

Popular in Questions Top

aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
jerry
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
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
alice
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
freewebwithme
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
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New

Other popular topics Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
freewebwithme
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
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New
marick
I had some trouble figuring out how to make many-to-many associations work. Once I got it working, I wrote a blog post. Because I’m a nov...
New

We're in Beta

About us Mission Statement