Is this the proper way to do a count query?

rowsCount: (
await db
.select({ value: countDistinct(inventoryItems.id) })
.from(inventoryItems)
.where(whereFilter)
)[0].value
rowsCount: (
await db
.select({ value: countDistinct(inventoryItems.id) })
.from(inventoryItems)
.where(whereFilter)
)[0].value
seems kinda weird to index 0 like that
5 Replies
Angelelz
Angelelz11mo ago
Why do you need a countDistinct?
jakeleventhal
jakeleventhalOP11mo ago
wouldnt count distinct be what you want in most cases?
Angelelz
Angelelz11mo ago
Not if you're counting unique ids It's adding overhead for nothing Anyway, that looks good to me You could use .then() which would be basically the same thing Also, if it's a unique id, you can just do count() because every row will have an id
jakeleventhal
jakeleventhalOP11mo ago
does count() implicitly take in the primary key as the arg
Angelelz
Angelelz11mo ago
count() returns count(*) on the database And that counts every row performantly
Want results from more Discord servers?
Add your server