bglusman
I’m curious how others in the community deal with first class enums… especially crossing boundaries like database, code logic, and absinthe? My team and I developed a home baked solution that I quite like, and am tempted to open source, though its not very much code, but I thought I’d check first what others are doiing and whether there are other solutions out there I’m not aware of…
What our solution mostly does that I like is, makes them first class, makes it easy to use them both in the database and in code, and expose as first class enums in absinthe/graphQL, supports optionally enforcing the limitation in database, supports deprecation, and supports versioning (though versioning support is pretty limited/manual and mostly tied to the DB enforcement to allow dropping the old constraint and adding the new one in a safe, DRY way…)
Are there other solutions supporting all of that, and/or more I’m not thinking of/haven’t had to deal with?
By the way happy to just share the few modules of code here as snippets instead of/in addition to “open sourcing” as a library/package, just curiious as the main point here is just curiosity/discussion on how others handle and whether its a pain point that wants tackling better than Ecto.Enum does at present… dont’ want to focus on our solution as I don’t think its anything amazing, but it works well enough for us…
Trending in Discussions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #ai
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming











Showing Posts 8 to 1- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
Eiji
It’s much simpler than only one of many optional components I’ve added to
enumex.and
are things that another optional component do.
This is a very manual version of what my code do. For
Advancedcase I’m automatically modifying all tables to support a new enum (with added or removed value). What you do only drops constraint and requires manual conversion for all tables using your constraint.Generally what you do is implement part of features from my
Simpleapproach with a bit ofAdvancedapproach code that is unfortunately very manual to use especially if a specific enum is used by multiple tables or when there is lots of enums used.So in short it’s not even 1/3 of features from
enumexpackage. Your solution lacks of automation and many helpful features to use enum onElixirside (like automatically generated guards or specification). What you show is very poorly documented, for example I use behaviours to document functions and macros defined inside my macros.Also I don’t believe
credowould be “happy” validating your code (ABCSizecheck for example). In contracts all of my features are documented (mostly in components as my package does not force any conventions), have full specification, 100% test coverage, passes all credo and dialyzer checks.bglusman
Thanks @karlosmid and @Eiji ! I had not seen either of those libraries and are exactly what I was curious about, I’ll have to dig into them… we’re not likely to replace our in-house tool because we use it extensively and it works well, but, good to be aware of/make easier for others to discover from this discussion perhaps…
For what it’s worth/in case anyone is interested, here’s the code from our in homebrewed solution, including an extension for Absinthe integration and for supporting constraints in migrations via a versioning convention… for usage, you
usethe module defining a new Enum, and then import or require your enum module to expose the macros for usage in your code…and our absinthe EnumBuilder
and middleware
We also add in our
repo.exfile, though this is purely optional/we use less now than we used toCould publish as a library, but, not a ton of code and not sure how it compares yet to above existing libraries I wasn’t aware of so just throwing out there for feedback/interest
bglusman
Oh I forgot about this library, I was talking about the built in Ecto.Enum but I had seen and probably used this library years ago becore
Ecto.Enumwas introduced in Ecto core… being able to check if a string IS a valid value is an interesting tool/usecase, though I’m not sure when I’d use it vs just patternmatching on the actual value or membership in the full list of values? Anyway, thanks for reminding me of!Eiji
https://forum.elixirforum.com/t/enumex-first-stable-release/71528
You define enum module which itself does nothing except storing compile-time data from DSL and for that module you add one or more components that uses that data to generate a function at compile-time.
I support various scenarios:
Simple - just storing an integer/string in the database - only Elixir-side validations - good for a single project
Advanced - as same as simple stores a static list of values, but using a migration and adapter you can create an enumerated type in database - validations in both Elixir in database - good for working on same db in multiple projects - limited support for databases supporting enumerated types
Custom - like Advanced, but with support for much more database like in Simple; it uses a relation instead of the enumerated type - validations in both Elixir in database - good for runtime-determined values (like user roles)
Both
SimpleandAdvancedcases (Enumex.Static) have an absinthe component and various other components as well … I believe my solution is therefore the most feature-complete.karlosmid
Hi, we are using protobuf enum definition, we put those in global protobuf folder.
Then with GenEnum – GenEnum v0.4.3
we got helpers:
axelson
GitHub - gjaldon/ecto_enum: Ecto extension to support enums in models · GitHub works pretty well for my needs. And it generates functions that allow you to use the enums elsewhere in the codebase, e.g.
bglusman
I do recall looking at typedstruct at some point and its nice it saves some typing and all, but, I’m not sure if I realized the direction it went, allowing you to use types in your struct definition as opposed to saving you tying up a type for your struct… that’s interesting… but given the direction of Elixir’s support for static typing more directly in the language, I’m not likely to start taking up a tool now that relies on the… what I’ll call deprecated type syntax? Since my understanding is there may be support for migrating that to the new type system, but the syntax and behavior will definitely be different.
But while the compile time errors there are nice, it does nothing to address use of the values elsewhere in code, as in my experience when you have enums, you need to reference specific values in multiple places, and one of the things I find inadequate about Ecto.Enum is it doesn’t help you do that in a DRY or safe way protecting from typos etc… our module (mostly) does, as long as its used “correctly”…
dogweather
My solution feels ad hoc and I’m sure I’ll be evolving it:
But I have a lot of boilerplate code that’s not DRY. I’m actually not too sure how much of it I need. I do wish this was entirely declarative, DRY, and introspectable so that I only need declare this
plan_idtype once.I do get nice IDE and compile-time error messages from the above, though if I supply an invalid plan_id: