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

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

  1. Preloads any many_to_many associations that appear in params.
  2. Casts params into data using optional_field/0 and required_fields/0 of module.
  3. Puts many_to_many associations from params into changeset.
  4. 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

  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

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

The Ecto.Repo that stores ecto_schema_module/0

Link to this section Types

Link to this type ecto_schema_module() View Source
ecto_schema_module() :: module

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

Link to this section Functions

Link to this function add_not_found(changeset, field, index, id) View Source
Link to this function allow_sandbox_access(map) View Source
allow_sandbox_access(Resources.sandbox_access_token) ::
  :ok |
  {:error, :sandbox_access_disallowed}

Allows access to Ecto.Adapters.SQL.Sandbox

Link to this function changeset(module, params) View Source
changeset(module, Resources.params) ::
  {:ok, Ecto.Changeset.t} |
  {:error, :ownership}

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

Link to this function changeset(module, data, params) View Source
changeset(module, Ecto.Schema.t, Resources.params) ::
  {:ok, Ecto.Changeset.t} |
  {:error, :ownership}
  1. Preloads any many_to_many associations that appear in params.
  2. Casts params into data using optional_field/0 and required_fields/0 of module.
  3. Puts many_to_many associations from params into changeset.
  4. Validates changeset with module ecto_schema_module/0 changeset/0.

Returns

  • {:ok, Ecto.Changeset.t} - All steps were successful
  • {:error, :ownership} - Preloading the the many_to_many associations that appear in params using module repo/0 hit an ownership error.
Link to this function delete(module, changeset, query_options) View Source
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

Link to this function full_associations(query_options) View Source

Uses query_options as full associatons with no additions.

Link to this function get(module, id, query_options) View Source
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.
Link to this function insert(module, changeset, query_options) View Source
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.
Link to this function list(module, query_options) View Source
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 in opts
  • {: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.

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

Link to this function update(module, changeset, query_options) View Source
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.
Link to this function update(module, data, params, query_options) View Source
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.

Link to this section Callbacks

Link to this callback ecto_schema_module() View Source
ecto_schema_module() :: module

The Ecto.Schema module stored in repo/0.

Link to this callback filter(arg0, name, value) View Source (optional)
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.
Link to this callback full_associations(arg0) View Source
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.