Skip to content

Centralizes reusable queries, e.g. for pagination and counting rows.

License

Notifications You must be signed in to change notification settings

adilsonchacon/uquery

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Uquery

Centralizes reusable SQL queries, e.g. for pagination and counting rows.

Installation

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.1"}
  ]
end

Run it in shell:

> mix deps.get

Usage

# 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 pagination(query, page, per_page) do
    UQuery.Paginator.pagination(__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

  ...

  # List example 1
  def list_projects(page, per_page) do
    from(p in Project, select: p)
    |> Repo.paginate(page, per_page)
  end

  # List example 2
  def list_projects(page, per_page) do
    from(p in Project, select: p)
    { query, pagination } = Repo.pagination(page, per_page)
    { Repo.all(query), pagination }
  end

  # Count list
  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.

About

Centralizes reusable queries, e.g. for pagination and counting rows.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages