W3cubDocs

/Phoenix

Ecto.Adapter behaviour

Specifies the minimal API required from adapters.

Summary

Types

adapter_meta()

The metadata returned by the adapter init/1

t()

Functions

lookup_meta(repo_name_or_pid)

Returns the adapter metadata from the init/1 callback

Callbacks

__before_compile__(env)

The callback invoked in case the adapter needs to inject code

checkout(adapter_meta, options, function)

Checks out a connection for the duration of the given function

dumpers(primitive_type, ecto_type)

Returns the dumpers for a given type

ensure_all_started(config, type)

Ensure all applications necessary to run the adapter are started

init(config)

Initializes the adapter supervision tree by returning the children and adapter metadata

loaders(primitive_type, ecto_type)

Returns the loaders for a given type

Types

adapter_meta()

adapter_meta() :: map()

The metadata returned by the adapter init/1.

It must be a map and Ecto itself will always inject two keys into the meta:

  • the :cache key, which as ETS table that can be used as a cache (if available)
  • the :pid key, which is the PID returned by the child spec returned in init/1

t()

t() :: module()

Functions

lookup_meta(repo_name_or_pid)

Returns the adapter metadata from the init/1 callback.

It expects a name or a PID representing a repo.

Callbacks

__before_compile__(env)

__before_compile__(term(), env :: Macro.Env.t()) :: Macro.t()

The callback invoked in case the adapter needs to inject code.

checkout(adapter_meta, options, function)

checkout(adapter_meta(), options :: Keyword.t(), (() -> result)) :: result
when result: var

Checks out a connection for the duration of the given function.

In case the adapter provides a pool, this guarantees all of the code inside the given fun runs against the same connection.

dumpers(primitive_type, ecto_type)

dumpers(primitive_type :: Ecto.Type.primitive(), ecto_type :: Ecto.Type.t()) ::
  [(term() -> {:ok, term()} | :error) | Ecto.Type.t()]

Returns the dumpers for a given type.

It receives the primitive type and the Ecto type (which may be primitive as well). It returns a list of dumpers with the given type usually at the beginning.

This allows developers to properly translate values coming from the Ecto into adapter ones. For example, if the database does not support booleans but instead returns 0 and 1 for them, you could add:

def dumpers(:boolean, type), do: [type, &bool_encode/1]
def dumpers(_primitive, type), do: [type]

defp bool_encode(false), do: {:ok, 0}
defp bool_encode(true), do: {:ok, 1}

All adapters are required to implement a clause for :binary_id types, since they are adapter specific. If your adapter does not provide binary ids, you may simply use Ecto.UUID:

def dumpers(:binary_id, type), do: [type, Ecto.UUID]
def dumpers(_primitive, type), do: [type]

ensure_all_started(config, type)

ensure_all_started(config :: Keyword.t(), type :: :application.restart_type()) ::
  {:ok, [atom()]} | {:error, atom()}

Ensure all applications necessary to run the adapter are started.

init(config)

init(config :: Keyword.t()) :: {:ok, :supervisor.child_spec(), adapter_meta()}

Initializes the adapter supervision tree by returning the children and adapter metadata.

loaders(primitive_type, ecto_type)

loaders(primitive_type :: Ecto.Type.primitive(), ecto_type :: Ecto.Type.t()) ::
  [(term() -> {:ok, term()} | :error) | Ecto.Type.t()]

Returns the loaders for a given type.

It receives the primitive type and the Ecto type (which may be primitive as well). It returns a list of loaders with the given type usually at the end.

This allows developers to properly translate values coming from the adapters into Ecto ones. For example, if the database does not support booleans but instead returns 0 and 1 for them, you could add:

def loaders(:boolean, type), do: [&bool_decode/1, type]
def loaders(_primitive, type), do: [type]

defp bool_decode(0), do: {:ok, false}
defp bool_decode(1), do: {:ok, true}

All adapters are required to implement a clause for :binary_id types, since they are adapter specific. If your adapter does not provide binary ids, you may simply use Ecto.UUID:

def loaders(:binary_id, type), do: [Ecto.UUID, type]
def loaders(_primitive, type), do: [type]

© 2012 Plataformatec
Licensed under the Apache License, Version 2.0.
https://hexdocs.pm/ecto/Ecto.Adapter.html