Bind

Flexible and dynamic Ecto query builder for Elixir applications, allowing developers to retrieve data flexibly without writing custom queries for each use case.

Installation

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

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

API

Bind.query(schema, params)

Parameters:

Returns: An Ecto query.

Usage Example

Create Ecto query:

query = Bind.query(MyApp.User, %{ "name[eq]" => "Alice", "age[gte]" => 30 })

Alternatively, with a query string:

query = Bind.query(MyApp.User, "?name[eq]=Alice&age[gte]=30")

And finally run the query to get results from the database:

results = Repo.all(query)

Error handling

case Bind.query(MyApp.User, %{ "name[eq]" => "Alice", "age[gte]" => 30 }) do
{:error, reason} ->
IO.puts("Error building query: #{reason}")
query ->
results = Repo.all(query)
end

Filtering

Examples:

%{"name[eq]" => "Alice", "age[gte]" => 30}
%{
"name[starts_with]" => "A",
"age[gte]" => 18,
"role[in]" => "superuser,admin,mod",
"is_active[true]" => "",
"last_login[nil]" => false
}

List of comparison operators supported:

Sorting

Use the sort parameter to specify sorting order:

params = %{"sort" => "-age"} # Sort by age descending

Pagination

Example:

params = %{"limit" => 20, "start" => 100}

Query String Support

Bind can also pass URL query strings:

query_string = "?name[eq]=Alice&age[gte]=30&sort=-age&limit=10"
query = Bind.query(MyApp.User, query_string)