helicon

helicon

Postgres JSONB via Ecto

Hello!
For example i have an schema Photo.

{
  "filename": "photo123.jpg",
  "content_type": "image/jpeg",
  "versions": {
     "original": {...},
     "thumbnail": {...}
     "facebook_version": {...}, 
     # ...
     # and some more versions
     # ...
  }
}

And there is a list of versions that should be present, but some other versions are optional.

After i took a look at this video - https://www.youtube.com/watch?v=sPmHP9ZPOWc, i see 2 cases to define data structure:

  1. Schema-less data (Map -> JSON -> Map)

    • Set versions field to :map type in Ecto and that will be interpreted as JSONB in Postgres;
    • Implement nested versions data validations on Photo changeset level.
  2. Embedded schema (Schema -> JSON -> schema)

    • define embedded Versions schema with full list of versions
    • all data validations functions are invoking at Versions changeset level

Objective
I am developing API application. And there is what i need from my data structure:

  • secure and trusted data saving
  • Convenient and understandable structure for querying and sending to frontend client

The second way looks more secure. But embedded schema it’s not dynamic data. I don’t want to store empty version fields in my DB, if some optional versions are absent(actually I do not know if this is a good idea). As i understood, the dynamic fields can’t be in Ecto embedded schema. Am i wrong?

Where Next?

Popular in Questions Top

dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
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
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
Fl4m3Ph03n1x
Background Let’s assume I have a typical GenServer that receives messages as requests, does some operation in a DB and returns responses....
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
Exadra37
Sometimes I want to check if the input into a function is not a blank string. My first approach: defmodule Example do def do_stuff(s...
New
Mooodi
Given a string, how can I get access to its character by index? Enum.at("my_string", 2) doesn't work. Or rather, not char, but a substr...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New

Other popular topics Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
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
sergio
I couldn’t find any guides that worked well with Phoenix 1.6.0 and esbuild. I hope this helps people test the waters and eases you into t...
New
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 45766 226
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
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
magnetic
Hey :wave:t3: Elixir community, I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New

We're in Beta

About us Mission Statement