View Source EctoCommand.Middleware behaviour (EctoCommand v0.1.0)

Middleware provides an extension point to add functions that you want to be called for every command execution

Implement the EctoCommand.Middleware behaviour in your module and define the before_execution/2, after_execution/2, after_failure/2 and invalid/2 callback functions.

example-middleware

Example middleware

defmodule SampleMiddleware do
  @behaviour EctoCommand.Middleware

  @impl true
  def before_execution(pipeline, _opts) do
    pipeline
    |> Pipeline.assign(:some_data, :some_value)
    |> Pipeline.update!(:command, fn command -> %{command | name: "updated-name"} end)
  end

  def after_execution(pipeline, _opts) do
    Logger.debug("Command executed successfully", command: pipeline.command, result: Pipeline.response(pipeline))

    pipeline
  end

  def after_failure(pipeline, _opts) do
    Logger.error("Command execution fails", command: pipeline.command, error: Pipeline.response(pipeline))

    pipeline
  end

  def invalid(pipeline, _opts) do
    Logger.error("invalid params received", params: pipeline.params, error: Pipeline.response(pipeline))

    pipeline
  end
end

Link to this section Summary

Link to this section Types

@type pipeline() :: %EctoCommand.Middleware.Pipeline{
  assigns: term(),
  command: term(),
  error: term(),
  halted: term(),
  handler: term(),
  metadata: term(),
  middlewares: term(),
  params: term(),
  response: term()
}

Link to this section Callbacks

Link to this callback

after_execution(pipeline, opts)

View Source
@callback after_execution(pipeline :: pipeline(), opts :: Keyword.t()) :: pipeline()
Link to this callback

after_failure(pipeline, opts)

View Source
@callback after_failure(pipeline :: pipeline(), opts :: Keyword.t()) :: pipeline()
Link to this callback

before_execution(pipeline, opts)

View Source
@callback before_execution(pipeline :: pipeline(), opts :: Keyword.t()) :: pipeline()
@callback invalid(pipeline :: pipeline(), opts :: Keyword.t()) :: pipeline()