EctoResource

Eliminate boilerplate involved in defining basic CRUD functions in a Phoenix context or Elixir module.

Installation

If available in Hex, the package can be installed by adding ecto_resource to your list of dependencies in mix.exs:

def deps do
  [
    {:ecto_resource, "~> 0.1.0"}
  ]
end

Basic Usage

All examples are using Accounts. This can be substituted for a Phoenix context or any other module.

  defmodule MyApp.Accounts do
    use EctoResource

    using_repo(MyApp.Repo) do
      resource(MyApp.User)
    end
  end

This will generate the functions:

def create_user(attributes) do
  EctoResource.create(MyApp.Repo, MyApp.User, attributes)
end

def all_users(options) do
  EctoResource.all(MyApp.Repo, MyApp.User, options)
end

def get_user(id, options) do
  EctoResource.get(MyApp.Repo, MyApp.User, id, options)
end

def change_user(struct_or_changeset) do
  EctoResource.change(MyApp.User, struct_or_changeset)
end

def update_user(%MyApp.User{id: 1}, changeset) do
  EctoResource.update(MyApp.Repo, MyApp.User, struct, changeset)
end

def delete_user(struct_or_changeset) do
  EctoResource.delete(MyApp.Repo, struct_or_changeset)
end

There are also introspection functions to understand what is generated by the macro

MyApp.Accounts.__resource__(:resources) == [
  {MyApp.Repo, MyApp.User,
    [
      "all_users/1",
      "change_user/1",
      "create_user/1",
      "delete_user/1",
      "get_user/2",
      "update_user/2"
    ]}
]

Advanced usage

More granular control is available through options

:read option

defmodule MyApp.Accounts do
  use EctoResource

  using_repo(MyApp.Repo) do
    resource(User, :read)
  end
end

This will generate the functions:

def all_users(options) do
  EctoResource.all(MyApp.Repo, MyApp.User, options)
end

def get_user(id, options) do
  EctoResource.get(MyApp.Repo, MyApp.User, id, options)
end

There are also introspection functions to understand what is generated by the macro

MyApp.Accounts.__resource__(:resources) == [
  {MyApp.Repo, MyApp.User,
    [
      "all_users/1",
      "get_user/2"
    ]}
]

:write option

defmodule MyApp.Accounts do
  use EctoResource

  using_repo(MyApp.Repo) do
    resource(User, :write)
  end
end

This will generate the functions:

def create_user(attributes) do
  EctoResource.create(MyApp.Repo, MyApp.User, attributes)
end

def change_user(struct_or_changeset) do
  EctoResource.change(MyApp.User, struct_or_changeset)
end

def update_user(%MyApp.User{id: 1}, changeset) do
  EctoResource.update(MyApp.Repo, MyApp.User, struct, changeset)
end

There are also introspection functions to understand what is generated by the macro

MyApp.Accounts.__resource__(:resources) == [
  {MyApp.Repo, MyApp.User,
    [
      "change_user/1",
      "create_user/1",
      "update_user/2"
    ]}
]

:delete option

defmodule MyApp.Accounts do
  use EctoResource

  using_repo(MyApp.Repo) do
    resource(User, :delete)
  end
end

This will generate the functions:

def delete_user(struct_or_changeset) do
  EctoResource.delete(MyApp.Repo, struct_or_changeset)
end

There are also introspection functions to understand what is generated by the macro

MyApp.Accounts.__resource__(:resources) == [
  {MyApp.Repo, MyApp.User,
    [
      "delete_user/1"
    ]}
]

Documentation can be generated with ExDoc and published on HexDocs. Once published, the docs can be found at https://hexdocs.pm/ecto_resource.