calcinator v1.5.0 Calcinator.Resources.Ecto.Repo behaviour

Default callbacks for Calcinator.Resources behaviour when backed by a single Ecto.Repo

Summary

Types

Name of a module that defines an Ecto.Schema.t

Functions

Ecto.Changeset.t using the default Ecto.Schema.t for module with params

  1. Casts params into data using optional_field/0 and required_fields/0 of module
  2. Validates changeset with module ecto_schema_module/0 changeset/0

Deletes data from module’s repo/0

Uses query_options as full associatons with no additions

Gets resource with id from module repo/0

  1. Insert changeset into module repo/0
  2. Inserts params into module repo/0 after converting them into an Ecto.Changeset.t

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

The full list of associations to preload in

The Ecto.Repo that stores ecto_schema_module/0

Types

ecto_schema_module()
ecto_schema_module() :: module

Name of a module that defines an Ecto.Schema.t

Functions

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

Allows access to Ecto.Adapters.SQL.Sandbox

changeset(module, params)
changeset(module, Resources.params) :: Ecto.Changeset.t

Ecto.Changeset.t using the default Ecto.Schema.t for module with params

changeset(module, data, params)
  1. Casts params into data using optional_field/0 and required_fields/0 of module
  2. Validates changeset with module ecto_schema_module/0 changeset/0
delete(module, data)
delete(module, Ecto.Schema.t) ::
  {:ok, Ecto.Schema.t} |
  {:error, :ownership} |
  {:error, Ecto.Changeset.t}

Deletes data from module’s repo/0

full_associations(query_options)

Uses query_options as full associatons with no additions.

get(module, id, opts)
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} - if id is not found in module’s repo/0.
  • {:error, :ownership} - if DBConnection.OwnershipError due to connection sharing error during tests.
  • {:ok, struct} - if id is found in module’s repo/0. Associations will also be preloaded in struct based on Resources.query_options.
insert(module, params, opts)
insert(module, Ecto.Changeset.t | map, Resources.query_options) ::
  {:ok, Ecto.Schema.t} |
  {:error, :ownership} |
  {:error, Ecto.Changeset.t}
  1. Insert changeset into module repo/0
  2. Inserts params into module repo/0 after converting them into an Ecto.Changeset.t

Returns

  • {:error, :ownership} - connection to backing store was not owned by the calling process
  • {:error, Ecto.Changeset.t} - if changeset cannot be inserted into module repo/0
  • {:ok, struct} - if changeset was inserted in to module repo/0. struct is preloaded with associations according to Resource.query_iptions in opts.
list(module, opts)
list(module, Resources.query_options) ::
  {:ok, [Ecto.Schema.t], nil} |
  {:error, :ownership}

Returns

  • {:error, :ownership} - connection to backing store was not owned by the calling process
  • {:ok, [struct], nil} - [struct] is the list of all module ecto_schema_module/0 in module repo/0. There is no (current) support for pagination: pagination is the nil in the 3rd element of the tuple.
sandboxed?(module)

Whether module repo/0 is sandboxed and allow_sandbox_access/1 should be called.

update(module, changeset, query_options)
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 the changeset had validations error or it could not be used to update struct in module repo/0.
  • {:ok, struct} - the updated struct. Associations are preloaded using Resources.query_options in query_options.
update(module, data, params, query_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 updating data with params had validations error or it could not be used to update data in module repo/0.
  • {:ok, struct} - the updated struct. Associations are preloaded using Resources.query_options in query_options.

Callbacks

ecto_schema_module()
ecto_schema_module() :: module

The Ecto.Schema module stored in repo/0.

full_associations(arg0)
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.

repo()
repo() :: module

The Ecto.Repo that stores ecto_schema_module/0.