-
-
Notifications
You must be signed in to change notification settings - Fork 9.2k
Average and sum aggregators for Query Engine API #12888
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
Average and sum aggregators for Query Engine API #12888
Conversation
|
More than happy to write some documentation and submit a PR if this gets accepted. |
Codecov Report
@@ Coverage Diff @@
## master #12888 +/- ##
==========================================
- Coverage 47.78% 47.60% -0.19%
==========================================
Files 231 231
Lines 8619 8653 +34
Branches 1922 1928 +6
==========================================
Hits 4119 4119
- Misses 3701 3733 +32
- Partials 799 801 +2
Flags with carried forward coverage won't be shown. Click here to find out more.
Continue to review full report at Codecov.
|
|
This pull request has been mentioned on Strapi Community Forum. There might be relevant details there: https://forum.strapi.io/t/aggregation-functions-like-sum-with-strapi/16822/4 |
|
@alexandrebodin did you have a chance to look at this? 👀 |
|
Hey @WalkingPizza Thank you for adding those. We implemented the count as a root-level function because it's so common but the idea for more general aggregations would be to expose sth similar to the below example so we can support a custom mix of filters / groupBy etc and make multiple aggregates at once. strapi.db.query.aggregate(
aggs: {
avg: {
'field': true
},
sum: {
'otherField': true
}
}
)This is far from a final version that would go through RFC but that's a snippet of the idea. |
|
As mentioned previously, we don't intend to expose aggregations one by one for now and we are not planning on working on this for the time being. I'll close the PR for now and hopefully we can use some of it's logic for future plans. Thank you for contributing so much @WalkingPizza 🎉 |
|
Thank you for taking a first step towards this @WalkingPizza. I have a similar use-case now that desperately needs this functionality. Iterating over the result sets to perfom these calculations is just not feasible as the dataset grows. I'd be willing to sponsor your contribution. Do you think you could adapt the aggregates to how @alexandrebodin sees this fitting into the bigger picture? |
|
This pull request has been mentioned on Strapi Community Forum. There might be relevant details there: |
What does it do?
Adds the
avgandsumfunctions to the Query Engine API.Why is it needed?
Just like the
countaggregator, it is sometimes useful to retrieve sums and/or averages from the database directly instead of fetching entries and executing operations over them manually.How to test it?
Testcontent type, with a numeric field calledvalue;Test;strapi.db.query('api::data.data').sum("value");and notice how it returns the sum of those values;strapi.db.query('api::data.data').avg("value");and notice how it returns the average of those values.