hubertlepicki

hubertlepicki

Improving Ecto ergonomics in ad-hoc querying scenarios

Last week I spent some time back in Rails world, and ActiveRecord in particular, and I was stunned how many keystrokes it saved me, compared to Ecto.

Not just keystrokes, some decisions are definitely more lax from security standpoint, but come in very handy, especially when making ad-hoc operations from the Rails console.

For example, Ecto is very particular in the data in side of creating queries, which ends up being quite verbose.

In ActiveRecord, we can query records by passing it a hash:

posts = Post.where(title: "Changelog", tags: ["post", "changelog"], published_at: nil)

the IN and IS NULL parts of the query will be infered from the hash.

We can also pass a Hash with string keys, or atom keys, and it’s not fussy about it either.

In Ecto, we have to either compose query by hand, or use keyword list this way:

posts = DB.Repo.query(DB.Post, title: "Changelog", ...)

But that will not work either for the IN clause or and won’t work for the IS NULL either because Ecto strictly enforces that comparison with nil is unsafe… which it kinda is, but on the edge cases like finding user by access token etc., and in a lot of places it’s just fine.

So far I ended up writing some helpers, but these are different between projects. For example, to support NIL values in the params one could write a reducer like this:

query =  Enum.reduce(params, base_query, fn                                                                                                                     
    { key, value}, query when not is_nil(value) ->                                                                                                                         
       from(o in query, where: field(o, ^key) == ^value)    
                                                                                                               
    {key, nil}, query ->                                                                                                                                                  
        from(o in query, where: is_nil(field(o, ^key)))                                                                                                                     
    end)  

The helper could be extended to support arrays etc.

Question is: is there a better way and/or did someone wrote an ActiveRecord-esque library for Ecto that would make working with records from console a bit more Rails-like?

Where Next?

Popular in Discussions Top

Donovan
Hello everyone, I’m so glad to have discovered this awesome community. Thanks for creating it! This is my second post, and apologies for...
New
nunobernardes99
Hi there Elixir friends :vulcan_salute: In a recent task I was on, I needed to check in two dates which of them is the maximum and which...
New
AlexMcConnell
The reason that Rails is as popular as it is is because it’s very easy for relatively inexperienced developers to get a lot of work done....
588 19652 166
New
Crowdhailer
I’ve been hearing much about the new formatter and it’s something I have been keen to try. I find examples buy far the most illuminating...
248 19327 150
New
mbenatti
Following https://github.com/tbrand/which_is_the_fastest |> https://raw.githubusercontent.com/tbrand/which_is_the_fastest/master/imgs...
New
rower687
Hi all, I’ve been reading a lot about the “let it crash” term and how supervising processes and the whole messaging passing make an elixi...
New
rms.mrcs
A couple of days ago I was discussing with a friend about different approaches to write microservices. He said that if he was going to w...
New
joeerl
I’m playing with Elixir - It’s fun. I think @rvirding does give Elixir courses these days. Re: files and database - when I given Erlang ...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
New

Other popular topics Top

New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 43487 311
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 52673 488
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement