fuelen

fuelen

Composite - a library for writing dynamic queries

Hey
I want to introduce you a small utility that may help in writing dynamic queries :slight_smile:

In a few words, this code:

blank_string? = fn string -> String.trim(string) == "" end
query = some_complex_query()

query =
  if not is_binary(params[:some_field1]) or blank_string?.(params[:some_field1]) do
    query
  else
    query |> where(some_field1: ^params[:some_field1])
  end

query =
  if is_nil(params[:some_field2]) do
    query
  else
    query |> where(some_field2: ^params[:some_field2])
  end

query |> Repo.all()

can be reorganzied to this:

blank_string? = fn string -> String.trim(string) == "" end

some_complex_query()
|> Composite.new(params)
|> Composite.param(:somefield1, &where(&1, some_field1: ^&2),
  ignore?: &(not is_binary(&1) or blank_string?.(&1))
)
|> Composite.param(:some_field2, &where(&1, some_field2: ^&2))
|> Repo.all()

However, library can be used not only with Ecto.
For more complex example with joins check test file.

Currently, it is not published to hex and available only on Github
https://github.com/fuelen/composite
Any feedback is welcome! :slight_smile:

First Post!

thojanssens1

thojanssens1

Aren’t Changesets used for parameter validations?

Most Liked

fuelen

fuelen

For validation - yes, but this library is about optional application.

Last Post!

fuelen

fuelen

Published an article about the library

Where Next?

Trending in Announcing Top

bluzky
You may know https://ui.shadcn.com/, a UI component library for React. I really love it’s design style and components. I’ve built some co...
385 14863 120
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
shahryarjb
The Chelekom project is a library of Phoenix and LiveView components generated via Mix tasks to fit developer needs seamlessly. One of i...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
zachdaniel
Introducing AshStorage! Attachment and file management that slots directly into your resources :smiling_face_with_sunglasses: I had hope...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
akoutmos
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New

We're in Beta

About us Mission Statement