Hello,
I’m going through the Ecto tutorial at: Querying · Elixir School
I would like to know if there’s an easy way to display query results in tabular form, in an iex shell. When iterating on queries, I would prefer to see the results that way, rather than looking at structs.
In rails land, the hirb gem captures the console output and does the job. Does anything similar exists with Elixir?
Digging a bit, I found the format_table()
utility function provided by Ecto. It expects data of this shape:
iex(1)> %{columns: ["id", "title"], rows: [[1, "Ready Player One"], [2, "Inception"]]} |> Ecto.Adapters.SQL.format_table() |> IO.puts()
+----+------------------+
| id | title |
+----+------------------+
| 1 | Ready Player One |
| 2 | Inception |
+----+------------------+
Which works great with this function call:
iex(2)> Repo.query("select m.id, title, tagline, ma.actor_id, a.name from movies as m INNER JOIN movies_actors as ma on ma.movie_id = m.id INNER JOIN actors as a on ma.actor_id = a.id") |> elem(1) |> Ecto.Adapters.SQL.format_table() |> IO.puts()
09:44:39.299 [debug] QUERY OK db=1.0ms queue=0.7ms idle=1309.3ms
select m.id, title, tagline, ma.actor_id, a.name from movies as m INNER JOIN movies_actors as ma on ma.movie_id = m.id INNER JOIN actors as a on ma.actor_id = a.id []
+----+------------------+-----------------------------+----------+----------------+
| id | title | tagline | actor_id | name |
+----+------------------+-----------------------------+----------+----------------+
| 1 | Ready Player One | Something about video games | 1 | Tyler Sheridan |
| 1 | Ready Player One | Something about video games | 2 | Gary |
+----+------------------+-----------------------------+----------+----------------+
However, it’s rather cumbersome when using the Ecto.Query
module:
iex(3)> %{columns: ["id", "title"], rows: Repo.all(from(m in Movie, where: m.id < 100, select: [m.id, m.title]))} |> Ecto.Adapters.SQL.format_table() |> IO.puts()
+----+------------------+
| id | title |
+----+------------------+
| 1 | Ready Player One |
| 2 | Inception |
+----+------------------+
Do you have any tips?