K
KyselyAdophilus

Is it possible to get the total count while fetching rows in a single query?

I have a query which looks like this:
const { items, count } = await db
.selectFrom(eb => eb
.selectFrom("ads")
.selectAll()
.$if(filters?.is_active !== undefined, eb => eb.where("is_active", "=", filters?.is_active as AdsTable["is_active"]))
.$if(filters?.is_expired !== undefined, eb => eb.where(sql`expires > now()`))
.offset((paginationOptions.page - 1) * paginationOptions.perPage)
.limit(paginationOptions.perPage)
.as("items"))
.select(() => [
sql<number>`cast(count(1) as int)`.as("count"),
sql<Selectable<AdsTable>[]>`coalesce(json_agg(items), '[]')`.as("items")
])
.executeTakeFirstOrThrow()
const { items, count } = await db
.selectFrom(eb => eb
.selectFrom("ads")
.selectAll()
.$if(filters?.is_active !== undefined, eb => eb.where("is_active", "=", filters?.is_active as AdsTable["is_active"]))
.$if(filters?.is_expired !== undefined, eb => eb.where(sql`expires > now()`))
.offset((paginationOptions.page - 1) * paginationOptions.perPage)
.limit(paginationOptions.perPage)
.as("items"))
.select(() => [
sql<number>`cast(count(1) as int)`.as("count"),
sql<Selectable<AdsTable>[]>`coalesce(json_agg(items), '[]')`.as("items")
])
.executeTakeFirstOrThrow()
I noticed that the 'count' only counts the result of the query and not all the rows in the table. Is it possible to make it to count all the rows in the table? I'm using PG
thelinuxlich
thelinuxlich46d ago
Maybe use a window function?
Adophilus
Adophilus43d ago
How can I do that? Because I had to separate them into 2 queries One to get the data And the other to get the count
Igal
Igal43d ago
It requires some smelly hackery, if you want a single count (not repeated per row)
thelinuxlich
thelinuxlich43d ago
not repeated per row would be a CTE
Want results from more Discord servers?
Add your server
More Posts
Separating results of join into objects of each typeHey guys, I'm curious if anyone knows a way to unmerge the results of a join into objects of both tySnippet compilationHi, is it somehow possible to get only part of the compiled code to use it as snippet in ORM? Or vicCreating an 'enum' type columnHi all, Trying to re-create this MySQL in Kysely ``` ... CREATE TABLE ...( ... members ENUM('femalError when destructuring QueryCreator in withRecursiveI'm new to Kysely and attempting to migrate a Next app from prisma to kysely. I've generated types, Is `sql.join` the best way to concatenate sql templates?I have some business logic that incrementally builds up a sql WHERE query as a string. It looks likeMultiple calls to values in insert statement?I just had a production bug where I was calling `.values` multiple times on a query object. I've fix0.27.3 is out. Didn't have time to write0.27.3 is out. Didn't have time to write release notes yet since it was a hotfix to support typescriusing pgvector with KyselyFollowing the example here: https://github.com/pgvector/pgvector-node?darkschemeovr=1#kysely But I Static/reusable custom window functionsHello, I'm trying to buld a properly typed `array_agg` window function and would like to reuse the eIs the Kysely main site down?I just want to confirm if Kysely's main site is downOn Conflict do update set ALL to be inserted columnsHere I am once again with a question for the on conflict bit in postgres. Would there be a way to auHow to use kysely in edge runtime (like NextJS 14 middleware)?Error: The edge runtime does not support Node.js 'crypto' module. Learn More: https://nextjs.org/dochow to write not (array1 && array2)Hi again, how to write following code? I would love to use not(expression) Expected result: ``` noPartial compileHello everyone, is it possible (somehow) to generate only part of the compiled query? I have an edgeQuerying on jsonArrayFromHi. So i need to fetch posts with comments from a database that are written by a set group of authorWhy does `.stream()` return a single result when I pass in a `chunkSize` option ?I'm guessing it returns `chunkSize` mount of rows for each iteration, but shouldn't it be an array iIs there a way to keep full date precision?It appears that selecting dates from Postgresql loses some precision, which wreaks havoc with my pag