calcinator v5.0.0 Calcinator.Resources.Ecto.Repo behaviour View Source
Default callbacks for Calcinator.Resources behaviour when backed by a single Ecto.Repo
If you don’t have any default loaded associations, you only need to define ecto_schema_module/0 and repo/0
defmodule MyApp.Posts do
@moduledoc """
Retrieves `%MyApp.Post{}` from `MyApp.Repo`
"""
use Calcinator.Resources.Ecto.Repo
# Functions
## Calcinator.Resources.Ecto.Repo callbacks
def ecto_schema_module(), do: MyApp.Post
def repo(), do: MyApp.Repo
end
If you need to override the loaded associations also override full_associations/1
defmodule MyApp.Posts do
@moduledoc """
Retrieves `%MyApp.Post{}` from `MyApp.Repo`
"""
use Calcinator.Resources.Ecto.Repo
# Functions
## Calcinator.Resources.Ecto.Repo callbacks
def ecto_schema_module(), do: MyApp.Post
@doc """
Always loads post author
"""
def full_associations(query_options) do
[:author | super(query_options)]
end
def repo(), do: MyApp.Repo
end
Link to this section Summary
Types
Name of a module that defines an Ecto.Schema.t
Functions
Allows access to Ecto.Adapters.SQL.Sandbox
Ecto.Changeset.t using the default Ecto.Schema.t for module with params
- Preloads any
many_to_manyassociations that appear inparams. - Casts
paramsintodatausingoptional_field/0andrequired_fields/0ofmodule. - Puts
many_to_manyassociations fromparamsinto changeset. - Validates changeset with
moduleecto_schema_module/0changeset/0
Deletes changeset from module’s repo/0
Uses query_options as full associatons with no additions
Gets resource with id from module repo/0
- Insert
changesetintomodulerepo/0 - Inserts
paramsintomodulerepo/0after converting them into anEcto.Changeset.t
Returns
Whether module repo/0 is sandboxed and allow_sandbox_access/1 should be called
Updates struct in module repo/0 using changeset
Updates data with params in module repo/0
Callbacks
The Ecto.Schema module stored in repo/0
Filters query by name with the given value prior to running query on module repo in list/1
The full list of associations to preload in
Link to this section Types
Name of a module that defines an Ecto.Schema.t
Link to this section Functions
allow_sandbox_access(Resources.sandbox_access_token) ::
:ok |
{:error, :sandbox_access_disallowed}
Allows access to Ecto.Adapters.SQL.Sandbox
changeset(module, Resources.params) ::
{:ok, Ecto.Changeset.t} |
{:error, :ownership}
Ecto.Changeset.t using the default Ecto.Schema.t for module with params
changeset(module, Ecto.Schema.t, Resources.params) :: {:ok, Ecto.Changeset.t} | {:error, :ownership}
- Preloads any
many_to_manyassociations that appear inparams. - Casts
paramsintodatausingoptional_field/0andrequired_fields/0ofmodule. - Puts
many_to_manyassociations fromparamsinto changeset. - Validates changeset with
moduleecto_schema_module/0changeset/0.
Returns
{:ok, Ecto.Changeset.t}- All steps were successful{:error, :ownership}- Preloading the themany_to_manyassociations that appear inparamsusingmodulerepo/0hit an ownership error.
delete(module, changeset :: Ecto.Changeset.t, Resources.query_options) :: {:ok, Ecto.Schema.t} | {:error, :ownership} | {:error, Ecto.Changeset.t}
Deletes changeset from module’s repo/0
Uses query_options as full associatons with no additions.
get(module, Resources.id, Resources.query_options) ::
{:ok, Ecto.Schema.t} |
{:error, :not_found} |
{:error, :ownership}
Gets resource with id from module repo/0.
Returns
{:error, :not_found}- ifidis not found inmodule’srepo/0.{:error, :ownership}- ifDBConnection.OwnershipErrordue to connection sharing error during tests.{:ok, struct}- ifidis found inmodule’srepo/0. Associations will also be preloaded instructbased onResources.query_options.
insert(module, Ecto.Changeset.t | map, Resources.query_options) :: {:ok, Ecto.Schema.t} | {:error, :ownership} | {:error, Ecto.Changeset.t}
- Insert
changesetintomodulerepo/0 - Inserts
paramsintomodulerepo/0after converting them into anEcto.Changeset.t
Returns
{:error, :ownership}- connection to backing store was not owned by the calling process{:error, Ecto.Changeset.t}- ifchangesetcannot be inserted intomodulerepo/0{:ok, struct}- ifchangesetwas inserted in tomodulerepo/0.structis preloaded with associations according toResource.query_iptionsinopts.
list(module, Resources.query_options) ::
{:ok, [Ecto.Schema.t], nil} |
{:error, :ownership} |
{:error, Alembic.Document.t}
Returns
{:error, Alembic.Document.t}- JSONAPI error listing the unknown filters inopts{:error, :ownership}- connection to backing store was not owned by the calling process{:ok, [struct], nil}-[struct]is the list of allmoduleecto_schema_module/0inmodulerepo/0. There is no (current) support for pagination: pagination is thenilin the 3rd element of the tuple.
Whether module repo/0 is sandboxed and allow_sandbox_access/1 should be called.
update(module, Ecto.Changeset.t, Resources.query_options) :: {:ok, Ecto.Schema.t} | {:error, :ownership} | {:error, Ecto.Changeset.t}
Updates struct in module repo/0 using changeset.
Returns
{:error, :ownership}- connection to backing store was not owned by the calling process{:error, Ecto.Changeset.t}- if thechangesethad validations error or it could not be used to updatestructinmodulerepo/0.{:ok, struct}- the updatedstruct. Associations are preloaded usingResources.query_optionsinquery_options.
update(module, Ecto.Schema.t, Resources.params, Resources.query_options) :: {:ok, Ecto.Schema.t} | {:error, Ecto.Changeset.t}
Updates data with params in module repo/0
Returns
{:error, :ownership}- connection to backing store was not owned by the calling process{:error, Ecto.Changeset.t}- if the changeset derived from updatingdatawithparamshad validations error or it could not be used to updatedatainmodulerepo/0.{:ok, struct}- the updatedstruct. Associations are preloaded usingResources.query_optionsinquery_options.
Link to this section Callbacks
The Ecto.Schema module stored in repo/0.
filter(Ecto.Query.t, name :: String.t, value :: String.t) :: {:ok, Ecto.Query.t} | {:error, Alembic.Document.t}
Filters query by name with the given value prior to running query on module repo in list/1
Returns
{:ok, query} - given query with name filter with value applied
{:error, Alembic.Document.t} - JSONAPI error document with error(s) showing why either name filter was not
supported or `value` was not supported for `name` filter.
full_associations(Resources.query_options) :: [atom] | Keyword.t
The full list of associations to preload in
Calcinator.Resources.get/2Calcinator.Resources.insert/2Calcinator.Resources.list/1Calcinator.Resources.update/2Calcinator.Resources.update/3
Should combine the request-specific associations in Resources.query_options with any default associations and/or
transform requested associations to repo/0-specific associations.
The Ecto.Repo that stores ecto_schema_module/0.