Dynamic schemas at runtime

469 views
Skip to first unread message

Lars Wikman

unread,
Mar 17, 2021, 4:57:20 AM3/17/21
to elixi...@googlegroups.com
Hey

So I've been wanting to do something in the CMS space around Elixir and I've tried to figure out a good way for application users to define schemas. The use case being that the user can define a content type such as Post or Page. And I want them to be dynamically manageable at runtime.

Since schemas in Ecto are Modules, creating them dynamically from user input would often be a bad idea (atom exhaustion, memory usage, probably more reasons).

Currently it looks like I could basically create a DynamicSchema module and have that generate a fake struct at runtime with the right keys, based on some kind of definition.

However Ecto also relies on the schema module to provide __schema__ functions. And those never really receive the schema struct, rather they provide information set at compile time.

This backs my solution into a corner of using something like the Process dictionary to set up a reference to the definition when instantiating my dynamic schema to make these functions behave differently for different structs. Which breaks at any process boundary. But if these __schema__ functions had more context it would be possible to do this.

It is a bit far out compared to most Ecto usage, so I get if it doesn't make sense to attempt to support doing this. But I think it has potential to do some cool stuff without needing to skip Ecto schemas.

Blog post to come with a PoC library probably. Using the Process dictionary.

-Lars Wikman

José Valim

unread,
Mar 17, 2021, 5:09:40 AM3/17/21
to elixi...@googlegroups.com
Hi Lars,

Note a lot of the functionality in Ecto can work without schemas. Changesets allow you to pass tuples instead of schemas and queries allow you to pass binaries too. The only thing not immediately supported is dynamic schemas with associations - but if that's an issue, it may be easier to address that than trying to hijack of the __schema__ functions, some of which are private, and may break at any time!

--
You received this message because you are subscribed to the Google Groups "elixir-ecto" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-ecto...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-ecto/CADqW%2BqkzkjUXULsO%3DEhAKmqNunPB4tTxZXqnWw_Xr%2B_Yc8S7Kg%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages