[FEATURE]: Better way to count the number of rows in a table
Describe what you want
Currently, retrieving the count of rows from a specific table involves a somewhat convoluted process that may result in unnecessary nesting. This can be cumbersome, especially when attempting to integrate the count directly into another object.
Proposed Solution: Introduce a more concise and intuitive method for obtaining row counts from database tables without the extra nesting. This would eliminate having to parse/strip the result.
Example:
// Current Method
const usersRegisteredCount = await database.select({usersRegistered: count()}).from(user);
// Returns: usersRegisteredCount: 0
// Improved Method
const usersRegisteredCount = await database.count().from(user);
// Would return: 0
Example from my personal usage:
return {
success: true,
statistics: {
usersRegistered: await database.select({usersRegistered: count()}).from(user),
},
}
The above code would return the below, which is not what I need:
{
success: true,
statistics: {
usersRegistered: [{
usersRegistered: 0
}]
}
}
With the suggested method, I would get the below, which is what I need:
{
success: true,
statistics: {
usersRegistered: 0
}
}
Looking forward to seeing this implemented.
I like this!
Same with avg, sum, max and min.
Drizzle ORM now has a feature that easily abstracts the counting rows query: https://orm.drizzle.team/docs/query-utils#count