CRUD behaviour (crud v1.0.0)

A module for easy access to the database.

Link to this section Summary

Callbacks

Adds a new entity to the database

Takes in parameters

Removes a structure from the database

Takes in parameters

Finds a list of elements from the database by matching part of a string to one of the item's fields in the pattern

Takes in parameters

Retrieves structure from DB

Takes in parameters

Gets a list of structures from the database

Takes in parameters

Changes the structure in the database

Takes in parameters

Link to this section Callbacks

Specs

add(mod :: Module.t(), opts :: List.t() | Map.t()) ::
  {:ok, Ecto.Schema.t()} | {:error, List.t()} | {:error, String.t()}
Adds a new entity to the database

Takes in parameters:

  • CRUD.add/2:
    • mod: Module
    • opts: Map or paramatras key: value separated by commas

Returns {:ok, struct} or {:error, error as a string or list of errors}

Examples

iex> MyApp.CRUD.add(MyApp.MyModule, %{key1: value1, key2: value2})
or
iex> MyApp.CRUD.add(MyApp.MyModule, key1: value1, key2: value)

Specs

delete(item :: Ecto.Schema.t()) ::
  {:ok, Ecto.Schema.t()} | {:error, List.t()} | {:error, String.t()}
Removes a structure from the database

Takes in parameters:

  • CRUD.delete/1:
    • item: struct
  • CRUD.delete/2:
    • mod: Module
    • id: Structure identifier in the database

Returns {:ok, struct} or {:error, error as a string or list of errors}

Examples

iex> MyApp.CRUD.delete(struct)
or
iex> MyApp.CRUD.delete(MyApp.MyModule, 1)
Link to this callback

delete(mod, id)

Specs

delete(mod :: Module.t(), id :: Integer.t() | String.t()) ::
  {:ok, Ecto.Schema.t()} | {:error, List.t()} | {:error, String.t()}
Link to this callback

find(mod, opts)

Specs

find(mod :: Module.t(), opts :: List.t() | Map.t()) :: {:ok, List.t()}
Finds a list of elements from the database by matching part of a string to one of the item's fields in the pattern

Takes in parameters:

  • CRUD.find/2:
    • mod: Module
    • opts: Map or paramatras key: value separated by commas

Returns {:ok, list of structures}

Examples

iex> MyApp.CRUD.find(MyApp.MyModule, %{key: "sample"})
or
iex> MyApp.CRUD.find(MyApp.MyModule, key: "sample")

Specs

get(mod :: Module.t(), id :: String.t() | Integer.t()) ::
  {:ok, Ecto.Schema.t()} | {:error, List.t()} | {:error, String.t()}
get(mod :: Module.t(), opts :: List.t() | Map.t()) ::
  {:ok, Ecto.Schema.t()} | {:error, List.t()} | {:error, String.t()}
Retrieves structure from DB

Takes in parameters:

  • CRUD.get/2:
    • mod: Module
    • id: Structure identifier in the database
  • CRUD.get/2:
    • mod: Module
    • opts: Map or paramatras key: value separated by commas

Returns {:ok, struct} or {:error, error as a string}

Examples

iex> MyApp.CRUD.get(MyApp.MyModule, 1)
or
iex> MyApp.CRUD.add(MyApp.MyModule, "8892da9e-9cb5-49dd-922c-7371083cdd85")
or
iex> MyApp.CRUD.add(MyApp.MyModule, id: 1)
or
iex> MyApp.CRUD.add(MyApp.MyModule, %{id: 1})

Specs

get_all(mod :: Module.t()) :: {:ok, List.t()}
Gets a list of structures from the database

Takes in parameters:

  • CRUD.get_all/1:
    • mod: Module
  • CRUD.get_all/2:
    • mod: Module
    • limit: Number of items to display
  • CRUD.get_all/2:
    • mod: Module
    • opts: Map or paramatras key: value separated by commas
  • CRUD.get_all/3:
    • mod: Module
    • limit: Number of items to display
    • offset: Number of elements at the beginning of the list to skip
  • CRUD.get_all/3:
    • mod: Module
    • limit: Number of items to display
    • opts: Map or paramatras key: value separated by commas
  • CRUD.get_all/4:
    • mod: Module
    • limit: Number of items to display
    • offset: Number of elements at the beginning of the list to skip
    • opts: Map or paramatras key: value separated by commas

