ecto_resource v0.1.5 EctoResource

This module represents the generic CRUD functionality that is boilerplate within Phoenix context files. It provides a DSL to easily generate the basic functions for a schema. This allows the context to focus on interesting, atypical implementations rather than the redundent, drifting crud functions.

Link to this section Summary

Functions

Returns a list of all the records for a given Schema.

Creates a changeset for a given Schema

Creates a new record with the given changeset

Deletes a given Schema struct

Gets a Schema struct by the given id. Can also take options to alter the resulting query.

Return a map of actions with the function names, descriptions

Get an underscored module name for use in generating functions.

Updates a given Schema with the given attributes

Link to this section Functions

Link to this function

all(repo, schema, options \\ [])

all(Ecto.Repo.t(), module(), term()) :: [Ecto.Schema.t()]

Returns a list of all the records for a given Schema.

Examples

iex> all(Repo, User)

[
  %User{
    __meta__: #Ecto.Schema.Metadata<:loaded, "users">,
    id: 1,
    inserted_at: ~N[2019-08-15 18:52:50],
    email: "example@example.com",
    updated_at: ~N[2019-08-15 18:52:50]
  }
]
Link to this function

change(schema, changable)

Creates a changeset for a given Schema

iex> change(%User{email: "user@example.com"}, %{email: "updated-email@example.com"})

#Ecto.Changeset<
  action: nil,
  changes: %{},
  errors: [],
  data: #Engine.BusinessPartners.BusinessPartner<>,
  valid?: true
>
Link to this function

create(repo, schema, attributes)

create(Ecto.Repo.t(), module(), map()) ::
  {:ok, Ecto.Schema.t()} | {:error, Ecto.Changeset.t()}

Creates a new record with the given changeset

Examples

iex> create(Repo, User, %{email: "user@example.com"})

{:ok,
 %User{
   __meta__: #Ecto.Schema.Metadata<:loaded, "users">,
   inserted_at: ~N[2019-08-17 00:41:41],
   email: "example@example.com",
   updated_at: ~N[2019-08-17 00:41:41]
 }}
Link to this function

delete(repo, deletable)

delete(Ecto.Repo.t(), Ecto.Schema.t()) ::
  {:ok, Ecto.Schema.t()} | {:error, Ecto.Changeset.t()}

Deletes a given Schema struct

Examples

iex> delete(Repo, %User{id: 1})

{:ok,
 %User{
   __meta__: #Ecto.Schema.Metadata<:deleted, "users">,
   id: 1,
   inserted_at: ~N[2019-08-17 00:41:41],
   email: "example@example.com",
   updated_at: ~N[2019-08-17 00:41:41]
 }}
Link to this function

get(repo, schema, id, options \\ [])

get(Ecto.Repo.t(), module(), term(), term()) :: Ecto.Schema.t() | nil

Gets a Schema struct by the given id. Can also take options to alter the resulting query.

Examples

iex> get(Repo, User, 1)

%User{
  __meta__: #Ecto.Schema.Metadata<:loaded, "users">,
  id: 1,
  inserted_at: ~N[2019-08-15 18:52:50],
  email: "example@example.com",
  updated_at: ~N[2019-08-15 18:52:50]
}
Link to this macro

resource(schema, options \\ [only: [:all, :change, :create, :delete, :get, :update]])

(macro)
Link to this function

resource_actions(suffix, arg2)

Return a map of actions with the function names, descriptions

Examples

iex> resource_actions("_suffix", only: [:change])

%{
  change: %{
    name: :change_suffix,
    description: "change_suffix/1"
  }
}

iex> resource_actions("_suffix", except: [:change])

%{
  all: %{
    name: :all_suffix,
    description: "all_suffix/1"
  },

  create: %{
    name: :create_suffix,
    description: "create_suffix/1"
  },

  delete: %{
    name: :delete_suffix,
    description: "delete_suffix/1"
  },

  get: %{
    name: :get_suffix,
    description: "get_suffix/2"
  },

  update: %{
    name: :update_suffix,
    description: "update_suffix/2"
  }
}

iex> resource_actions("_suffix", :read)

%{
  all: %{
    name: :all_suffix,
    description: "all_suffix/1"
  },

  get: %{
    name: :get_suffix,
    description: "get_suffix/2"
  }
}

iex> resource_actions("_suffix", :write)

%{
  change: %{
    name: :change_suffix,
    description: "change_suffix/1"
  },

  create: %{
    name: :create_suffix,
    description: "create_suffix/1"
  },

  update: %{
    name: :update_suffix,
    description: "update_suffix/2"
  }
}

iex> resource_actions("_suffix", :delete)

%{
  delete: %{
    name: :delete_suffix,
    description: "delete_suffix/1"
  }
}
Link to this function

underscore_module_name(module)

underscore_module_name(module()) :: String.t()

Get an underscored module name for use in generating functions.

Examples

iex> underscore_module_name(User)

"user"
Link to this function

update(repo, schema, updateable, attributes)

update(Ecto.Repo.t(), module(), Ecto.Schema.t(), map()) ::
  {:ok, Ecto.Schema.t()} | {:error, Ecto.Changeset.t()}

Updates a given Schema with the given attributes

Examples

iex> update(Repo, User, %User{id: 1}, %{email: "updated@example.com"})

{:ok,
 %User{
    __meta__: #Ecto.Schema.Metadata<:loaded, "users">,
    id: 1,
    inserted_at: ~N[2019-08-15 18:52:50],
    email: "updated@example.com",
    updated_at: ~N[2019-08-17 01:16:01]
 }}
Link to this macro

using_repo(repo, list)

(macro)