All Projects → DockYard → Inquisitor

DockYard / Inquisitor

Composable query builder for Ecto

Programming Languages

elixir
2628 projects

Labels

Projects that are alternatives of or similar to Inquisitor

Birdsong
🐦🎼 Swift WebSockets client for Phoenix Channels.
Stars: ✭ 124 (-23.46%)
Mutual labels:  phoenix
Littlechat
A peer-to-peer video chat application made using Phoenix, LiveView, and WebRTC. Want to know how it's made? Read the blog post: https://littlelines.com/blog/2020/07/06/building-a-video-chat-app-in-phoenix-liveview
Stars: ✭ 144 (-11.11%)
Mutual labels:  phoenix
Mipha
Proj Elixir Forum build with phoenix 1.5.
Stars: ✭ 153 (-5.56%)
Mutual labels:  phoenix
Grapevine
The MUD Chat Network
Stars: ✭ 131 (-19.14%)
Mutual labels:  phoenix
Ex oauth2 provider
Making OAuth 2 provider and authentication with http bearer as simple as possible for Elixir and Phoenix apps
Stars: ✭ 137 (-15.43%)
Mutual labels:  phoenix
Captain Fact Api
🔎 CaptainFact - API. The one that serves and process all the data for https://captainfact.io
Stars: ✭ 145 (-10.49%)
Mutual labels:  phoenix
Phoenix live dashboard
Realtime dashboard with metrics, request logging, plus storage, OS and VM insights
Stars: ✭ 1,657 (+922.84%)
Mutual labels:  phoenix
Elixir Runtime
The community-supported runtime for Elixir on Google App Engine.
Stars: ✭ 158 (-2.47%)
Mutual labels:  phoenix
Codefund
Deprecated. Please go to https://github.com/gitcoinco/code_fund_ads
Stars: ✭ 140 (-13.58%)
Mutual labels:  phoenix
The Zen Of Elixir
Collection of top articles reflecting the Zen of Elixir
Stars: ✭ 148 (-8.64%)
Mutual labels:  phoenix
Phoenix For Vk
Yet another VK client for Android
Stars: ✭ 131 (-19.14%)
Mutual labels:  phoenix
Grip
The microframework for writing powerful web applications.
Stars: ✭ 137 (-15.43%)
Mutual labels:  phoenix
Phoenix swoosh
Swoosh <3 Phoenix
Stars: ✭ 145 (-10.49%)
Mutual labels:  phoenix
Nova
An attempt to port/rebuild Spree, an open source e-commerce solution, with Elixir and Phoenix.
Stars: ✭ 125 (-22.84%)
Mutual labels:  phoenix
Ex money
A [work-in-progress] self-hosted personal finance app
Stars: ✭ 154 (-4.94%)
Mutual labels:  phoenix
Moba
A turn-based browser RPG built with Phoenix LiveView
Stars: ✭ 120 (-25.93%)
Mutual labels:  phoenix
Rummage phoenix
Full Phoenix Support for Rummage. It can be used for searching, sorting and paginating collections in phoenix.
Stars: ✭ 144 (-11.11%)
Mutual labels:  phoenix
Passport
Provides authentication for phoenix application
Stars: ✭ 159 (-1.85%)
Mutual labels:  phoenix
Veil
Simple passwordless authentication for your Phoenix apps
Stars: ✭ 153 (-5.56%)
Mutual labels:  phoenix
Faker Elixir
💧 FakerElixir generates fake data for you.
Stars: ✭ 147 (-9.26%)
Mutual labels:  phoenix

Inquisitor Build Status

Easily build composable queries for Ecto.

Inquisitor is built and maintained by DockYard, contact us for expert Elixir and Phoenix consulting.

Usage

Adding Inquisitor to a project is simple:

defmodule MyApp.PostController do
  use Inquisitor

  def index(conn, params) do
    posts =
      App.Post
      |> build_query(conn, params)
      |> Repo.all()

    json(conn, posts)
  end
end

After use Inquisitor build_query/3 is added to the MyApp.PostController. It takes a queryable variable, the conn, and the params as arguments.

This sets up a key/value queryable API for the Post model. Any combination of fields on the model can be queried against. For example, requesting [GET] /posts?foo=bar&baz=qux could create the query:

SELECT p0."foo", p0."baz" FROM posts as p0 WHERE (p0."foo" = $1) AND (p0."baz" = $1);

$1 and $2 will get the values of "bar" and "qux",

Security

By default, Inquisitor is an opt-in library. It will not provide any querying access to any key/value pair. The params list will be iterated over and a no-op function is called on each element. You must add custom query handlers that have a higher matching order on a case by case basis.

If you'd like to add a catch-all for any key/value pair you can override the default:

def build_query(query, attr, value, _conn) do
  Ecto.Query.where(query, [r], field(r, ^String.to_existing_atom(attr)) == ^value)
end

However, this is not recommended. Instead you should create a @whitelist module attribute that contains all of they keys you wish to allow access to:

@whitelist ["title", "bio"]

def build_query(query, attr, value, _conn) when attr in @whitelist do
  Ecto.Query.where(query, [r], field(r, ^String.to_existing_atom(attr)) == ^value)
end

This will handle matching keys in the whitelist and all unmatched keys will fall back to the pass-through without affecting the query.

Adding custom query handlers

Use build_query/4 to add key/value pair handlers:

defmodule MyApp.PostsController do
  use Inquisitor

  def index(conn, params) do
    posts =
      App.Post
      |> build_query(params)
      |> Repo.all()

    json(conn, posts)
  end

  def build_query(query, "inserted_at", date, _conn) do
    Ecto.Query.where(query, [p], p.inserted_at >= ^date)
  end
end

Handing fields that don't exist on the model

The keys you query against don't need to exist on the model. Revisiting the date example, let's say we want to find all posts inserted for a given month and year:

def build_query(query, attr, value, _conn) when attr == "month" or attr == "year" do
  Ecto.Query.where(query, [e], fragment("date_part(?, ?) = ?", ^attr, e.inserted_at, type(^value, :integer)))
end

Usage Outside of Phoenix Controllers

To use inside a module other than a Phoenix Controller, you'll need to import Ecto.from/1 otherwise you may see an error like cannot use ^value outside of match clauses.

Note: we use warn: false to suppress an incorrect warning generated by Elixir thinking from is unused.

defmodule MyApp.PlainModule do
  import Ecto.Query, only: [from: 1], warn: false
  use Inquisitor
end

Plugins

We collect all Inquisitor plugins that extend its behavior:

Authors

We are very thankful for the many contributors.

Versioning

This library follows Semantic Versioning

Want to help?

Please do! We are always looking to improve this library. Please see our Contribution Guidelines on how to properly submit issues and pull requests.

Legal

DockYard, Inc. © 2016

@dockyard

Licensed under the MIT license

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].