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:
-
Schema-less data (Map -> JSON -> Map)
- Set
versionsfield to:maptype in Ecto and that will be interpreted asJSONBin Postgres; - Implement nested
versionsdata validations onPhotochangeset level.
- Set
-
Embedded schema (Schema -> JSON -> schema)
- define embedded
Versionsschema with full list of versions - all data validations functions are invoking at
Versionschangeset level
- define embedded
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?
Popular in Questions
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
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
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
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
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
Background
Let’s assume I have a typical GenServer that receives messages as requests, does some operation in a DB and returns responses....
New
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
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
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
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
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
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
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
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
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...
New
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
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
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
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
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







