K
Kysely•2y ago
ohmi

Asserting type of .countAll() (MySQL)

I've noticed that the return type from the result of aggregation functions like countAll() and sum() are string | number | bigint. I assume this is in-case the number returned is too large to be stored in a JS "number." In my case, it appears to be being returned as strings. Is it possible to assert it as a number so that I don't need to wrap every query in a parseInt(result as string, 10)?
Solution:
nvm got it ```ts await db .selectFrom("player_stats") .where("last_active", ">", dateThreshold)...
Jump to solution
3 Replies
ohmi
ohmi•2y ago
Followup question, why is string a possibility when we can express larger numbers in bigint?
Solution
ohmi
ohmi•2y ago
nvm got it
await db
.selectFrom("player_stats")
.where("last_active", ">", dateThreshold)
.select((eb) => eb.fn.countAll<number>().as("count"))
.executeTakeFirstOrThrow()
await db
.selectFrom("player_stats")
.where("last_active", ">", dateThreshold)
.select((eb) => eb.fn.countAll<number>().as("count"))
.executeTakeFirstOrThrow()
Igal
Igal•2y ago
Hey 👋 Kysely's API is dialect and underlying driver agnostic, by design. It doesn't do any data transformation, leaving everything to the underlying driver. Also note, some aggregate functions are nullable in some extreme edge cases. You can provide a nullable generic type too.
Want results from more Discord servers?
Add your server