Returns {:ok, list}

Examples

iex> MyApp.CRUD.get_all(MyApp.MyModule)
or
iex> MyApp.CRUD.get_all(MyApp.MyModule, 200)
or
iex> MyApp.CRUD.get_all(MyApp.MyModule, status: 1)
or
iex> MyApp.CRUD.get_all(MyApp.MyModule, %{status: 1})
or
iex> MyApp.CRUD.get_all(MyApp.MyModule, 200, 50)
or
iex> MyApp.CRUD.get_all(MyApp.MyModule, 200, status: 1)
or
iex> MyApp.CRUD.get_all(MyApp.MyModule, 200, %{status: 1})
or
iex> MyApp.CRUD.get_all(MyApp.MyModule, 200, 50, status: 1)
or
iex> MyApp.CRUD.get_all(MyApp.MyModule, 200, 50, %{status: 1})
Link to this callback

get_all(mod, opts)

Specs

get_all(mod :: Module.t(), opts :: List.t() | Map.t()) :: {:ok, List.t()}
get_all(mod :: Module.t(), limit :: Integer.t()) :: {:ok, List.t()}
Link to this callback

get_all(mod, limit, offset)

Specs

get_all(mod :: Module.t(), limit :: Integer.t(), offset :: Integer.t()) ::
  {:ok, List.t()}
get_all(mod :: Module.t(), limit :: Integer.t(), opts :: List.t() | Map.t()) ::
  {:ok, List.t()}
Link to this callback

get_all(mod, limit, offset, opts)

Specs

get_all(
  mod :: Module.t(),
  limit :: Integer.t(),
  offset :: Integer.t(),
  opts :: List.t() | Map.t()
) :: {:ok, List.t()}
Link to this callback

update(item, opts)

Specs

update(item :: Ecto.Schema.t(), opts :: List.t() | Map.t()) ::
  {:ok, Ecto.Schema.t()} | {:error, List.t()} | {:error, String.t()}
Changes the structure in the database

Takes in parameters:

  • CRUD.update/2:
    • item: struct
    • opts: Map or paramatras key: value separated by commas
  • CRUD.update/3:
    • mod: Module
    • id: Structure identifier in the database
    • opts: Map or paramatras key: value separated by commas
  • CRUD.update/4:
    • mod: Module
    • key: Name of one of the fields in the structure
    • val: Field value
    • opts: Map or paramatras key: value separated by commas

Returns {:ok, struct} or {:error, error as a string or list of errors}

Examples

iex> MyApp.CRUD.update(struct, %{key1: value1, key2: value2})
or
iex> MyApp.CRUD.update(struct, key1: value1, key2: value)
or
iex> MyApp.CRUD.update(MyApp.MyModule, 5, %{key1: value1, key2: value})
or
iex> MyApp.CRUD.update(MyApp.MyModule, 5, key1: value1, key2: value)
or
iex> MyApp.CRUD.update(MyApp.MyModule, :id, 5, %{key1: value1, key2: value})
or
iex> MyApp.CRUD.update(MyApp.MyModule, :id, 5, key1: value1, key2: value)
Link to this callback

update(mod, id, opts)

Specs

update(
  mod :: Module.t(),
  id :: Integer.t() | String.t(),
  opts :: List.t() | Map.t()
) :: {:ok, Ecto.Schema.t()} | {:error, List.t()} | {:error, String.t()}
Link to this callback

update(mod, key, val, opts)

Specs

update(
  mod :: Module.t(),
  key :: Atom.t(),
  val :: Integer.t() | String.t(),
  opts :: List.t() | Map.t()
) :: {:ok, Ecto.Schema.t()} | {:error, List.t()} | {:error, String.t()}