View Source Bee.Api behaviour (bee v0.4.3)
Generate an Api for repository.
Summary
Callbacks
If the query has a limit, offset, distinct or combination set, it will be automatically wrapped in a subquery in order to return the proper result.
Obtain all models from the data store where the conditions on where
or or_where
matches.
Returns []
(empty list) if no result was found.
Count an entity. Return the integer with the value
Delete an entity using its primary key on id
in a given entity or entity or changeset.
Delete many of an entity on data store by list of id (primary key).
Check if exists an entity. Return the tuple {:ok, true} | {:error, false}
Check if exists an entity, return the boolean true or false .
Obtain a single model from the data store where the primary key matches the
given id.
Returns {:error, :not_found}
if no result was found. If the struct in the queryable
has no or more than one primary key, it will raise an argument error.
Similar to get/2
but raises Ecto.NoResultsError
if no record was found.
Obtain a single model from the data store where the conditions on where
or or_where
matches.
Returns {:error, :not_found}
if no result was found. If the struct in the queryable
has no or more than one primary key, it will raise an argument error.
Similar to get_by/2
but raises Ecto.NoResultsError
if no record was found.
Inserts an entity on data store by struct defined Ecto.Schema
,
or map of data, or a changeset.
Inserts many of an entity on data store by struct defined Ecto.Schema
,
or map of data, or a changeset.
Prepare query from params.
Updates an entity using its primary key on id
in a given map or struct, or a given changeset.
Types
Callbacks
@callback aggregate( mode :: :avg | :count | :max | :min | :sum, field :: atom(), options() ) :: integer()
If the query has a limit, offset, distinct or combination set, it will be automatically wrapped in a subquery in order to return the proper result.
The aggregation will fail if any group_by
field is set.
Calculate the given aggregate
.
The mode
is one of :avg | :count | :max | :min | :sum
Options
:where
- The group conditions for WHERE argument in SQL that results in AND for any keys.:or_where
- Using after:where
, grouping conditions in a OR for WHERE argument in SQL.:group
- Grouping query by fields:distinct
- List of columns (atoms) that remove duplicate on query:debug
- Show in console, the generated Ecto query before call:limit
- Set the limit for the query:offset
- Set the offset for the query:aggregate
- Settings of aggregate data
Example
Post.Api.aggregate(:count, :id, where: [title: "My Post"])
@callback all(options()) :: [Ecto.Schema.t()] | [term()] | []
Obtain all models from the data store where the conditions on where
or or_where
matches.
Returns []
(empty list) if no result was found.
Options
:where
- The group conditions for WHERE argument in SQL that results in AND for any keys.:or_where
- Using after:where
, grouping conditions in a OR for WHERE argument in SQL.:preload
- Preload relations on query:order
- Set order for the query:select
- Fields should be returned by query:group
- Grouping query by fields:distinct
- List of columns (atoms) that remove duplicate on query:debug
- Show in console, the generated Ecto query before call:limit
- Set the limit for the query:offset
- Set the offset for the query
See the "Knowing Options" section at the module documentation for more details.
Example
Post.Api.all(where: [area: "Financial"])
Post.Api.all(where: [area: "Financial"], preload: [:comments])
Post.Api.all(where: [area: "Financial"], preload: [comments: [preload: [:users]]])
Count an entity. Return the integer with the value
Options
:where
- The group conditions for WHERE argument in SQL that results in AND for any keys.:or_where
- Using after:where
, grouping conditions in a OR for WHERE argument in SQL.:group
- Grouping query by fields:distinct
- List of columns (atoms) that remove duplicate on query:debug
- Show in console, the generated Ecto query before call:limit
- Set the limit for the query:offset
- Set the offset for the query
Example
Post.Api.count(where: [title: "My Post"])
@callback delete(id :: String.t() | integer()) :: {:ok, Ecto.Schema.t()} | {:error, Ecto.Changeset.t()} | {:error, :not_found}
@callback delete(model :: Ecto.Schema.t()) :: {:ok, Ecto.Schema.t()} | {:error, Ecto.Changeset.t()} | {:error, :not_found}
@callback delete(changeset :: Ecto.Changeset.t()) :: {:ok, Ecto.Schema.t()} | {:error, Ecto.Changeset.t()} | {:error, :not_found}
Delete an entity using its primary key on id
in a given entity or entity or changeset.
Example
Post.Api.delete(1)
Post.Api.delete(%Post{id: 1})
Post.Api.delete(%Ecto.Changeset{})
@callback delete_many_by_id(ids :: [binary()] | [integer()], opts :: keyword()) :: {:ok, %{total: integer(), deleted: integer()}} | {:error, term()}
Delete many of an entity on data store by list of id (primary key).
Options
:batch
- quantity integer of items by batch.
Example
Post.Api.delete_many_by_id([1,2,3,4,5], [ batch: 10 ])
@callback exists(id :: bitstring()) :: {:ok, true} | {:error, false}
@callback exists(options()) :: {:ok, true} | {:error, false}
Check if exists an entity. Return the tuple {:ok, true} | {:error, false}
Options
:where
- The group conditions for WHERE argument in SQL that results in AND for any keys.:or_where
- Using after:where
, grouping conditions in a OR for WHERE argument in SQL.:group
- Grouping query by fields:distinct
- List of columns (atoms) that remove duplicate on query:debug
- Show in console, the generated Ecto query before call:limit
- Set the limit for the query:offset
- Set the offset for the query
Example
Post.Api.exists(1)
Post.Api.exists(where: [title: "My Post"])
@callback exists?(options()) :: true | false
Check if exists an entity, return the boolean true or false .
Options
:where
- The group conditions for WHERE argument in SQL that results in AND for any keys.:or_where
- Using after:where
, grouping conditions in a OR for WHERE argument in SQL.:group
- Grouping query by fields:distinct
- List of columns (atoms) that remove duplicate on query:debug
- Show in console, the generated Ecto query before call:limit
- Set the limit for the query:offset
- Set the offset for the query
Example
Post.Api.exists?(where: [title: "My Post"])
@callback get(id(), options()) :: {:ok, Ecto.Schema.t()} | {:ok, term()} | {:error, :not_found}
Obtain a single model from the data store where the primary key matches the
given id.
Returns {:error, :not_found}
if no result was found. If the struct in the queryable
has no or more than one primary key, it will raise an argument error.
Options
:preload
- Preload relations on query:order
- Set order for the query:select
- Fields should be returned by query:group
- Grouping query by fields:distinct
- List of columns (atoms) that remove duplicate on query:debug
- Show in console, the generated Ecto query before call:limit
- Set the limit for the query:offset
- Set the offset for the query
See the "Knowing Options" section at the module documentation for more details.
Example
Post.Api.get(42)
Post.Api.get(42, preload: [:comments])
Post.Api.get(42, preload: [comments: [preload: [:users]]])
@callback get!(id(), options()) :: {:ok, Ecto.Schema.t()} | {:ok, term()} | {:error, :not_found}
Similar to get/2
but raises Ecto.NoResultsError
if no record was found.
Options
:preload
- Preload relations on query:order
- Set order for the query:select
- Fields should be returned by query:group
- Grouping query by fields:distinct
- List of columns (atoms) that remove duplicate on query:debug
- Show in console, the generated Ecto query before call:limit
- Set the limit for the query:offset
- Set the offset for the query
See the "Knowing Options" section at the module documentation for more details.
Example
Post.Api.get!(42)
Post.Api.get!(42, preload: [:comments])
Post.Api.get!(42, preload: [comments: [preload: [:users]]])
@callback get_by(options()) :: {:ok, Ecto.Schema.t()} | {:ok, term()} | {:error, :not_found}
Obtain a single model from the data store where the conditions on where
or or_where
matches.
Returns {:error, :not_found}
if no result was found. If the struct in the queryable
has no or more than one primary key, it will raise an argument error.
Options
:where
- The group conditions for WHERE argument in SQL that results in AND for any keys.:or_where
- Using after:where
, grouping conditions in a OR for WHERE argument in SQL.:preload
- Preload relations on query:order
- Set order for the query:select
- Fields should be returned by query:group
- Grouping query by fields:distinct
- List of columns (atoms) that remove duplicate on query:debug
- Show in console, the generated Ecto query before call:limit
- Set the limit for the query:offset
- Set the offset for the query
See the "Knowing Options" section at the module documentation for more details.
Example
Post.Api.get_by(where: [title: "My Post"])
Post.Api.get_by(where: [title: "My Post"], preload: [:comments])
Post.Api.get_by(where: [title: "My Post"], preload: [comments: [preload: [:users]]])
@callback get_by!(options()) :: {:ok, Ecto.Schema.t()} | {:ok, term()} | {:error, :not_found}
Similar to get_by/2
but raises Ecto.NoResultsError
if no record was found.
Options
:where
- The group conditions for WHERE argument in SQL that results in AND for any keys.:or_where
- Using after:where
, grouping conditions in a OR for WHERE argument in SQL.:preload
- Preload relations on query:order
- Set order for the query:select
- Fields should be returned by query:group
- Grouping query by fields:distinct
- List of columns (atoms) that remove duplicate on query:debug
- Show in console, the generated Ecto query before call:limit
- Set the limit for the query:offset
- Set the offset for the query
See the "Knowing Options" section at the module documentation for more details.
Example
Post.Api.get_by!(where: [title: "My Post"], or_where: [title: "My Post 2"])
Post.Api.get_by!(where: [title: "My Post"], preload: [:comments])
Post.Api.get_by!(where: [title: "My Post"], preload: [comments: [preload: [:users]]])
@callback insert(params :: map()) :: {:ok, Ecto.Schema.t()} | {:error, Ecto.Changeset.t()}
@callback insert(struct :: Ecto.Changeset.t()) :: {:ok, Ecto.Schema.t()} | {:error, Ecto.Changeset.t()}
Inserts an entity on data store by struct defined Ecto.Schema
,
or map of data, or a changeset.
In case a struct or map is given, the struct or map is converted into a changeset with all non-nil fields as part of the changeset.
In case a changeset is given, the changes in the changeset are merged with the struct fields, and all of them are sent to the database.
Example
Post.Api.insert(%Post{title: "My Post})
Post.Api.insert(%{title: "My Post})
Post.Api.insert(%Ecto.Changeset{changes: %{title: "My Post}})
@callback insert_many(data :: [map()], opts :: keyword()) :: {:ok, %{total: integer(), inserted: integer(), conflicts: integer()}} | {:error, term()}
Inserts many of an entity on data store by struct defined Ecto.Schema
,
or map of data, or a changeset.
Options
:conflict_target
- :column_name | {:unsafe_fragment, binary_fragment}:on_conflict
- one of :raise, :nothing, :replace_all, {:replace_all_except, fields}, {:replace, fields}:batch
- quantity integer of items by batch.
Example
Post.Api.insert_many(
[
%{id: 1, title: "My Post"},
%{id: 2, title: "My Post 2"},
...
],
[
on_conflict: {:replace_all_except, [:id]},
conflict_target: [:id],
batch: 10
]
)
@callback params_to_query( prepare_options(), struct() ) :: [any()]
Prepare query from params.
Options
:limit
- Limit of rows:offset
- Offset of rows.:permission
- Permission for show fields:fields
- List fields to return:assocs
- List assoc to preload:filter
- Filters
Example
Post.Api.params_to_query([])
@callback update(params :: map()) :: {:ok, Ecto.Schema.t()} | {:error, Ecto.Changeset.t()}
@callback update(struct :: Ecto.Schema.t() | Ecto.Changeset.t()) :: {:ok, Ecto.Schema.t()} | {:error, Ecto.Changeset.t()}
Updates an entity using its primary key on id
in a given map or struct, or a given changeset.
Only valid when id
is a primary key
Example
post = %Post{id: 1, title: "My Post}
Post.Api.update(post, %{title: "My Post2})
Post.Api.update(%{id: 1 title: "My Post2})
Post.Api.update(%Ecto.Changeset{})
@callback update(struct :: Ecto.Schema.t(), params :: map()) :: {:ok, Ecto.Schema.t()} | {:error, Ecto.Changeset.t()}