Getting results and count in one query


Is there a good way of getting the results from ecto along with the number of records returned in one query?

The result I’m looking for would be something like:
%{results: [%Module{id: 1}, %Module{id: 2}], count: 2}

results = Repo.all(query)
%{results: results, count: length(results)}

Doing the same in postgres directly is klunky at best, as you’d need to just select a single row to be able to get a count, which would mean the results would need to be selected as an array of data in said one result row.

1 Like

Ah! Honestly never thought about using length/1 for the returned list! I was too focused on getting the count directly from the database. Thank you!