tanweerdev
Dynamic embedded schema based on type
I have a json/map field in database which can actually holds 3 kinds of objects based on type field define in table
so structure is like this
field(:type, :string) => user/player/admin
field(:object, :map) => embedded schema needed here. and it can be any of the three schemas ie user/admin/player
So is this possible I can have the flexibility of mysql and beauty/support of Ecto embeds.
Marked As Solved
mathieuprog
Check out this library:
Also Liked
mathieuprog
That’s one way to achieve it. If the frontend calls the same service for handling different kind of data, then it makes sense that it is included by the frontend. Otherwise you may also pick the right embed on the server.
Note also that it is hard or probably impossible to have the structure that you have shown, i.e. have the type stored only in a separate field. That’s because when using a custom Ecto Type for the field (storing the dynamic embed), you do not have access to the other fields of the schema. So for example, if you want to load the data from the DB, in order to load the right embed struct you really need to have that type field living with your data.







