Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strapi v3 deep sort

Tags:

strapi

Let's say I have modelA with a ForeignKey on modelB.

In api/modelA/services/modelA.js, I can define:

// [...]
module.exports = {
  /**
   * Promise to fetch all records
   *
   * @return {Promise}
   */
  find (params, populate) {
    params = {
      ...params,

      _sort: 'field_from_model_a:DESC,other_field_model_a:ASC'
    }
    populate = [
      'modelB'
    ]
    return strapi.query('post').find(params, populate)
  },
// [...]

This allows me to sort my query and I can also filter deep relations when using

curl -X GET "http://localhost:1337/modelA?modelB.field_from_model_b=example_value

But I can't find how to sort on modelB.some_field.

If I write _sort: 'modelB.field_model_b:ASC', I get an error saying: missing FROM-clause entry for table "modelB"

like image 400
abrunet Avatar asked Dec 05 '25 18:12

abrunet


1 Answers

Indeed, since Strapi release v3.4.2, it appears you can deep sort your queries. Yay !

You can find an example in the tests here of the strapi project.

Here is the syntax (as expected in my initial question):

{
  // ...
  _sort: 'modelB.field_model_b:ASC'
}
like image 156
abrunet Avatar answered Dec 10 '25 00:12

abrunet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!