ecto_shorts v0.1.0 EctoShorts.Actions

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()
aggregate_options() :: :avg | :count | :max | :min | :sum

Link to this type

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

Link to this type

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

Link to this type

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

Link to this type

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

Link to this section Functions

Link to this function

aggregate(schema, params, aggregate, field, opts \\ [])

Link to this function

all(query)
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)
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)
create(Ecto.Schema.t(), map() | Keyword.t()) ::
  {: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)
delete(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)
delete(Ecto.Schema.t(), 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)
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)
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)
stream(query(), 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)
update(Ecto.Schema.t(), integer(), map() | Keyword.t()) ::
  {:ok, Ecto.Schema.t()} | {:error, Ecto.Changeset.t()}
update(Ecto.Schema.t(), map(), Keyword.t()) ::
  {:ok, Ecto.Schema.t()} | {:error, Ecto.Changeset.t()}
update(module(), Ecto.Schema.t(), 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