Skip to content

Conversation

@WalkingPizza
Copy link
Contributor

What does it do?

Adds the avg and sum functions to the Query Engine API.

Why is it needed?

Just like the count aggregator, 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?

  1. Create a Test content type, with a numeric field called value;
  2. Create a random amount of entries of type Test;
  3. Run strapi.db.query('api::data.data').sum("value"); and notice how it returns the sum of those values;
  4. Run strapi.db.query('api::data.data').avg("value"); and notice how it returns the average of those values.

@WalkingPizza
Copy link
Contributor Author

More than happy to write some documentation and submit a PR if this gets accepted.

@WalkingPizza WalkingPizza changed the title Feature/avg sum aggregators Average and sum aggregators for Query Engine API Mar 20, 2022
@codecov
Copy link

codecov bot commented Mar 20, 2022

Codecov Report

Merging #12888 (792462c) into master (f880ec4) will decrease coverage by 0.18%.
The diff coverage is 0.00%.

@@            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     
Flag Coverage Δ
front ?
unit 47.60% <0.00%> (-0.19%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
packages/core/database/lib/entity-manager.js 3.65% <0.00%> (-0.18%) ⬇️
packages/core/database/lib/entity-repository.js 6.38% <0.00%> (-0.60%) ⬇️
packages/core/database/lib/query/query-builder.js 1.97% <0.00%> (-0.15%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update f9fca8a...792462c. Read the comment docs.

@alexandrebodin alexandrebodin self-assigned this Mar 21, 2022
@strapi-bot
Copy link

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

@WalkingPizza
Copy link
Contributor Author

@alexandrebodin did you have a chance to look at this? 👀

@alexandrebodin alexandrebodin added issue: enhancement Issue suggesting an enhancement to an existing feature source: core:database Source is core/database package labels May 20, 2022
@alexandrebodin
Copy link
Member

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.

@alexandrebodin
Copy link
Member

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 🎉

@jerodfritz
Copy link
Contributor

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?

@strapi-bot
Copy link

This pull request has been mentioned on Strapi Community Forum. There might be relevant details there:

https://forum.strapi.io/t/aggregation-queries/26380/2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

issue: enhancement Issue suggesting an enhancement to an existing feature source: core:database Source is core/database package

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants