K
Kysely9mo ago
djMax

How can I get total row count without blowing up types?

I have a simple query function that is in charge of generating a list of ids so that it can be passed into a function which does some other fancy stuff with those ids as a CTE. Like so:
export async function getByExternalId(
db: Kysely<DB>,
partner: string,
externalId: string,
) {
return getWithDataByIds(db, db => db.selectFrom('some_table')
.innerJoin('some_other_table', 'some_table.provider_id', 'some_other_table.provider_id')
.select('some_id')
.where('external_id', '=', externalId)
.where('name', '=', partner));
}
export async function getByExternalId(
db: Kysely<DB>,
partner: string,
externalId: string,
) {
return getWithDataByIds(db, db => db.selectFrom('some_table')
.innerJoin('some_other_table', 'some_table.provider_id', 'some_other_table.provider_id')
.select('some_id')
.where('external_id', '=', externalId)
.where('name', '=', partner));
}
Now, the problem is I also want to get the total row count with COUNT(*) OVER() so that I don't have to make two trips. But if I try to add sql to the select list to get that value, the type system loses its mind... Can I somehow cast the sqlCOUNT(*) OVER() to behave as if the query is going to return a column with the name total and make it not worry about how?
Solution:
I guess maybe this works?
.select(['some_id', (eb) => eb.fn.count('some_id').over().as('total')])
.select(['some_id', (eb) => eb.fn.count('some_id').over().as('total')])
...
Jump to solution
1 Reply
Solution
djMax
djMax9mo ago
I guess maybe this works?
.select(['some_id', (eb) => eb.fn.count('some_id').over().as('total')])
.select(['some_id', (eb) => eb.fn.count('some_id').over().as('total')])
Want results from more Discord servers?
Add your server