HerrBertling

HerrBertling

Nested data structures the Phoenix way?

Hey all!

I‘m currently learning Phoenix and Elixir. My day job is working on the frontend side of things with React.

For fun, I started building a project management app for craftsmen. Different companies should use it. They‘d have projects, a calendar, overview of the latest changes,…

There’s a nested data structure that I have on the project detail page and I‘d love to get some advice on how to „think“ of this in a Phoenix-Elixir-way.

Each project has phases, each phase has tasks and each task can have appointments. I‘ve got all of these in separate data tables (another thing I never touched - data modeling on the db level :sweat_smile:) which seems to be the correct way to do it, quite a few belongs_to in there as well.

When I think about this from the UI side of things, my hunch is to add a form for „add task“ as a LiveComponent. I could of course do all of this at once in a single LiveView and render some nested data structure with different events for different forms. Or could I? Coming from Remix, I‘m used to thinking in nested routes, but that‘s unfortunately not a thing in Phoenix from what I understood so far.

Since I have almost no experience with all of this besides a few tutorials, I would love to get some feedback on how people handle these scenarios. Nested data structures have to be common scenarios, no?

Appreciate any thoughts and hints on this.

Most Liked

josevalim

josevalim

Creator of Elixir

Welcome @HerrBertling! Generally speaking, you may read data as nested but most updates happen within the data directly.

For example, if projects has phases, phases has tasks, and task have appointments, and you are showing them all on the same page, then you would likely load all of the data as nested, using Ecto’s preloads. On the other hand, if you only show appointments once you are seeing a task page, which is potentially a separate LiveView, you are likely only loading that particular task and its appointments.

When it comes to adding a new task, you most likely send only the “phase_id” to the server alongside the task parameters. Then when you receive the event, you validate the user can indeed edit and add tasks to that phase, and then you insert the task with its relevant phase_id to the database. The insertion does not need to go through the nesting.

Where Next?

Popular in Questions 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
chokchit
** (DBConnection.ConnectionError) connection not available and request was dropped from queue after 2733ms. You can configure how long re...
New
Kurisu
For example for a current url like http://localhost:4000/cosmetic/products?_utf8=✓&query=perfume&page=2, I would like to get: ...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
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
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: The documentation above suggests that while ...
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
sergio_101
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
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
rms.mrcs
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

Other popular topics Top

axelson
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...
239 48475 226
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
senggen
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
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
lessless
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
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31194 112
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 52774 488
New
sergio_101
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
romenigld
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
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54250 245
New

We're in Beta

About us Mission Statement