An Ecto extension for defining and using enumerations (enums) in Elixir schemas, supporting integer, string, and PostgreSQL enum types.
EctoEnum is an extension for the Ecto database wrapper in Elixir that adds support for enumerated types (enums) in data models. It allows developers to define custom enum types that can be used as field types in Ecto schemas, ensuring data integrity and providing a clean, type-safe interface for enum values in applications.
Elixir developers using Ecto for database interactions who need to enforce enum constraints at the application and database levels, particularly those working with PostgreSQL's native enum types.
Developers choose EctoEnum because it seamlessly integrates with Ecto's type system, offering automatic casting between atoms, strings, and integers, and provides migration helpers for PostgreSQL enum types, ensuring type safety and data integrity without manual boilerplate.
Ecto extension to support enums in models
Open-Awesome is built by the community, for the community. Submit a project, suggest an awesome list, or help improve the catalog on GitHub.
Supports multiple definition styles via defenum/2, use with EctoEnum, and PostgreSQL-specific defenum/3, allowing adaptation to different database backends and preferences.
Seamlessly casts between atoms, strings, and integers for integer-backed enums, reducing boilerplate in queries and changesets, as shown in the README's examples with Repo and cast/4.
Integrates with PostgreSQL's enum types using helper functions like create_type/0 and type/0 in migrations, enabling database-level constraints for enhanced data integrity.
Provides reflection functions such as __enum_map__/0 and valid_value?/1 for inspecting and validating enum values at runtime, aiding in debugging and dynamic checks.
Altering PostgreSQL enum values requires non-transactional migrations with @disable_ddl_transaction true, and dropping values is not straightforward, making schema changes risky and manual.
While integer and string-backed enums work with any Ecto-supported database, PostgreSQL-specific features like native enums and migration helpers are unavailable for others like MySQL or SQLite.
Managing PostgreSQL enum types in migrations can be error-prone, especially when handling different schemas or backfilling data, as admitted in the README's 'Important notes/gotchas' section.