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
versions
field to:map
type in Ecto and that will be interpreted asJSONB
in Postgres; - Implement nested
versions
data validations onPhoto
changeset level.
- Set
-
Embedded schema (Schema -> JSON -> schema)
- define embedded
Versions
schema with full list of versions - all data validations functions are invoking at
Versions
changeset 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?