gjaldon

gjaldon

As the title states, EctoEnum has just been updated after some time of hardly any activity in the repo. Here’s the latest release: Release 0.3.2 · gjaldon/ecto_enum · GitHub

The latest release still isn’t compatible with Ecto 1.1 to 2.0. I’ll be working on that in the upcoming days. There’s been significant change in how custom Ecto types work in 1.1 so it will take a bit more work. :slight_smile:

Would appreciate any feedback! Enjoy!

First Post!

OvermindDL1

OvermindDL1

I’m looking forward. I skipped by EctoEnum because of lack of Ecto2 support so I will look at it again here soon.

It would be nice if it had a mode where it could work with native PostgreSQL Enum types as I am already using those in my app and it would be nice to have better checks for it. :slight_smile:

Most Liked

idi527

idi527

Just in case anyone is looking for a way to use postgres enum types with ecto, here’s how (without any additional dependencies).

defmodule Invoice.Status do
  @moduledoc """
  The status of payment; whether the invoice has been paid or not.

  Enumeration members:
  - `:payment_automatically_applied`
  - `:payment_complete`
  - `:payment_declined`
  - `:payment_due`
  - `:payment_past_due`
  """

  @behaviour Ecto.Type

  statuses = [
    :payment_automatically_applied,
    :payment_complete,
    :payment_declined,
    :payment_due,
    :payment_past_due
  ] |> Enum.map(fn status ->
    {status, to_string(status)}
  end)

  @doc "Returns the underlying schema type for the custom type"
  @spec type :: :invoice_status
  def type, do: :invoice_status

  @doc "Casts the given input to the custom type"
  @spec cast(atom | binary) :: {:ok, atom} | :error
  def cast(status)

  for {atom, string} <- statuses do
    def cast(unquote(atom)), do: {:ok, unquote(atom)}
    def cast(unquote(string)), do: {:ok, unquote(atom)}
  end

  def cast(_other), do: :error

  @doc "Loads the given term into a custom type"
  @spec load(binary) :: {:ok, atom}
  def load(status)

  for {atom, string} <- statuses do
    def load(unquote(string)), do: {:ok, unquote(atom)}
  end

  @doc "Dumps the given term into an Ecto native type"
  @spec dump(atom | binary) :: {:ok, binary} | :error
  def dump(status)

  for {atom, string} <- statuses do
    def dump(unquote(atom)), do: {:ok, unquote(string)}
    def dump(unquote(string)), do: {:ok, unquote(string)}
  end

  def dump(_other), do: :error
end
gjaldon

gjaldon OP

No doubt you could write your own code to create a custom Ecto type. The point of this small and focused library is to do the metaprogramming for you so don’t need to write that code every time you need to create an Enum in Ecto. With EctoEnum, you just need to write a few lines of code to have a StatusEnum, DayOfWeekEnum, etc… Also, this library actually has tests and is used by many other devs. That’s the point of having libraries and sharing packages, so we can focus on other work.

gjaldon

gjaldon OP

Oh and EctoEnum works with Postgres’s Enum type or a plain ol’ integer. It will have support for MySQL’s Enum type in the future too.

Last Post!

outlog

outlog

isn’t it in the readme already?
https://github.com/gjaldon/ecto_enum#postgres

defmodule MyApp.Repo.Migrations.AddToGenderEnum do
  use Ecto.Migration
  @disable_ddl_transaction true

  def up do
    Ecto.Migration.execute "ALTER TYPE gender ADD VALUE 'other'"
  end

  def down do
  end
end

Where Next? Top

Trending in Announcing Top

woylie
Flop is an Elixir library that applies filtering, ordering and pagination parameters to your Ecto queries. offset-based pagination with...
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
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
fuelen
Hi all! I want to present a small library which provides a mix task for generating an Entity-Relationship Diagram for Ecto schemas. You...
New
anuaralfetahe
Hello Published a new library - ProcessHub! ProcessHub is a library designed to manage process distribution within the Elixir cluster. ...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New
AstonJ
This showed up on my feed.. anyone heard of it? Just hype? Ox Alpha is a reasoning model designed for coding, sustained ag...
New
sergio
It’s not that it’s vocabulary is too advanced. It’s something worse. I get lost trying to follow even a paragraph written by Claude. It’...
New
sorenone
Today we’re releasing Oban for Python. Not an Oban client in Python. Not a pythonx wrapper embedded in Elixir. Nope, it’s a fully operati...
New
akoutmos
@hugobarauna, Dr. Dimitrios Koutmos (my brother) and I (Alex Koutmos) have been hard at work on writing a book on how you can use Elixir ...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews