fireproofsocks

fireproofsocks

I’m wondering the best way to solve this particular problem: I have a schema for file uploads (think of an asset manager). The database record stores the file size, the file name, and its location. I also have a Phoenix endpoint where one can stream that file, e.g. for use as a src in an <img> tag. The route is something like /download/:id.

My question is: how to add a field to the Ecto Schema where I can populate the download url? The schema struct already knows the ID of a record, so the only thing I need to do is to build the URL using one of the Phoenix route helpers. Would this require a custom type?

Showing Posts 1 to 10

amnu3387

amnu3387

I think virtual fields are a good use case for it,
For instance:

defimpl Jason.Encoder, for: WhateverSchema do
  def encode(struct, opts) do
    struct
    |> Map.put(:avatar, RunTimeSettings.get_url_for(struct))
    # |> maybe_trim_private_fields()
    |> Jason.Encode.map(opts)
  end
end

Personally, if the full url is the only thing that makes sense for the client(s), I replace the field itself with the valid url, if the client needs/can use the field value for something, I define a virtual field and then populate that instead.

al2o3cr

al2o3cr

Do you need the data on the schema itself, or is it sufficient to include the URL when the struct is serialized to JSON? In the latter case, @amnu3387’s solution will do exactly what you need.

In the former case, things are more complicated - invoking route helpers from inside schemas will mean a circular dependency between the data layer and the Phoenix layer.

fireproofsocks

fireproofsocks OP

That makes sense – is :avatar a virtual field in that example? I haven’t tested that, but is the virtual field even necessary if you are just putting a field into a map?

And I guess for thoroughness, I would love to see an example of how to add something to the schema. E.g. the class accessor example of having a first_name and last_name field in the database, and then having a “virtual” field for full_name that concatenates the two together.

amnu3387

amnu3387

I completely misread what you’re wanting to do - that is not, indeed, useful for creating routes for use inside the app instead for providing the final link outside of it (spa, wtv). Isn’t there a helper to build urls already?

Regarding the virtual field, it was just an example, sometimes it makes sense other times not?

dimitarvp

dimitarvp

Doesn’t arc_ecto do what you need?

fireproofsocks

fireproofsocks OP

I want to know how to create accessors in Ecto – the exact use-case is secondary. The “full_name” field is a better example, perhaps.

fireproofsocks

fireproofsocks OP

Yes, there’s a helper for that use case, but as @al2o3cr mentioned, that would create a circular dependency. The primary concept I’m trying to figure out is how to introduce a calculated field on read so that the rest of the app won’t know or care that its value did not come directly from the database.

fireproofsocks

fireproofsocks OP

For a GraphQL endpoint, it was easy enough to add in the field in the resolver…

lindem

lindem

I usually have a function in my schema’s defmodule that fills virtual fields for that purpose (full name field example), which is called from functions in the context after the actual Repo interaction.

As far as I know, there is no way to have something that looks like you do map.prop but that does getter_for_prop(map) instead (like, for instance, ember computed properties or more generally ES6 getters and setters).

It looks like this (off the top of my head):

def get_person(id) do
  case Repo.get(Person, id) do
     {:ok, person} -> {:ok, Person.fill_virtual_fields(person)}
     other -> other
  end
end

(You can pipe a list of schema instances through Enum.map())

jgb-solutions

jgb-solutions

Hey! How do you deal with virtual field with relationship in Absinthe then? Say you want to fetch an Artist with their Tracks but each track as a virtual field, poster_url, to fetch to url of the poster column. What’s the easiest way to do that?

— All posts loaded —

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
peck
Hi everyone. My team and I have been working on a fairly modest app based around video streaming and chat, but we’ve landed a customer t...
New
bradley
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
unaware8150
Hello folks! So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
Cxx-mlr
I’m working on a small exercise involving update_in/3, and I came up with this solution: data = %{ name: "Periodic Table", category:...
New
Alvinkariuki
How Can I Optimise Compile Time Dependencies I have been building an elixir application for about 2 years now. Many modules and files ha...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
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

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews