Skip to content

Implements the middleware pattern for your Ecto repos

License

Notifications You must be signed in to change notification settings

vereis/ecto_middleware

Repository files navigation

EctoMiddleware

Installation

Add :ecto_middleware to the list of dependencies in mix.exs:

def deps do
  [
    {:ecto_middleware, "~> 1.0.0"}
  ]
end

About

This library, when use-ed, allows you to configure a generic middleware/2 in any module that has use Ecto.Repo within it, like so:

defmodule MyApp.Repo do
  use Ecto.Repo,
    otp_app: :my_app,
    adapter: Ecto.Adapters.Postgres

  use EctoMiddleware

  def middleware(action, _resource) when action in [:delete, :delete!] do
    [MyApp.EctoMiddleware.MaybeSoftDelete, EctoMiddleware.Super, MyApp.EctoMiddleware.Log]
  end

  def middleware(_action, _resource) do
    [EctoMiddleware.Super, MyApp.EctoMiddleware.Log]
  end
end

This is very much inspired by other implementations of the middleware pattern, especially Absinthe's.

By default, EctoMiddleware provides the EctoMiddleware.Super middleware which is responsible for actually executing any behaviour actually given to you by Ecto.Repo. Custom functionality can be added before, or after this EctoMiddleware.Super middleware to enable you to customize your repo actions accordingly.

Please see the docs for more information!

Example Usage

My library EctoHooks is a small library which allows you to define before_* and after_* callbacks directly in your Ecto.Schemas much like the old Ecto.Model callbacks before they were removed.

This library was re-implemented entirely to be defined as a pair of Ecto.Middleware middleware.

See the callback implementations for example usage!

Links

About

Implements the middleware pattern for your Ecto repos

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published