-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
poc: support virtual embeds #4216
base: master
Are you sure you want to change the base?
Conversation
How would a virtual embed be loaded/dumped? What is the use case? :) |
Right. but if we are going to include it in the docs, we need to explain when this would useful. Why do you have a virtual field in this style? What is the use case? When would we recommend it? |
Virtual embeds could be useful when an Ecto schema is used primarily as application's internal data structure and in the second place for storing it somewhere. For example, I have a project where I use a schema with several embedded schemas for representing a game state that lives in a GenServer. Ecto changesets are very useful for mutating this state in response to player's actions. Only part of the state should be stored (in ETS or database) to be restored when the GenServer (re)starts. So I made some fields virtual in order to skip them when dumping the state to a storage. But I'm forced to declare embeds as virtual maps or map arrays and write special functions for casting them and tracking their changes. Another example: I cast an external API JSON data into a schema in order to make convenient transformations into internal data format and reject invalid data. Nested JSON objects are casted into embeds. This API uses almost same data structure for requests and responses, so when making a request to the API, I reuse the schema and dump it to JSON. The request structure has less fields than response, so again some fields of the schema are virtual and therefore are not dumped when I make a request. I can't use this approach if one of response embeds should not be send when using the schema for a request. |
Of course, this problems could be solved in other ways, but making embeds virtual seems more natural to me in this cases. After all, if we can mark any embedded schema field as virtual so it would not be dumped/loaded, why should embeds be an exception? |
Thanks for the clarification. I would probably model your case with different schemas but it doesn't hurt supporting this for consistency, as long as the implementation is straight-forward. @v0idpwn do you see this growing much in complexity? |
I don't think it will grow in complexity at all, will add some tests. |
We have a project that would benefit from using We have a complex embedded field that we want to display in the UI more using forms directive. Let's say we have an embed for IP, called The challenge arises with more complex scenarios, like converting a list of embeds into a simpler list of embeds that are compatible with form validations and other features, such as sorting (we are not using liveview, and unfortunately, it isn't an option in this case). Our current workaround has been to use It works, but it is not ideal (and I imagine not intended to be used in that way either). It always goes with this comment cc: @Lgdev07 |
Sorry, but that is still unclear then, if you never write it down, what is the benefit of being an embedded? It could as well be |
We are using the changesets for validations and rendering errors against the "virtual embed". You have a point about having a different schema that is not persisted, however, in that case, we would need to duplicate many other fields where we don't do this type of normalization. Using |
The question is if we should. WDYT?
Wrote this while answering a question on slack.