Which ecto query |> Repo.all style is more readable?

Which code style do you think is more readable/elegant?

1. Query in variable

query = from p in Post,
  select: p.title

Repo.all(query)

2. Query pipelined into Repo

from(p in Post,
  select: p.title
)
|> Repo.all

3. Query inline Repo call

Repo.all(
  from p in Post,
    select: p.title
)
  • Query in variable
  • Query pipelined into Repo
  • Query inline Repo call

0 voters

I have chosen the first one. Anyway, I would choose the second one but with pipe syntax.

Post 
|> select([p], p.title)
|> Repo.all()

For me is much more easy to understand, but the sql-styled query syntax seems to be the preference of Elixir community.

7 Likes

it’s convenient when you want to extract parts of query to functions, i like piping in general, i hate pipe style query syntax, especially when multiple tables are referenced. If only the syntax was not so weird, i’d use pipe style queries

1 Like

I most often do the first because I tend to change a query in multiple steps that doesn’t pipe well (it would with a monadic pipe though, but elixir’s standard library lacks that very common feature). If it is truly a super short single expression query like that then I’d generally do 2.