README.md

# EctoResource

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

## Installation

If [available in Hex](https://hex.pm/docs/publish), the package can be installed
by adding `ecto_resource` to your list of dependencies in `mix.exs`:

```elixir
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.

```elixir
  defmodule Accounts do
    use EctoResource

    using_repo(Repo) do
      resource(User)
    end
  end
```

This will generate the functions:

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

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

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

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

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

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

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

```elixir
Accounts.__resource__(:resources) == [
  {Repo, 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

```elixir
defmodule Accounts do
  use EctoResource

  using_repo(Repo) do
    resource(User, :read)
  end
end
```

This will generate the functions:

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

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

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

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

## :write

```elixir
defmodule Accounts do
  use EctoResource

  using_repo(Repo) do
    resource(User, :write)
  end
end
```

This will generate the functions:

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

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

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

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

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

## :delete

```elixir
defmodule Accounts do
  use EctoResource

  using_repo(Repo) do
    resource(User, :delete)
  end
end
```

This will generate the functions:

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

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

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

## :only

```elixir
defmodule Accounts do
  use EctoResource

  using_repo(Repo) do
    resource(User, only: [:change])
  end
```

This will generate the functions:

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

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

```elixir
Accounts.__resource__(:resources) == [
  {Repo, User,
    [
      "change_user/1"
    ]}
]
```

## :except

```elixir
defmodule Accounts do
  use EctoResource

  using_repo(Repo) do
    resource(User, except: [:change])
  end
```

This will generate the functions:

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

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

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

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

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

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

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

Documentation can be generated with [ExDoc](https://github.com/elixir-lang/ex_doc)
and published on [HexDocs](https://hexdocs.pm). Once published, the docs can
be found at [https://hexdocs.pm/ecto_resource](https://hexdocs.pm/ecto_resource).