la_219

la_219

How do I get the result size of a query in Ecto?

[chat quote=“la_219;384;2023-01-30T12:04:41Z” channel=“Elixir Chat” channelId=“2” multiQuote=“true” chained=“true”]
Just a quick question from an Elixir & Ecto beginner: How do I get the result size of a query? I worked with pagination and that worked like a charm but I also need to give the total size to the frontend. Do I need another query for that or is there a more elegant solution?
[/chat]

[chat quote=“kartheek;385;2023-01-30T12:13:26Z” chained=“true”]
You are using any specific pagination library ? Scrivener has those details in the result.
[/chat]

[chat quote=“la_219;386;2023-01-30T12:20:58Z” chained=“true”]
No, so far not. But if it has enough perks, it might be worth it
[/chat]

[chat quote=“kartheek;387;2023-01-30T12:23:36Z” chained=“true”]
https://github.com/drewolson/scrivener_ecto
[/chat]

[chat quote=“la_219;388;2023-01-30T12:28:39Z” chained=“true”]
thank you!
[/chat]

[chat quote=“zachallaun;389;2023-01-30T13:13:05Z” chained=“true”]
@la_219 it might be worth thinking through the problem even if you use scrivener, just as a learning exercise! The critical thing is that everything boils down to a SQL query, which returns rows of records. Barring something like array_agg and building the structs yourself, you’d need/want to include the count on every row of the returned result. In Ecto, that means you’d add a virtual field to your schema definition, something like total_count. You could then join each returned row on every row, selecting a count of those rows.

Another thing to consider is the database you’re using. If you’re over a network, like Postgres, it’s usually better to jam everything into one query. If you’re using SQLite though, since the DB is local, you can sort of ignore the cost of multiple requests and just structure things in whatever way is easiest to reason about!
[/chat]

[chat quote=“la_219;393;2023-01-30T16:33:24Z” chained=“true”]
Thank you! That’s exactly what I ended up doing. I totally agree that it’s worth to solve a problem (at least in theory) first before using a library. I’m working with postgres so getting everything into one query is definitely the preferred way
[/chat]

Marked As Solved

dimitarvp

dimitarvp

Repo.aggregate(query, :count, :id) should work in most cases (unless your query does not include an id field in its select statement, in which case replace it with another column).

Link to Repo.aggregate docs.

Also Liked

la_219

la_219

thank you, that works like a charm! :slight_smile:

Where Next?

Popular in Questions Top

chokchit
** (DBConnection.ConnectionError) connection not available and request was dropped from queue after 2733ms. You can configure how long re...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
mgjohns61585
Could someone help me? I’m making my first elixir program, number guessing game. I can’t figure out how to convert the user’s guess from ...
New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New

Other popular topics Top

danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 29603 241
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 43487 311
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54120 245
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement