ecto_resource v0.1.6 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

Same as create/3 but returns the struct or raises if the changeset is invalid.

Deletes a given Schema struct

Same as delete/2 but returns the struct or raises if the changeset is invalid.

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

Same as get/4 but raises Ecto.NoResultsError if no record was found.

Get an underscored module name for use in generating functions.

Updates a given Schema with the given attributes

Same as update/4 but returns the struct or raises if the changeset is invalid.

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

create!(repo, schema, attributes)

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

Same as create/3 but returns the struct or raises if the changeset is invalid.

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

delete!(repo, deletable)

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

Same as delete/2 but returns the struct or raises if the changeset is invalid.

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 function

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

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

Same as get/4 but raises Ecto.NoResultsError if no record was found.

Link to this macro

resource(schema, options \\ [])

(macro)
Link to this function

resource_descriptions(resources)

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 function

update!(repo, schema, updateable, attributes)

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

Same as update/4 but returns the struct or raises if the changeset is invalid.

Link to this macro

using_repo(repo, list)

(macro)