Zip columns and results from Ecto SQL query to a list of maps

0 minute read
defmodule EctoHelper do
  def query2map(Repo, query) do
    {_, res} = Ecto.Adapters.SQL.query(Repo, query)

    columns = 
      res.columns
      |> Enum.map(fn x -> String.to_atom(x) end)
    
    res.rows
      |> Enum.map(&Enum.zip(columns, &1))
      |> Enum.map(&Enum.into(&1, %{}))
  end
end