drizzle-orm icon indicating copy to clipboard operation
drizzle-orm copied to clipboard

[FEATURE]: Better way to count the number of rows in a table

Open Bytedefined opened this issue 1 year ago • 1 comments

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.

Bytedefined avatar Apr 07 '24 11:04 Bytedefined

I like this!

Same with avg, sum, max and min.

skyf0l avatar May 07 '24 09:05 skyf0l

Drizzle ORM now has a feature that easily abstracts the counting rows query: https://orm.drizzle.team/docs/query-utils#count

L-Mario564 avatar Oct 21 '24 17:10 L-Mario564