Kubb Tietoisuus
Kubb Tietoisuus

Reputation: 31

Sails js sort by populated field

I need to sort data from a MySQL database on related table row.

Suppose we have two models:

ModelOne:

module.exports = {

  tableName: 'modelOne',

  attributes: {

     // some atributes.........

     modelTwo{
        model: 'ModelTwo',
        columnName: 'model_two_id'
     }
   }
} 

ModelTwo:

module.exports = {

  tableName: 'modelTwo',

  attributes: {
     // some atributes.........

     model_two_id: {
        type: 'integer',
        autoIncrement: true,
        primaryKey: true
     },
     name: 'string'
   }
}

I would like to do something like:

ModelOne
  .find(find_criteria)
  .populateAll()
  .paginate({page: page, limit: limit})
  .sort('modelTwo.name')
  .then(...)

Is there possibility to do this or I need to write an SQL query without using Waterline functions?

Upvotes: 0

Views: 960

Answers (2)

hlozancic
hlozancic

Reputation: 1499

This is how I do it...

ModelOne
  .find(find_criteria)
  .populate('modelTwo', {sort: 'name ASC'})
  .paginate({page: page, limit: limit})
  .then(...)

As you can see, you can pass {sort} object to populate method.

Upvotes: 1

SkyQ
SkyQ

Reputation: 380

No. Deep-population-sorting is on future list https://github.com/balderdashy/waterline/issues/266

Upvotes: 0

Related Questions