-
-
Notifications
You must be signed in to change notification settings - Fork 190
-
-
Notifications
You must be signed in to change notification settings - Fork 190
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Simplify count() behavior #201
Comments
The array is required since you can have multiple values returned from a count query too. For example.
Returns
Assuming you are 51 rows in the database and also each user has an age. |
Coming from Laravel https://laravel.com/docs/5.5/queries#aggregates They would solve it as (rough syntax): // Get multiple aggregates
await UserRatings.query().select('user_id, avg(rating)').groupBy('user_id').fetch()
// Get single aggregate
await UserRating.query().avg('rating')
await UserRating.query().where('user_id', '=', 42).avg('rating') So here, all aggregates do an auto-fetch and return the single row result. |
Yeah but if you need 2 aggregates in the same query? |
Is this coming from Knex? |
Yes it is |
Ok then I think this goes too deep to modify What about adding a I don't know knex enough, but something like... KnexQueryBuilder.prototype.countRows = async function () {
return (await knex().from(this).count('* as total'))[0].total
} |
Yeah we can do this, maybe KnexQueryBuilder.prototype.rowsCount = async function (field) {
field = field || '*'
return (await knex().from(this).count(`${field} as total`))[0].total
} |
Assign to me I’ll do |
Done 😄 |
Closing since a PR is opened for this feature. |
It seems Adonisjs 5 is missing this feature. There is no |
@thetutlage can you suggest for this?
|
I wonder if the default behavior of
count()
can be simplified. Right now, we need to doWhat if
count(expression = null)
means to use* as total
and return[0].total
from the call?Seems like the vast majority of cases would use this simple form.
The text was updated successfully, but these errors were encountered: