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_many
associations that appear inparams
. - Casts
params
intodata
usingoptional_field/0
andrequired_fields/0
ofmodule
. - Puts
many_to_many
associations fromparams
into changeset. - Validates changeset with
module
ecto_schema_module/0
changeset/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
changeset
intomodule
repo/0
- Inserts
params
intomodule
repo/0
after 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_many
associations that appear inparams
. - Casts
params
intodata
usingoptional_field/0
andrequired_fields/0
ofmodule
. - Puts
many_to_many
associations fromparams
into changeset. - Validates changeset with
module
ecto_schema_module/0
changeset/0
.
Returns
{:ok, Ecto.Changeset.t}
- All steps were successful{:error, :ownership}
- Preloading the themany_to_many
associations that appear inparams
usingmodule
repo/0
hit 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}
- ifid
is not found inmodule
’srepo/0
.{:error, :ownership}
- ifDBConnection.OwnershipError
due to connection sharing error during tests.{:ok, struct}
- ifid
is found inmodule
’srepo/0
. Associations will also be preloaded instruct
based onResources.query_options
.
insert(module, Ecto.Changeset.t | map, Resources.query_options) :: {:ok, Ecto.Schema.t} | {:error, :ownership} | {:error, Ecto.Changeset.t}
- Insert
changeset
intomodule
repo/0
- Inserts
params
intomodule
repo/0
after converting them into anEcto.Changeset.t
Returns
{:error, :ownership}
- connection to backing store was not owned by the calling process{:error, Ecto.Changeset.t}
- ifchangeset
cannot be inserted intomodule
repo/0
{:ok, struct}
- ifchangeset
was inserted in tomodule
repo/0
.struct
is preloaded with associations according toResource.query_iptions
inopts
.
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 allmodule
ecto_schema_module/0
inmodule
repo/0
. There is no (current) support for pagination: pagination is thenil
in 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 thechangeset
had validations error or it could not be used to updatestruct
inmodule
repo/0
.{:ok, struct}
- the updatedstruct
. Associations are preloaded usingResources.query_options
inquery_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 updatingdata
withparams
had validations error or it could not be used to updatedata
inmodule
repo/0
.{:ok, struct}
- the updatedstruct
. Associations are preloaded usingResources.query_options
inquery_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/2
Calcinator.Resources.insert/2
Calcinator.Resources.list/1
Calcinator.Resources.update/2
Calcinator.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
.