<!--
SPDX-FileCopyrightText: 2026 Łukasz Niemier <~@hauleth.dev>
SPDX-License-Identifier: Apache-2.0
-->
# EctoView
Helper functions for creating database views using `Ecto.Query` helpers.
## Installation
Add `ecto_view` to your list of dependencies in `mix.exs`:
```elixir
def deps do
[
{:ecto_view, "~> 1.0"}
]
end
```
## Usage
```elixir
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:
```elixir
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](https://hexdocs.pm/ecto_view).