EctoEnum: Ecto extension to support enums in Ecto models

EctoEnum - Ecto extension to support enums in Ecto models

This library is for defining Ecto enums (which are Ecto.Types) backed by integer, string, or user-defined types storage. User-defined types only work with PostgreSQL at the moment.

Usage looks like:

# lib/my_app/ecto_enums.ex

import EctoEnum
defenum StatusEnum, registered: 0, active: 1, inactive: 2, archived: 3

Or like:

defmodule User do
  use Ecto.Schema

  schema "users" do
    field :status, StatusEnum
  end
end

To learn more, check it out on Github.

This library has just been updated to v1.3.0 to add features like useing and string-backed enums!

Hope you guys like it I and look forward to your feedback/comments!

4 Likes

Would it be possible to have a function that we can call that checks the postgresql database’s Enum definition and makes sure it matches the defenum definition, and if there are any inconsistencies then it would return back, say, a {:error, reason} where reason is a list of the inconsistencies so we can log it or error or whatever we want (in addition to other reason codes like the Repo passed in is failing or something, :ok means successful and everything matches)?

I have to match to a pre-existing type of a backend database so I’d like to make sure I stay in sync, especially as it updates (been bit once by this). ^.^

4 Likes

That would be a really useful function. Just added the issue for your request at https://github.com/gjaldon/ecto_enum/issues/80 :slight_smile:

2 Likes