Git Product home page Git Product logo

ecto_query_string's Introduction

EctoQueryString

Compose an Ecto.Query with a querystring

Installation

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

def deps do
  [
    {:ecto_query_string, "~> 0.1.0"}
  ]
end

Usage

Say you have the following schemas:

defmodule Foo do
  use Ecto.Schema

  schema "foos" do
    field(:name, :string)
    field(:age, :integer)
    has_many(:bars, Bar)
  end
end

defmodule Bar do
  use Ecto.Schema

  schema "bars" do
    field(:title, :string)
    field(:likes, :integer)
    belongs_to(:foo, Foo)
  end
end

You can do things like this:

query = Ecto.Query.from(user in User)
query_string =  "username=mrmicahcooper&greater:age=18&limit=10"
EctoQueryString.query(query, query_string)

And get:

Ecto.Query.from(u0 in User,
  where: u0.age > ^"18",
  where: u0.username == ^"mrmicahcooper",
  limit: ^"10"
)

Here is the full DSL

# Basic Queries
"name=micah"                => where: foo.name = ^"micah"
"name=micah,bob"            => where: foo.name in ^["micah", "bob"]
"!name=micah"               => where: foo.name != ^"micah"
"!name=micah,bob"           => where: foo.name not in ^["micah", "bob"]
"like:foo=bar*"             => where: like(x.foo, ^"bar%")
"like:foo=*bar"             => where: like(x.foo, ^"%bar")
"like:name=*micah*"         => where: like(foo.name, ^"%micah%")
"ilike:name=micah*"         => where: ilike(foo.name, ^"micah%")
"ilike:name=*micah"         => where: ilike(foo.name, ^"%micah")
"ilike:foo=*bar*"           => where: ilike(x.foo, ^"%bar%")
"less:age=99"               => where: foo.age < 99
"greater:age=40"            => where: foo.age > 40
"range:age=40:99"           => where: foo.age < 99 and foo.age > 40
"or:name=micah"             => or_where: foo.name = ^"micah"
"or:name=micah,bob"         => or_where: foo.name in ^["micah", "bob"]
"!or:name=bar"              => or_where: foo.name != ^"bar"
"!or:name=micah,bob"        => or_where: foo.name not in ^["bar", "baz"]
"select=foo,bar"            => select: [:foo, :bar]
"fields=foo,bar"            => select: [:foo, :bar]
"limit=.:99"                => limit: 99
"offset=40:."               => offset: 40
"between=40:99"             => offset: 40, limit: 99
"order=foo,-bar,baz"        => order_by: [asc: :foo, desc: :bar, asc: :baz]
#Incorporating Associated Tables
"bars.title=micah"          => join: bars in assoc(foo, :bars), where: bars.title = ^"micah"
"bars.title=micah,bob"      => join: bars in assoc(foo, :bars), where: bars.title in ^["micah", "bob"]
"!bars.title=micah"         => join: bars in assoc(foo, :bars), where: bars.title != ^"micah")
"!bars.title=micah,bob"     => join: bars in assoc(foo, :bars), where: bars.title not in ^["micah", "bob"])
"like:bars.title=micah*"    => join: bars in assoc(foo, :bars), where: like(bars.title, ^"bar%")
"like:bars.title=*micah"    => join: bars in assoc(foo, :bars), where: like(bars.title, ^"%bar")
"like:bars.title=*micah*"   => join: bars in assoc(foo, :bars), where: like(bars.title, ^"%bar%")
"ilike:bars.title=micah*"   => join: bars in assoc(foo, :bars), where: ilike(bars.title, ^"micah%")
"ilike:bars.title=*micah"   => join: bars in assoc(foo, :bars), where: ilike(bars.title, ^"%micah")
"ilike:bars.title=*micah* " => join: bars in assoc(foo, :bars), where: ilike(bars.title, ^"%micah%")
"less:bars.likes=99"        => join: bars in assoc(foo, :bars), where: bars.likes < 99
"greater:bars.likes=99"     => join: bars in assoc(foo, :bars), where: bars.likes > 99
"range:bars.likes=40:99"    => join: bars in assoc(foo, :bars), where: bars.likes< 99 and bars.likes > 40
"or:bars.title=micah"       => join: bars in assoc(foo, :bars), or_where: bars.title == ^"micah"
"or:bars.title=micah,bob"   => join: bars in assoc(foo, :bars), or_where: bars.title in ^["micah", "bob"
"!or:bars.title=micah"      => join: bars in assoc(foo, :bars), or_where: bars.title != ^"micah"
"!or:bars.title=micah,bob"  => join: bars in assoc(foo, :bars), or_where: bars.title not in ^["micah", "bob"
"select=email,bars.title"   => join: bars in assoc(foo, :bars), select: [{:bars, [:title]}, :email], preload: [:bars]

Caveats

When using select - In order to hydrate the schema, you must always at least select=id from every schema. Even nested schemas would need at least select=id,foos.id

When using order - You cannot not (currently) order by nested fields.

ecto_query_string's People

Contributors

mrmicahcooper avatar plicjo avatar

Stargazers

Roman avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.