mathieuprog

mathieuprog

Polymorphic_embed - polymorphic Embeds in Ecto

Hello :wave:

I published a library that brings support for polymorphic/dynamic embeds in Ecto.

Ecto’s embeds_one macro requires a specific schema module to be specified. This library removes this restriction by dynamically determining which schema to use, based on data to be stored (from a form or API) and retrieved (from the data source).

Example use case:

Say you have a Reminder schema. A reminder has a set date and text, but also specific fields whether it is an email reminder or sms reminder. Instead of adding and mixing all the email- and sms-related fields together in the Reminder schema, where one set of fields or the other are null values, you can add a polymorphic embed, only containing the relevant sms or email fields. The polymorphic embed also supports its own changeset validations.

See example code and usage in Readme file :point_down:

Inspired by ecto_poly library.

Most Liked

mathieuprog

mathieuprog

  • Support for LiveView forms has been added thanks to the quality contribution of Mathias!

  • Support for primary keys has been added

The latter may introduce breaking changes, therefore Polymoprhic Embed version has been bumped to 2.0.0

Migration from 1.x to 2.x is easy:

  • Make sure that every existing polymorphic embedded_schema contains the setting @primary_key false
mathieuprog

mathieuprog

I imagine this is a pretty common need, and that many codebases would either have a schema with sets of null values, or end up having their own custom Ecto type implementations.

This library indeed provides a custom Ecto Type that determines the right embed schema to use, based on params to cast. Nice thing is, you can tell PolymorphicEmbed that the presence of specific params identifies a certain schema – no need for a “type” field in the params:)

In addition to the advantages that a library offers (get rid of boilerplate code in the app, reusable code across projects, bugfixes, …), it comes with some nice features:

  • As mentioned, detect which types to use for the data being cast-ed, based on fields present in the data (no need for a type field in the data)
  • Run changeset validations when a changeset/2 function is present (when absent, the library will introspect the fields to cast)
  • Support for nested polymorphic embeds
  • Support for nested embeds_one/embeds_many embeds
  • Display form inputs for polymorphic embeds in Phoenix templates

Added those features in the Readme file.

mathieuprog

mathieuprog

Version 3.0.0 has been released!

We now encourage the use of polymorphic_embeds_one/2 and polymorphic_embeds_many/2 macros to define your polymorphic embeds.

If you’re curious as to why: a field containing a polymorphic list of embeds defaulted to nil before version 3. To follow embeds_many/3 we should default to []. This can only be done by forcing the :default option of the field/3 to [] (ie we can’t configure this in the Type). I added the new macros in order to set these kind of defaults setting up the field for the polymorphic embeds.

Thanks to Alexandre @Matsa59 for his contribution which led to this update!

thomas.fortes

thomas.fortes

Awesome, I’ve been working exactly with this problem this past week, my solution was built with an Ecto custom type, it works perfectly, but there’s a lot of boilerplate…

From the docs it seems like it will be almost a plug and play with just some minor adjustments, will open a branch and try it :slight_smile:

Edit: Yeah, looking at the source of the lib it does what I was doing manually with a custom type, it took zero modifications in the code aside of changing the field type in the schema and configuring the lib, so far all the tests passed.

mathieuprog

mathieuprog

Support for ecto_sqlite3 has been added in version 1.7.0!

Other features added:

  • :with option allowing to specify a custom changeset;
  • :required option to specify whether the embed is required or not;
  • traverse_errors/2 function to include polymorphic embeds’ errors.

Where Next?

Popular in Libraries Top

tompave
Hello there, I would like to share a feature toggles library (AKA feature flags) I’ve been working on. The main package is FunWithFlags...
New
woylie
I released Doggo, a collection of unstyled Phoenix components. Features Unstyled Phoenix components. Storybook that can be added to...
New
OvermindDL1
Been making an MLElixir thing (not released yet…) for fun in spare time in the past day. I’m just trying to see how much I can get an ML...
132 13487 106
New
benlime
LiveMotion enables high performance animations declared on the server and run on the client. As a follow up to my previous thread A libr...
New
MRdotB
I needed to reuse React components from my Chrome extension in my Phoenix/LiveView backend. I noticed that for Svelte/Vue, there are live...
New
ericlathrop
I built a silly site for Halloween that uses Phoenix Channels on the backend, and React on the frontend. I had many problems integrating ...
New
bryanjos
Hi, I just published version 0.23.0 of Elixirscript. Most of the changes are around JavaScript interop now that Elixirscript uses the ...
New
vic
Expat is a tiny experiment I did for extracting patterns and being able to reuse them (compose and share patterns between elixir librarie...
New
mischov
import Meeseeks.CSS html = HTTPoison.get!("https://news.ycombinator.com/").body for story <- Meeseeks.all(html, css("tr.athing")) do...
New
maltoe
Hello! Came here to announce ChromicPDF, a pet project PDF generator I’ve been working on for the past few months. Why another PDF gener...
New

Other popular topics Top

shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
William
I would like to know that is there any online source for learning Phoenix Framework for building E-Commerce Store? Any advantage on build...
New
lessless
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
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 49522 488
New
polypush135
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
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
273 38985 115
New

Sub Categories:

We're in Beta

About us Mission Statement