Skip to content

An elixir package which enables you to write a query functionally on top of ecto.

License

Notifications You must be signed in to change notification settings

vasuadari/ecto_query_plus

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

EctoQueryPlus

Lets you compose ecto queries in a functional way.

Installation

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

def deps do
  [
    {:ecto_query_plus, "~> 0.1"}
  ]
end

Usage

defmodule Accounts.User do
  use Ecto.Schema

  schema "users" do
    field(:name, :string)
    field(:email, :string)
    field(:last_login_at, :native_datetime)
    field(:locked_at, :native_datetime)
  end
end
defmodule Accounts.User.Query do
  import EctoQueryPlus

  alias Accounts.User

  def list(filters, options \\ %{}) do
    User.Query
    |> wherep(filters)
    |> offsetp(options[:offset])
    |> limitp(options[:limit])
    |> selectp(options[:select])
    |> order_byp(options[:order_by])
    |> preloadp(options[:preload])
  end
end

Now, you could simply query like

User.Query
|> list(
  [
    {:name, :not_nil},
    {:email, ["[email protected]"]},
    {:last_login_at, :gt, DateTime.utc_now()},
    {:locked_at, nil}
  ],
  %{
    select: [:id],
    order_by: {:last_login_at, :asc},
    limit: 5,
    offset: 10
  }
)

Available operators to filter by

  • {field, :gte, value}
  • {field, :gt, value}
  • {field, :lt, value}
  • {field, :lte, value}
  • {field, :ilike, pattern}
  • {field, nil}
  • {field, :not_nil}
  • {field, list}

Available options

  • {:select, list}
  • {:offset, integer_value}
  • {:limit, integer_value}
  • {:order_by, {field, :asc}}
  • {:order_by, {field, :desc}}
  • {:preload, list}

License

MIT

About

An elixir package which enables you to write a query functionally on top of ecto.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages