Aggregation functions (like SUM) with Strapi

Hi, I can’t find anything related to aggregation functions in the v4 documentation ( REST / Entity Service / Query engine).

My assumption is that it the Strapi layer doesnt provide those at this time. So how could I do to customize my controller in the mean time ?
I know that Strapi is built on top of BookshelfJS, which itself uses KnexJS, but I can’t find any examples of how to use those within a Strapi controller.

My usecase is : Get the SUM of all the amount in the table PAYMENT for a given USER

Thanks !

Could you use the API to gather all payment information and calculate a sum that way?

Hi, thank you for your answer ! That could be a solution when I only retrieve the user’s payments and there’s not a lot of them.

Problem is : I also have an admin dashboard, with a view that shows all the payments from all the users in a server-paginated table. In this view I also would like to have stats ( so for example, the sum of all the payments amount from all users in a given time range).

My table is paginated so I cannot use its payload to calculate this amount if I have more than 1 page. I could create another endpoint just to calculate those stats, and use findMany, then loop and sum the amounts manually, but that would be expensive on a relatively big array of payments ? ( like 1000-5000 items ) ?

That’s why I thought about aggregation…

After talking about this on Discord with others, someone added this PR : Average and sum aggregators for Query Engine API by WalkingPizza · Pull Request #12888 · strapi/strapi (github.com)

So wait & see