AE
Ash Elixirβ€’2y ago
kernel

pattern to use for embedded resources with variable fields?

I'm brainstorming how I'd be able to have embedded resources that have variable schemas, lets say I have a SocialMediaAccount resource, and I want an 'adapter_config' column that will be a set of config to integrate with the SocialMediaAccount, i.e: twitter api keys etc I'm going to implement an adapter pattern type thing that will include validations and a few methods to integrate with the social platform, I'm just wondering how I'd define that column in my resource? I was thinking of just using a map type - then handling validations etc in a custom change / validation?
10 Replies
kernel
kernelOPβ€’2y ago
then it just saves to jsonb when I save, I'm just wondering if there's a nicer way to do it could also just KISS and pattern match different functions based on the type of social media account will encrypt api tokens etc within the jsonb, so will already need a custom changeset to handle that it would be nice to be able to use an EmbeddedResource and have them automatically swap out based on a column on the SocialMediaAccount resource, I can get a nice DSL to define validations / lifecycles
ZachDaniel
ZachDanielβ€’2y ago
Have you looked into the new union type? Sounds like it’s exactly what you want
kernel
kernelOPβ€’2y ago
yup it is
ZachDaniel
ZachDanielβ€’2y ago
Not that new actually I guess
kernel
kernelOPβ€’2y ago
just opened the docs πŸ™‚ new for me lol easy to plug in a custom change to encrypt certain values?
ZachDaniel
ZachDanielβ€’2y ago
Yeah, since the destination type can be an embedded resource We use the primary actions on the target resource to instantiate things So you could add a change to the primary create action
kernel
kernelOPβ€’2y ago
easy, so override read to decrypt stuff and create / update to encyrpt
ZachDaniel
ZachDanielβ€’2y ago
I’d suggest adding calculations to decrypt
kernel
kernelOPβ€’2y ago
right makes sense then I just don't load them if I don't actually need to use them can even get fancy with the new field policies 😜 assuming they work on calculations?
ZachDaniel
ZachDanielβ€’2y ago
Yep!

Did you find this page helpful?