View Source EctoHooks behaviour (ecto_hooks v1.2.1)
Module exposing various utility functions for interacting with, and introspecting the state of EctoHooks.
For more extensive usage instructions, please see associated documentation for
EctoHooks.Repo
.
Summary
Functions
Alternative interface for initializing EctoHooks when use
-ed in a module as follows
Disables EctoHooks running for all future Repo operations in the current process.
Ensures EctoHooks run for all future Repo operations in the current process.
Returns a boolean indicating if any before_*
or after_*
hooks are allowed
to execute in the current process.
Utility function which returns the "nesting" of the current EctoHooks context.
Utility function which returns true if currently executing inside the context of an Ecto Hook.
Callbacks
@callback after_delete(schema_struct :: struct(), delta :: EctoHooks.Delta.t()) :: struct()
@callback after_get(schema_struct :: struct(), delta :: EctoHooks.Delta.t()) :: struct()
@callback after_insert(schema_struct :: struct(), delta :: EctoHooks.Delta.t()) :: struct()
@callback after_update(schema_struct :: struct(), delta :: EctoHooks.Delta.t()) :: struct()
@callback before_delete(queryable :: %Ecto.Queryable{}) :: %Ecto.Queryable{}
@callback before_insert(queryable :: %Ecto.Queryable{}) :: %Ecto.Queryable{}
@callback before_update(queryable :: %Ecto.Queryable{}) :: %Ecto.Queryable{}
Functions
Alternative interface for initializing EctoHooks when use
-ed in a module as follows:
def MyApp.Repo do
use Ecto.Repo,
otp_app: :my_app,
adapter: Ecto.Adapters.Postgres
use EctoHooks
end
Please see EctoHooks.Repo
for more information about EctoHooks usage, examples,
and considerations.
@spec disable_hooks(Keyword.t()) :: :ok
Disables EctoHooks running for all future Repo operations in the current process.
Internally, this is called with the options [global: false]
which gets
automatically cleared after triggering any Ecto.Repo
callback.
@spec enable_hooks(Keyword.t()) :: :ok
Ensures EctoHooks run for all future Repo operations in the current process.
Internally, we use it to scope re-enabling hooks per execution, as we disabled
them as a mitigation for infinitely looping hooks caused by hooks triggering
other hooks. This happens when provided [global: false]
, which should only be
done internally.
@spec hooks_enabled?() :: boolean()
Returns a boolean indicating if any before_*
or after_*
hooks are allowed
to execute in the current process.
Note that calls to Ecto.Repo
callbacks may reset or change the state of this flag,
and that this function is primarily exposed for introspection or debugging purposes.
@spec hooks_ref_count() :: pos_integer()
Utility function which returns the "nesting" of the current EctoHooks context.
By default, every hook will "acquire" an EctoHook context and increment a ref count. These ref counts are automatically decremented once a hook finishes running.
This is provided as a lower level alternative the enable_hooks/1
, disable_hooks/1
,
and hooks_enabled?/0
functions.
@spec in_hook?() :: boolean()
Utility function which returns true if currently executing inside the context of an Ecto Hook.