calcinator v5.0.0 Calcinator.Resources behaviour View Source
A module that exposes Ecto schema structs
Link to this section Summary
Types
ID that uniquely identifies the struct
Pagination information returned from the backing store
:associations
- associations to load in thestruct
:filters
- filters on the result:meta
- meta data that is traded back and forth between clients and servers that don’t fit into JSONAPI, such asCalcinator.Meta.Beam
.:page
- the page used for pagination.nil
implies no pagination, not default pagination.:sorts
- the directions to sort fields on the primary resource or its associations
A module that implements the Resources
behaviour
Functions
Converts the attribute to a field if a corresponding field exists in ecto_schema_module
JSONAPI filter values that allow multiple values, similar to includes, are comma separated without spaces, instead of
having to do String.split(comma_separated_values, ",")
in all filters that accept multiple values, this function can
be used
Error when a filter name
is not supported by the callback module
Callbacks
Allows access to sandbox for testing
Changeset for creating a struct from the params
- Changeset for updating
resource
withparams
. - Changeset for deleting
resource
(params
will be an empty map)
Deletes a single struct in a changeset
Gets a single struct
insert(changeset, query_options)
Gets a list of struct
s
Returns
Applies updates in changeset
Updates struct
Link to this section Types
ID that uniquely identifies the struct
Pagination information returned from the backing store.
query_options() :: %{optional(:associations) => atom | [atom], optional(:filters) => %{optional(String.t) => String.t}, optional(:meta) => %{optional(String.t) => Alembic.json}, optional(:page) => Resources.Page.t | nil, optional(:sorts) => Sorts.t}
:associations
- associations to load in thestruct
:filters
- filters on the result:meta
- meta data that is traded back and forth between clients and servers that don’t fit into JSONAPI, such asCalcinator.Meta.Beam
.:page
- the page used for pagination.nil
implies no pagination, not default pagination.:sorts
- the directions to sort fields on the primary resource or its associations
sandbox_access_token() :: %{:owner => term, optional(atom) => any}
A module that implements the Resources
behaviour
Link to this section Functions
Converts the attribute to a field if a corresponding field exists in ecto_schema_module
If a field exists, then it is returned. This includes fields with _
that have -
in their attribute name and
virtual fields.
iex> Calcinator.Resources.attribute_to_field("name", Calcinator.Resources.TestAuthor)
{:ok, :name}
iex> Calcinator.Resources.attribute_to_field("password-confirmation", Calcinator.Resources.TestAuthor)
{:ok, :password_confirmation}
Invalid field names will return an error
iex> Calcinator.Resources.attribute_to_field("password-hash", Calcinator.Resources.TestAuthor)
{:error, "password-hash"}
Associations are not fields, so they will return an error
iex> Calcinator.Resources.attribute_to_field("author", Calcinator.Resources.TestPost)
{:error, "author"}
Returns
{:ok, field}
-attribute
with-
has the correspondingfield
with_
inecto_schema_module
{:error, attribute}
-attribute
does not have corresponding field inecto_schema_module
JSONAPI filter values that allow multiple values, similar to includes, are comma separated without spaces, instead of
having to do String.split(comma_separated_values, ",")
in all filters that accept multiple values, this function can
be used.
iex> Calcinator.Resources.split_filter_value("1,2,3")
["1", "2", "3"]
unknown_filter(name :: String.t) :: Alembic.Document.t
Error when a filter name
is not supported by the callback module.
iex> Calcinator.Resources.unknown_filter("name")
%Alembic.Document{
errors: [
%Alembic.Error{
detail: "Unknown name filter",
source: %Alembic.Source{
pointer: "/filter/name"
},
status: "422",
title: "Unknown Filter"
}
]
}
Link to this section Callbacks
allow_sandbox_access(sandbox_access_token) :: :ok | {:error, :sandbox_access_disallowed}
Allows access to sandbox for testing
changeset(params) :: {:ok, Ecto.Changeset.t} | {:error, :ownership}
Changeset for creating a struct from the params
Returns
{:ok, Ecto.Changeset.t}
- a changeset was created with no error while trying to access the backing store{:error, :ownership}
- connection to backing store was not owned by the calling process when associations were preloaded into the data for the changeset.many_to_many
associations require the associations to be preloaded into theEcto.Changeset.t
data
before callingEcto.Changeset.put_assoc/3
.
changeset(resource :: Ecto.Schema.t, params) :: {:ok, Ecto.Changeset.t} | {:error, :ownership}
- Changeset for updating
resource
withparams
. - Changeset for deleting
resource
(params
will be an empty map)
Returns
{:ok, Ecto.Changeset.t}
- a changeset was created with no error while trying to access the backing store{:error, :ownership}
- connection to backing store was not owned by the calling process when associations were preloaded into the data for the changeset.many_to_many
associations require the associations to be preloaded into theEcto.Changeset.t
data
before callingEcto.Changeset.put_assoc/3
.
delete(changeset :: Ecto.Changeset.t, query_options) :: {:ok, struct} | {:error, :ownership} | {:error, :timeout} | {:error, Ecto.Changeset.t}
Deletes a single struct in a changeset
Returns
{:ok, struct}
- the delete succeeded and the returned struct is the state before delete{:error, :ownership}
- connection to backing store was not owned by the calling process{:error, :timeout}
- timeout occured while deletingstruct
from backing store.{:error, Ecto.Changeset.t}
- errors while deleting thechangeset
.Ecto.Changeset.t
errors
contains errors. These will normally be constraint errors or only those validations that can occur inprepare_changes
callbacks that requireEcto.Changeset.t
action
and orrepo
to be set.
get(id, query_options) :: {:ok, struct} | {:error, :not_found} | {:error, :ownership} | {:error, :timeout} | {:error, reason :: term}
Gets a single struct
Returns
{:ok, struct}
-id
was found.{:error, :not_found}
-id
was not found.{:error, :ownership}
- connection to backing store was not owned by the calling process{:error, :timeout}
- timeout occured while gettingid
from backing store.{:error, reason}
- an error occurred with the backing store forreason
that is backing store specific.
insert(changeset :: Ecto.Changeset.t | params, query_options) :: {:ok, struct} | {:error, :ownership} | {:error, :timeout} | {:error, Ecto.Changeset.t}
insert(changeset, query_options)
Inserts changeset
into a single new struct
.
Returns
{:ok, struct}
-changeset
was inserted intostruct
{:error, :ownership}
- connection to backing store was not owned by the calling process{:error, :timeout}
- timeout occured while insertingchangeset
into backing store.{:error, Ecto.Changeset.t}
- insert failed.Ecto.Changeset.t
errors
contain errors.
insert(params, query_options)
Inserts params
into a single new struct
.
Returns
{:ok, struct}
-params
were inserted intostruct
{:error, :ownership}
- connection to backing store was not owned by the calling process{:error, :timeout}
- timeout occured while insertingparams
into backing store.{:error, Ecto.Changeset.t}
- insert failed.Ecto.Changeset.t
errors
contain errors.
list(query_options) :: {:ok, [struct], pagination | nil} | {:error, :ownership} | {:error, :timeout} | {:error, reason :: term}
Gets a list of struct
s.
Returns
{:ok, [resource], nil}
- all resources matching query{:ok, [resource], pagination}
- page of resources matching query{:error, :ownership}
- connection to backing store was not owned by the calling process{:error, :timeout}
- timeout occured while getting resources from backing store.{:error, reason}
- an error occurred with the backing store forreason
that is backing store specific.
Returns
true
- ifallow_sandbox_access/1
should be called before any of the query methods are calledfalse
- otherwise
update(changeset :: Ecto.Changeset.t, query_options) :: {:ok, struct} | {:error, :ownership} | {:error, :timeout} | {:error, Ecto.Changeset.t}
Applies updates in changeset
Returns
{:ok, struct}
- the update succeeded and the returnedstruct
contains the updates{:error, :ownership}
- connection to backing store was not owned by the calling process{:error, :timeout}
- timeout occured while updatingchangeset
withparams
in backing store.{:error, Ecto.Changeset.t}
- errors while updatingstruct
withparams
.Ecto.Changeset.t
errors
contains errors.
update(resource :: Ecto.Schema.t, params, query_options) :: {:ok, struct} | {:error, Ecto.Changeset.t} | {:error, :bad_gateway} | {:error, :not_found} | {:error, :ownership} | {:error, :timeout}
Updates struct
Returns
{:ok, struct}
- the update succeeded and the returnedstruct
contains the updates{:error, Ecto.Changeset.t}
- errors while updatingstruct
withparams
.Ecto.Changeset.t
errors
contains errors.{:error, :bad_gateway}
- error in backing store that cannot be represented as another type of error{:error, :not_found}
- the resource in the changeset was not found and so cannot be updated. This may mean that the resource was deleted withdelete/1
after theget/2
orlist/1
returned.{:error, :ownership}
- connection to backing store was not owned by the calling process{:error, :timeout}
- timeout occured while updatingresource
withparams
in backing store.