Centralizes reusable SQL queries, e.g. for pagination and counting rows.
If available in Hex, the package can be installed
by adding uquery
to your list of dependencies in mix.exs
:
def deps do
[
{:uquery, "~> 0.1.0"}
]
end
Run it in shell:
> mix deps.get
# in your repo file:
defmodule MyApp.Repo do
use Ecto.Repo,
otp_app: :my_app,
adapter: Ecto.Adapters.Postgres
...
def paginate(query, page, per_page) do
UQuery.Paginator.paginate(__MODULE__, query, page, per_page)
end
def count(query) do
UQuery.Counter.count(__MODULE__, query)
end
...
end
# in your models:
defmodule MyApp.Project do
...
import Ecto.Query, warn: false
alias MyApp.Repo
alias MyApp.Projects.Project
...
def list_projects(page, per_page) do
from(p in Project, select: p)
|> Repo.paginate(page, per_page)
end
def count_projects() do
from(p in Project, select: p)
|> Repo.count()
end
...
end
Documentation can be generated with ExDoc and published on HexDocs. Once published, the docs can be found at https://hexdocs.pm/uquery.