ecto_shorts v0.1.4 EctoShorts.Actions View Source

Actions for crud in ecto, this can be used by all schemas/queries

Link to this section Summary

Functions

Gets a collection of schemas from the database

Gets a collection of schemas from the database but allows for a filter

Creates a schema with given params

Deletes a schema

Deletes a schema

Finds a schema with matching params

Gets a schema from the database

Gets a collection of schemas from the database but allows for a filter

Updates a schema with given updates

Link to this section Types

Link to this type

aggregate_options() View Source
aggregate_options() :: :avg | :count | :max | :min | :sum

Link to this type

filter_params() View Source
filter_params() :: Keyword.t() | map()

Link to this type

query() View Source
query() :: Ecto.Query | Ecto.Schema

Link to this type

schema_list() View Source
schema_list() :: [Ecto.Schema.t()] | []

Link to this type

schema_res() View Source
schema_res() :: {:ok, Ecto.Schema.t()} | {:error, String.t()}

Link to this section Functions

Link to this function

aggregate(schema, params, aggregate, field, opts \\ []) View Source
aggregate(
  queryable :: query(),
  params :: filter_params(),
  agg_opts :: aggregate_options(),
  field :: atom(),
  opts :: Keyword.t()
) :: term()

Link to this function

all(query) View Source
all(queryable :: query()) :: schema_list()

Gets a collection of schemas from the database

Examples

iex> EctoSchemas.Actions.all(EctoSchemas.Accounts.User)
[]
Link to this function

all(query, params) View Source
all(queryable :: query(), params :: filter_params()) :: schema_list()

Gets a collection of schemas from the database but allows for a filter

Examples

iex> Enum.each(1..4, fn _ -> create_user() end)
iex> length(EctoSchemas.Actions.all(EctoSchemas.Accounts.User, first: 3)) === 3
true
Link to this function

create(schema, params) View Source
create(schema :: Ecto.Schema.t(), params :: filter_params()) ::
  {:ok, Ecto.Schema.t()} | {:error, Ecto.Changeset.t()}

Creates a schema with given params

Examples

iex> {:ok, schema} = EctoSchemas.Actions.create(EctoSchemas.Accounts.User, user_params(first_name: "TEST"))
iex> schema.first_name
"TEST"
iex> {:error, changeset} = EctoSchemas.Actions.create(EctoSchemas.Accounts.User, Map.delete(user_params(), :first_name))
iex> "can't be blank" in errors_on(changeset).first_name
true
Link to this function

delete(schema_data) View Source
delete(schema_data :: Ecto.Schema.t()) ::
  {:ok, Ecto.Schema.t()} | {:error, Ecto.Changeset.t()}

Deletes a schema

Examples

iex> user = create_user()
iex> {:ok, schema} = EctoSchemas.Actions.delete(user)
iex> schema.first_name === user.first_name
true
Link to this function

delete(schema, id) View Source
delete(schema :: Ecto.Schema.t(), id :: integer()) ::
  {:ok, Ecto.Schema.t()} | {:error, Ecto.Changeset.t()}

Deletes a schema

Examples

iex> user = create_user()
iex> {:ok, schema} = EctoSchemas.Actions.delete(EctoSchemas.Accounts.User, user.id)
iex> schema.first_name === user.first_name
true
Link to this function

find(query, params) View Source
find(queryable :: query(), params :: filter_params()) :: schema_res()

Finds a schema with matching params

Examples

iex> user = create_user()
iex> {:ok, schema} = EctoSchemas.Actions.find(EctoSchemas.Accounts.User, first_name: user.first_name)
iex> schema.first_name === user.first_name
true
Link to this function

get(schema, id) View Source
get(queryable :: query(), id :: term()) :: Ecto.Schema.t() | nil

Gets a schema from the database

Examples

iex> user = create_user()
iex> %{id: id} = EctoSchemas.Actions.get(EctoSchemas.Accounts.User, user.id)
iex> id === user.id
true
iex> EctoSchemas.Actions.get(EctoSchemas.Accounts.User, 2504390) # ID nonexistant
nil
Link to this function

stream(query, params) View Source
stream(queryable :: query(), params :: filter_params()) :: Enum.t()

Gets a collection of schemas from the database but allows for a filter

Link to this function

update(schema, schema_id, updates) View Source
update(
  schema :: Ecto.Schema.t(),
  id :: integer(),
  updates :: map() | Keyword.t()
) :: {:ok, Ecto.Schema.t()} | {:error, Ecto.Changeset.t()}
update(schema :: Ecto.Schema.t(), schema_data :: map(), updates :: Keyword.t()) ::
  {:ok, Ecto.Schema.t()} | {:error, Ecto.Changeset.t()}
update(schema :: module(), schema_data :: Ecto.Schema.t(), updates :: map()) ::
  {:ok, Ecto.Schema.t()} | {:error, Ecto.Changeset.t()}

Updates a schema with given updates

Examples

iex> user = create_user()
iex> {:ok, schema} = EctoSchemas.Actions.update(EctoSchemas.Accounts.User, user, first_name: user.first_name)
iex> schema.first_name === user.first_name
true