ecto_shorts v1.1.0 EctoShorts.Actions View Source
Actions for CRUD in ecto, these 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 or an options list.
Similar to all/2
but can also accept a keyword options list.
Creates a schema with given params. Can also accept a keyword options list.
Deletes a schema
Similar to delete/1
but can also accept a keyword options list.
Deletes a schema. Can also accept a keyword options list.
Finds a schema with matching params. Can also accept a keyword options list.
Finds a schema by params and updates it or creates with results of params/update_params merged. Can also accept a keyword options list.
Finds a schema by params or creates one if it isn't found. Can also accept a keyword options list.
Accepts a list of schemas and attempts to find them in the DB. Any missing Schemas will be created. Can also accept a keyword options list.
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. Can also accept a keyword options list.
Link to this section Types
aggregate_options()
View Source
aggregate_options() :: :avg | :count | :max | :min | :sum
aggregate_options() :: :avg | :count | :max | :min | :sum
filter_params() View Source
query()
View Source
query() :: Ecto.Query | Ecto.Schema
query() :: Ecto.Query | Ecto.Schema
schema_list()
View Source
schema_list() :: [Ecto.Schema.t()] | []
schema_list() :: [Ecto.Schema.t()] | []
schema_res()
View Source
schema_res() :: {:ok, Ecto.Schema.t()} | {:error, String.t()}
schema_res() :: {:ok, Ecto.Schema.t()} | {:error, String.t()}
Link to this section Functions
aggregate(schema, params, aggregate, field, opts \\ [])
View Source
aggregate(
queryable :: query(),
params :: filter_params(),
agg_opts :: aggregate_options(),
field :: atom(),
opts :: Keyword.t()
) :: term()
aggregate( queryable :: query(), params :: filter_params(), agg_opts :: aggregate_options(), field :: atom(), opts :: Keyword.t() ) :: term()
all(query)
View Source
all(queryable :: query()) :: schema_list()
all(queryable :: query()) :: schema_list()
Gets a collection of schemas from the database
Examples
iex> EctoSchemas.Actions.all(EctoSchemas.Accounts.User)
[]
all(query, params)
View Source
all(queryable :: query(), params :: filter_params()) :: schema_list()
all(queryable :: query(), opts :: Keyword.t()) :: schema_list()
all(queryable :: query(), params :: filter_params()) :: schema_list()
all(queryable :: query(), opts :: Keyword.t()) :: schema_list()
Gets a collection of schemas from the database but allows for a filter or an options list.
Options
:repo
- A module that uses the Ecto.Repo Module.
Examples
iex> Enum.each(1..4, fn _ -> create_user() end)
iex> length(EctoSchemas.Actions.all(EctoSchemas.Accounts.User, first: 3)) === 3
true
iex> Enum.each(1..4, fn _ -> create_user() end)
iex> length(EctoSchemas.Actions.all(EctoSchemas.Accounts.User, repo: MyApp.MyRepoModule.Repo)) === 3
true
all(query, params, opts)
View Source
all(queryable :: query(), params :: filter_params(), opts :: Keyword.t()) ::
schema_list()
all(queryable :: query(), params :: filter_params(), opts :: Keyword.t()) :: schema_list()
Similar to all/2
but can also accept a keyword options list.
Options
:repo
- A module that uses the Ecto.Repo Module.
Examples
iex> Enum.each(1..4, fn _ -> create_user() end) iex> length(EctoSchemas.Actions.all(EctoSchemas.Accounts.User, first: 3, repo: MyApp.MyRepoModule.Repo)) === 3 true
create(schema, params, opts \\ [])
View Source
create(
schema :: Ecto.Schema.t(),
params :: filter_params(),
opts :: Keyword.t()
) :: {:ok, Ecto.Schema.t()} | {:error, Ecto.Changeset.t()}
create( schema :: Ecto.Schema.t(), params :: filter_params(), opts :: Keyword.t() ) :: {:ok, Ecto.Schema.t()} | {:error, Ecto.Changeset.t()}
Creates a schema with given params. Can also accept a keyword options list.
Options
:repo
- A module that uses the Ecto.Repo Module.
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
Examples
iex> {:ok, schema} = EctoSchemas.Actions.create(EctoSchemas.Accounts.User, user_params(first_name: "TEST"), repo: MyApp.MyRepoModule.Repo)
iex> schema.first_name
true
delete(schema_data)
View Source
delete(schema_data :: Ecto.Schema.t()) ::
{:ok, Ecto.Schema.t()} | {:error, Ecto.Changeset.t()}
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
delete(schema_data, opts)
View Source
delete(schema_data :: Ecto.Schema.t(), opts :: Keyword.t()) ::
{:ok, Ecto.Schema.t()} | {:error, Ecto.Changeset.t()}
delete(schema :: Ecto.Schema.t(), id :: integer()) ::
{:ok, Ecto.Schema.t()} | {:error, Ecto.Changeset.t()}
delete(schema_data :: Ecto.Schema.t(), opts :: Keyword.t()) :: {:ok, Ecto.Schema.t()} | {:error, Ecto.Changeset.t()}
delete(schema :: Ecto.Schema.t(), id :: integer()) :: {:ok, Ecto.Schema.t()} | {:error, Ecto.Changeset.t()}
Similar to delete/1
but can also accept a keyword options list.
Options
:repo
- A module that uses the Ecto.Repo Module.
Examples
iex> user = create_user()
iex> {:ok, schema} = EctoSchemas.Actions.delete(user, repo: MyApp.MyRepoModule.Repo)
iex> schema.first_name === user.first_name
true
delete(schema, id, opts)
View Source
delete(schema :: Ecto.Schema.t(), id :: integer(), opts :: Keyword.t()) ::
{:ok, Ecto.Schema.t()} | {:error, Ecto.Changeset.t()}
delete(schema :: Ecto.Schema.t(), id :: integer(), opts :: Keyword.t()) :: {:ok, Ecto.Schema.t()} | {:error, Ecto.Changeset.t()}
Deletes a schema. Can also accept a keyword options list.
Options
:repo
- A module that uses the Ecto.Repo Module.
Examples
iex> user = create_user()
iex> {:ok, schema} = EctoSchemas.Actions.delete(EctoSchemas.Accounts.User, user.id)
iex> schema.first_name === user.first_name
true
iex> user = create_user()
iex> {:ok, schema} = EctoSchemas.Actions.delete(EctoSchemas.Accounts.User, user.id)
iex> schema.first_name === user.first_name
true
find(query, params, opts \\ [])
View Source
find(queryable :: query(), params :: filter_params(), opts :: Keyword.t()) ::
schema_res()
find(queryable :: query(), params :: filter_params(), opts :: Keyword.t()) :: schema_res()
Finds a schema with matching params. Can also accept a keyword options list.
Options
:repo
- A module that uses the Ecto.Repo Module.
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
iex> user = create_user()
iex> {:ok, schema} = EctoSchemas.Actions.find(EctoSchemas.Accounts.User, first_name: user.first_name, repo: MyApp.MyRepoModule.Repo)
iex> schema.first_name === user.first_name
true
find_and_update(schema, params, update_params, opts \\ [])
View Source
find_and_update(Ecto.Schema.t(), map(), map(), opts :: Keyword.t()) ::
{:ok, Ecto.Schema.t()} | {:error, Ecto.Changeset.t()}
find_and_update(Ecto.Schema.t(), map(), map(), opts :: Keyword.t()) :: {:ok, Ecto.Schema.t()} | {:error, Ecto.Changeset.t()}
Finds a schema by params and updates it or creates with results of params/update_params merged. Can also accept a keyword options list.
Options
:repo
- A module that uses the Ecto.Repo Module.:replica
- If you don't want to perform any reads against your Primary, you can specify a replica to read from.
Examples
iex> {:ok, schema} = EctoSchemas.Actions.find_and_update(EctoSchemas.Accounts.User, %{email: "some_email"}, %{name: "great name"})
iex> {:ok, schema} = EctoSchemas.Actions.find_and_update(EctoSchemas.Accounts.User, %{email: "some_email"}, %{name: "great name}, repo: MyApp.MyRepoModule.Repo, replica: MyApp.MyRepoModule.Repo.replica())
find_or_create(schema, params, opts \\ [])
View Source
find_or_create(Ecto.Schema.t(), map(), opts :: Keyword.t()) ::
{:ok, Ecto.Schema.t()} | {:error, Ecto.Changeset.t()}
find_or_create(Ecto.Schema.t(), map(), opts :: Keyword.t()) :: {:ok, Ecto.Schema.t()} | {:error, Ecto.Changeset.t()}
Finds a schema by params or creates one if it isn't found. Can also accept a keyword options list.
Options
:repo
- A module that uses the Ecto.Repo Module.:replica
- If you don't want to perform any reads against your Primary, you can specify a replica to read from.
Examples
iex> {:ok, schema} = EctoSchemas.Actions.find_or_create(EctoSchemas.Accounts.User, %{name: "great name"})
iex> {:ok, schema} = EctoSchemas.Actions.find_or_create(EctoSchemas.Accounts.User, %{name: "great name"}, repo: MyApp.MyRepoModule.Repo, replica: MyApp.MyRepoModule.Repo.replica())
find_or_create_many(schema, param_list, opts)
View Source
find_or_create_many(Ecto.Schema.t(), [map()], opts :: Keyword.t()) ::
{:ok, [Ecto.Schema.t()]} | {:error, [Ecto.Changeset.t()]}
find_or_create_many(Ecto.Schema.t(), [map()], opts :: Keyword.t()) :: {:ok, [Ecto.Schema.t()]} | {:error, [Ecto.Changeset.t()]}
Accepts a list of schemas and attempts to find them in the DB. Any missing Schemas will be created. Can also accept a keyword options list.
Options
:repo
- A module that uses the Ecto.Repo Module.:replica
- If you don't want to perform any reads against your Primary, you can specify a replica to read from.
Examples
iex> {:ok, records} = EctoSchemas.Actions.find_or_create_many(EctoSchemas.Accounts.User, [%{name: "foo"}, %{name: "bar}]) iex> length(records) === 2
get(schema, id, opts \\ [])
View Source
get(queryable :: query(), id :: term(), options :: Keyword.t()) ::
Ecto.Schema.t() | nil
get(queryable :: query(), id :: term(), options :: Keyword.t()) :: 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
stream(query, params, opts \\ [])
View Source
stream(queryable :: query(), params :: filter_params(), opts :: Keyword.t()) ::
Enum.t()
stream(queryable :: query(), params :: filter_params(), opts :: Keyword.t()) :: Enum.t()
Gets a collection of schemas from the database but allows for a filter
update(schema, schema_data, updates, opts \\ [])
View Source
update(
schema :: Ecto.Schema.t(),
schema_data :: map(),
updates :: Keyword.t(),
opts :: Keyword.t()
) :: {:ok, Ecto.Schema.t()} | {:error, Ecto.Changeset.t()}
update(
schema :: Ecto.Schema.t(),
id :: integer(),
updates :: map() | Keyword.t(),
opts :: Keyword.t()
) :: {:ok, Ecto.Schema.t()} | {:error, Ecto.Changeset.t()}
update(
schema :: Ecto.Schema.t(),
schema_data :: map(),
updates :: Keyword.t(),
opts :: Keyword.t()
) :: {:ok, Ecto.Schema.t()} | {:error, Ecto.Changeset.t()}
update(
schema :: module(),
schema_data :: Ecto.Schema.t(),
updates :: map(),
opts :: Keyword.t()
) :: {:ok, Ecto.Schema.t()} | {:error, Ecto.Changeset.t()}
update( schema :: Ecto.Schema.t(), schema_data :: map(), updates :: Keyword.t(), opts :: Keyword.t() ) :: {:ok, Ecto.Schema.t()} | {:error, Ecto.Changeset.t()}
update( schema :: Ecto.Schema.t(), id :: integer(), updates :: map() | Keyword.t(), opts :: Keyword.t() ) :: {:ok, Ecto.Schema.t()} | {:error, Ecto.Changeset.t()}
update( schema :: Ecto.Schema.t(), schema_data :: map(), updates :: Keyword.t(), opts :: Keyword.t() ) :: {:ok, Ecto.Schema.t()} | {:error, Ecto.Changeset.t()}
update( schema :: module(), schema_data :: Ecto.Schema.t(), updates :: map(), opts :: Keyword.t() ) :: {:ok, Ecto.Schema.t()} | {:error, Ecto.Changeset.t()}
Updates a schema with given updates. Can also accept a keyword options list.
Options
:repo
- A module that uses the Ecto.Repo Module.:replica
- If you don't want to perform any reads against your Primary, you can specify a replica to read from.
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
iex> user = create_user()
iex> {:ok, schema} = EctoSchemas.Actions.update(EctoSchemas.Accounts.User, 1, first_name: user.first_name, repo: MyApp.MyRepoModule.Repo, replica: MyApp.MyRepoModule.Repo.replica())
iex> schema.first_name === user.first_name
true