ole

ole

Hello!
I am working on partial migration of an existing app to elixir. DB structure is already exists and couldn’t be easily changed.
I have next situation:
table ‘components’ has all available components in system and has field type. Types are hardcoded: html, twitter_feed, rss etc. So it’s something like a widgets.
Data for each type is saved in separated table: components_html, components_twitter_feed, components_rss etc.. And each of such table has relation with main component record with a field ‘component_id’
Schema is implemented in Component/ComponentRss/ComponentHtml etc
I have has_one relation in Component for each type:
has_one :html, ComponentHtml
has_one :rss, ComponentRss

and belongs_to in child schema:
belongs_to :component, Component

Let’s say I want to access route ‘components/:id’ and return JSON in format:

{
  id: 123,
  type: 'html',
  name: 'Html Test',
  pivot: { 
     // All data from table components_html
    id: 1,
    component_id: 123,
    html_content: '<h1>It"s html content'
 }
}

For another component it could be another pivot data from another table (dependent on type):

{
  id: 124,
  type: 'rss',
  name: 'Rss feed',
  pivot: { 
     // All data from table components_rss
    id: 1,
    component_id: 124,
    feed_url: 'https://feed.url'
 }
}

I stuck a bit how to implement reading based on type.
I can’t do it like this

Component
    |> Repo.get!(id)
    |> Repo.preload(:html)
    |> Repo.preload(:rss)

There 15 types, and with that approach it will read from all tables (but only one contains valid record).
Plus I need to make a json. Do I need to create view response function for each type of component using matching patterns?
I can’t figure out how to make some multipurpose solution to have:

%{
      id: component.id,
      type: component.type,
      pivot: %{
        component_rss_structure (json representation of ComponentRss/ComponentHtml/etc)
      }
    }

Later I will call PUT route to update record from similar structure. I need somehow to update sub-component data from ‘pivot’ structure in request. How can I dynamically choose sub-component?

Thanks in advance.

Showing Posts 1 to 2

fuelen

fuelen

I stuck a bit how to implement reading based on type.
I can’t do it like this

Component
    |> Repo.get!(id)
    |> Repo.preload(:html)
    |> Repo.preload(:rss)

but you can do this:

component = Repo.get!(Component, id)
component = Repo.preload(component, component.type)

if type doesn’t match assoc (not an atom or different naming), create a helper function type_to_assoc_name/1 that will do this mapping.

ole

ole OP

Thank you! yep, I did something like this. There is still a lot of places to improve, but it’s working already:

def get_component!(id) do
    Component
    |> Repo.get!(id)
    |> preloads()
  end

  defp preloads(component) do
    component
    |> Repo.preload(Component.get_type_as_atom(component.type))
  end

And implemented JSON for model:

def get_type_as_atom(type) do
    %{
      "html" => :html,
      "rss" => :rss
    }[type]
  end
defimpl Jason.Encoder, for: Component do
    def encode(value, opts) do
      fields = [Component.get_type_as_atom(value.type) | [:name, :type]  ]
      m = Map.take(value, fields)
      Jason.Encode.map(m, opts)
    end
  end
— All posts loaded —

Where Next? Top

Trending in Questions Top

Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
Trolleger
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
matt-savvy
Anyone here using Honeybadger? My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of Bandit.HTTPError...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews