calcinator v1.5.0 Calcinator.Resources behaviour

A module that exposes Ecto schema structs

Summary

Types

Invoke name filter with value

ID that uniquely identifies the struct

Pagination information returned from the backing store

  • :associations - associations to load in the struct
  • :filters - filters on the result
  • :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
t()

A module that implements the Resources behaviour

Functions

Converts the attribute to a field if a corresponding field exists in ecto_schema_module

Callbacks

Allows access to sandbox for testing

Changeset for creating a struct from the params

Changeset for updating struct with params

Deletes a single struct

Gets a single struct

Inserts params into a single new struct

Gets a list of structs

Returns

Applies updates in changeset

Types

filter()
filter() :: %{optional(name :: String.t) => value :: term}

Invoke name filter with value.

id()
id() :: term

ID that uniquely identifies the struct

pagination()
pagination() :: map

Pagination information returned from the backing store.

params()
params() :: map
query_options()
query_options() :: %{optional(:associations) => atom | [atom], optional(:filters) => [filter], optional(:page) => Resources.Page.t | nil, optional(:sorts) => Sorts.t}
  • :associations - associations to load in the struct
  • :filters - filters on the result
  • :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()
sandbox_access_token() :: %{:owner => term, optional(atom) => any}
t()
t() :: module

A module that implements the Resources behaviour

Functions

attribute_to_field(attribute, ecto_schema_module)

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 corresponding field with _ in ecto_schema_module
  • {:error, attribute} - attribute does not have corresponding field in ecto_schema_module

Callbacks

allow_sandbox_access(sandbox_access_token)
allow_sandbox_access(sandbox_access_token) ::
  :ok |
  {:already, :owner | :allowed} |
  :not_found

Allows access to sandbox for testing

changeset(params)
changeset(params) :: Ecto.Changeset.t

Changeset for creating a struct from the params

changeset(resource, params)
changeset(resource :: Ecto.Schema.t, params) :: Ecto.Changeset.t

Changeset for updating struct with params

delete(struct)
delete(struct) ::
  {:ok, struct} |
  {:error, :ownership} |
  {:error, Ecto.Changeset.t}

Deletes a single struct

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, Ecto.Changeset.t} - errors while deleting the struct. Ecto.Changeset.t errors contains errors.
get(id, query_options)
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 getting id from backing store .
  • {:error, reason} - an error occurred with the backing store for reason that is backing store specific.
insert(params, query_options)
insert(params, query_options) ::
  {:ok, struct} |
  {:error, :ownership} |
  {:error, Ecto.Changeset.t}
insert(Ecto.Changeset.t, query_options) ::
  {:ok, struct} |
  {:error, :ownership} |
  {:error, Ecto.Changeset.t}

Inserts params into a single new struct

Returns

  • {:ok, struct} - params were inserted into struct
  • {:error, :ownership} - connection to backing store was not owned by the calling process
  • {:error, Ecto.Changeset.t} - insert failed. Ecto.Changeset.t errors contain errors.
list(query_options)
list(query_options) ::
  {:ok, [struct], pagination | nil} |
  {:error, :ownership} |
  {:error, :timeout} |
  {:error, reason :: term}

Gets a list of structs.

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 for reason that is backing store specific.
sandboxed?()
sandboxed?() :: boolean

Returns

  • true - if allow_sandbox_access/1 should be called before any of the query methods are called
  • false - otherwise
update(arg0, query_options)
update(Ecto.Changeset.t, query_options) ::
  {:ok, struct} |
  {:error, :ownership} |
  {:error, Ecto.Changeset.t}

Applies updates in changeset

Returns

  • {:ok, struct} - the update succeeded and the returned struct contains the updates
  • {:error, :ownership} - connection to backing store was not owned by the calling process
  • {:error, Ecto.Changeset.t} - errors while updating struct with params. Ecto.Changeset.t errors contains errors.
update(resource, params, query_options)
update(resource :: Ecto.Schema.t, params, query_options) ::
  {:ok, struct} |
  {:error, Ecto.Changeset.t} |
  {:error, :bad_gateway} |
  {:error, :ownership} |
  {:error, :not_found}

Updates struct

Returns

  • {:ok, struct} - the update succeeded and the returned struct contains the updates
  • {:error, Ecto.Changeset.t} - errors while updating struct with params. Ecto.Changeset.t errors contains errors.
  • {:error, :bad_gateway} - error in backing store that cannot be represented as another type of error
  • {:error, :ownership} - connection to backing store was not owned by the calling process
  • {:error, :not_found} - the resource in the changeset was not found and so cannot be updated. This may mean that the resource was deleted with delete/1 after the get/2 or list/1 returned.