How to change programmatically field values in middleware to make strapi filter by updated values?

System Information
  • Strapi Version: v4.11.3
  • Operating System: MacOS Sonoma 14.5
  • Database: PostgreSQL
  • Node Version: v16.20.2

Hi!

I’ve created a field “distance” of type number for my Strapi collection “points”.

Now, I give a real value to this field only when the client sends me a “position” (lat/lgn) via Headers; in the collection middleware I map the points from DB and i give to “distance” the calculated value (distance between client location and point’s coordinates").

The problem is that I can’t use ?sort=distance in my API calls becuase obviously the values in DB of distance are still 0 for all points.

There is a way in Strapi to “fit in” in the Api call to make the sort function work? I know I could update all points’ distance value in the middleware but, you know, there would be 300 “update()” calls for each “find()” call!

Thanks in advance,

Davide