fuelen
Composite - a library for writing dynamic queries
Hey
I want to introduce you a small utility that may help in writing dynamic queries ![]()
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! ![]()
First Post!
thojanssens1
Most Liked
fuelen
Last Post!
fuelen
Trending in Announcing
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...
New
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
The Chelekom project is a library of Phoenix and LiveView components generated via Mix tasks to fit developer needs seamlessly.
One of i...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
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
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
Introducing AshStorage! Attachment and file management that slots directly into your resources :smiling_face_with_sunglasses:
I had hope...
New
Other Trending Topics
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
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
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
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
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
@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
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #elixirconf-us
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex









