Thanks for the reply, will do! Some more questions:
1. Even if I wasn't using jsonb, but just doing a JOIN with custom fields on the joined tables, do you envision Ecto supporting a view-like representation of enumerated columns, or alternatively all columns from all join tables, embedded on the same structure?
(Similar to Rails some_scope.joins(:something_else).select("something_else.foo as something_else_bar"), object.something_else_bar becomes available on each instance, whether or not it exists on schema or not). This makes building flat reports from composite queries easier.
2. For jsonb, it'd also be nice to have data->object syntax in query to pull specific fields from the data as regular fields in the returned data.
Currently I achieved both of the above by hand using:
sql = """
SELECT
child_entities.data->>'name' AS child_object_name,
parent_entities.data->>'name' AS parent_object_name
FROM entities child_entities
INNER JOIN entities parent_entities
ON (child_entities.data->'parent_id') = (parent_entities.data->'id')
ORDER BY child_entities.data->>'name'
"""
{_, result} = Ecto.Adapters.SQL.query(Repo, sql, %{})
entities = result[:rows] |>
Stream.map(&Stream.zip(result[:columns], &1) |>
Enum.into(%{})) |>
Enum.to_list
Returning:
[
%{
"child_entity_id" => 1,
"child_object_name" => "foo",
"parent_entity_id" => 2,
"parent_object_name" => "bar"
},
...
]
This seems to work, but obviously requires a lot of hand-crafted parsing into something I can actually output as JSON API. And I'm not sure how optimized the above code is. :)
Eugene