Helpers for using SQL views with Ecto
Elixir 99.2%
Other 0.8%
1 1 0

Clone this repository

https://tangled.org/hauleth.dev/ecto_view https://tangled.org/did:plc:6psaz6n5fuurrfet67zs4ljf/ecto_view
git@knot.hauleth.dev:hauleth.dev/ecto_view git@knot.hauleth.dev:did:plc:6psaz6n5fuurrfet67zs4ljf/ecto_view

For self-hosted knots, clone URLs may differ based on your setup.

Download tar.gz
README.md

EctoView#

Helper functions for creating database views using Ecto.Query helpers.

Installation#

Add ecto_view to your list of dependencies in mix.exs:

def deps do
  [
    {:ecto_view, "~> 1.0"}
  ]
end

Usage#

defmodule MyApplication.Repo.Migration.CreateViewFoo do
  use Ecto.Migration
  use EctoView.Migration # This line **MUST** be after `use Ecto.Migration`

  def change do
    query = from foo in "foos", # This **MUST** select from table name, no schemas allowed
      where: foo.bar == 2137,
      select: %{ # This **MUST** be a map of selected fields
        a: foo.a,
        b: foo.b
      }

    create view("foos_view", query)
  end
end

If you want, you can also create materialised view using materialized_view/2 function.

If you create materialised view, then you can use EctoView.refresh_materialized_view/3 function to refresh content of given view. If you will add:

defmodule MyApplication.Repo do
  use Ecto.Repo, 
  use EctoView

  # …
end

Then there will be a helper function Repo.refresh_materialised_view/2 that will call respective function for that repo.

The docs can be found at https://hexdocs.pm/ecto_view.