Composing Ecto Multi Queries

I love Ecto.Multi but there are few rough edges to my understanding…

  1. Multi.run seems to be the superior option in almost every non-trivial case (i.e. when one operation depends on a previous one in the pipeline). It seems a little awkward, compared to just using a Multi.delete / Multi.update etc.

  2. dynamic operation step naming - I suppose a naive approach suffices, just wondering if anyone has good insight / tips

I found Ecto.Multi.run to be vastly more readable as soon as I put all the anonymous functions it uses into real functions instead of inlining them.

3 Likes

Good pt

FWIW, I ended up wrapping each Multi.run in a function instead, which I really like…

def delete(...), do:    
    Ecto.Multi.new
    |> (get_by_multi ...)
    |> (delete_multi ...)
    |> Example.Repo.transaction  
    |> ...
 
  defp get_by_multi(multi, ....), do:
    multi
    |> (Ecto.Multi.run ... )
 
  defp delete_multi(multi, ...), do:
    multi
    |> (Ecto.Multi.run ... )
1 Like