jkbbwr

jkbbwr

So I have found a pothole and I am not sure what the solution is.

I have a pretty standard JSON api in phoenix, I am using ecto, and I am mapping those to maps inside the ThingJSON this is all pretty standard.

Now depending on the route, the controller might decide it doesn’t want to preload some data. For example

%Aos.Schema.Shipyard{
  __meta__: #Ecto.Schema.Metadata<:loaded, "shipyard">,
  id: "103b0451-7396-4f11-9cf8-7081c1829f5e",
  port_id: "a748b9d2-40ff-4f14-8c8c-258b343145eb",
  port: %Aos.Schema.Port{
    __meta__: #Ecto.Schema.Metadata<:loaded, "port">,
    id: "a748b9d2-40ff-4f14-8c8c-258b343145eb",
    name: "London",
    shortcode: "lond",
    destinations: #Ecto.Association.NotLoaded<association :destinations is not loaded>,
    ships: #Ecto.Association.NotLoaded<association :ships is not loaded>,
    shipyard: #Ecto.Association.NotLoaded<association :shipyard is not loaded>,
    agents: #Ecto.Association.NotLoaded<association :agents is not loaded>,
    inserted_at: ~U[2024-07-01 02:02:57Z],
    updated_at: ~U[2024-07-01 02:02:57Z]
  },
  ships: [
    %Aos.Schema.ShipyardStock{
      __meta__: #Ecto.Schema.Metadata<:loaded, "shipyard_stock">,
      id: "8117b318-b0ef-4aa7-be31-88437acbe8fc",
      shipyard_id: "103b0451-7396-4f11-9cf8-7081c1829f5e",
      shipyard: #Ecto.Association.NotLoaded<association :shipyard is not loaded>,
      ship_id: "0a238468-1cf0-47b4-b891-d5936d6554a8",
      ship: #Ecto.Association.NotLoaded<association :ship is not loaded>,
      cost: 100,
      inserted_at: ~U[2024-07-01 02:02:57Z],
      updated_at: ~U[2024-07-01 02:02:57Z]
    }
  ],
  inserted_at: ~U[2024-07-01 02:02:57Z],
  updated_at: ~U[2024-07-01 02:02:57Z]
}

On one route it might want to preload the :ships but on another it might not want to.

I have some pretty standard view code that looks like this


  def render("shipyards.json", %{page: page}) do
    %{
      data: for(yard <- page.entries, do: shipyard(yard)),
      meta: %{
        page_number: page.page_number,
        page_size: page.page_size,
        total_entries: page.total_entries,
        total_pages: page.total_pages
      }
    }
  end

  def shipyard(shipyard) do
    %{
      id: shipyard.id,
      port: PortJSON.port(shipyard.port)
    }
  end

Now I want to modify the shipyard function to also display the ships but only if the assoc is actually preloaded.

Okay so I think

  def shipyard(shipyard) do
    %{
      id: shipyard.id,
      port: PortJSON.port(shipyard.port),
      ships:
        if Ecto.assoc_loaded?(shipyard.ships) do
          for(entry <- shipyard.ships, do: stock(entry))
        else
          # ???
        end
    }
  end

Problem is, if ships is not loaded it will export json of ships: null which is misleading IMO. I’d rather it just omitted the key.

I could do something like this

  def render_if(map, key, assoc, render) do
    if Ecto.assoc_loaded?(assoc) do
      Map.put(map, key, render.(assoc))
    else
      map
    end
  end

  def shipyard(shipyard) do
    %{
      id: shipyard.id,
      port: PortJSON.port(shipyard.port)
    }
    |> render_if(:ships, shipyard.ships, &stock/1)
  end

But it feels kinda wrong.

Also why is Ecto.assoc_loaded? not available as a guard.

Anyone got any ideas?

Showing Posts 1 to 3

tfwright

tfwright

I don’t think there’s anything wrong with the way you’re handling it. I have done similar although I had a util that constructed the json with variable guards, among them filtering out unloaded associations–something like this:

def serialize(data) do
  Map.flat_map(data, fn 
    {field, %Ecto.AssocNotLoaded{}} -> []
    {field, assoc = %{}} -> [{field, serialize(assoc)}]
    {field, val} -> [{field, val}]
  end)
  # other selecting/filtering...
end
dimitarvp

dimitarvp

You can pattern match on Ecto.AssocNotLoaded and then have the map construction not include the key in that clause, or you can put nil and then have Enum.reject that deletes any map pairs with a nil value. Either one would be just fine.

gregvaughn

gregvaughn

I’d likely use a filter or guard is_list(shipyard.ships)

— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
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
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
subsaharancoder
I’ve followed the Phoenix LiveView file upload code here Uploads — Phoenix LiveView v1.0.0-rc.7 and so far everything works just fine wit...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
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
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
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
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New

Latest on Elixir Forum

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews