Ordering results of eager-loaded nested models in Node Sequelize

node.jsPostgresqlsequelize.js

node.js Problem Overview


I have a complex set of associated models. The models are associated using join tables, each with an attribute called 'order'. I need to be able to query the parent model 'Page' and include the associated models, and sort those associations by the field 'order'.

The following is having no effect on the results' sort order:

db.Page.findAll({
  include: [{
    model: db.Gallery,
    order: ['order', 'DESC'],
    include: [{
      model: db.Artwork,
      order: ['order', 'DESC']
    }]
  }],
})

node.js Solutions


Solution 1 - node.js

I believe you can do:

db.Page.findAll({
  include: [{
    model: db.Gallery
    include: [{
      model: db.Artwork
    }]
  }],
  order: [
    // sort by the 'order' column in Gallery model, in descending order.

    [ db.Gallery, 'order', 'DESC' ], 


    // then sort by the 'order' column in the nested Artwork model in a descending order.
    // you need to specify the nested model's parent first.
    // in this case, the parent model is Gallery, and the nested model is Artwork

    [ db.Gallery, db.ArtWork, 'order', 'DESC' ]
  ]
})

There are also a bunch of different ways, or things you can do when ordering. Read more here: https://sequelize.org/master/manual/model-querying-basics.html#ordering-and-grouping

Solution 2 - node.js

If you also use 'as' and let's say you want to order by 'createdDate' , the query looks like this:

DbCategoryModel.findAll({
    include: [
        {
            model: DBSubcategory,
            as: 'subcategory',
            include: [
                {
                    model: DBProduct,
                    as: 'product',
                }
            ],
        }
    ],
    order: [
        [
            {model: DBSubcategory, as: 'subcategory'},
            {model: DBProduct, as: 'product'},
            'createdDate',
            'DESC'
        ]
    ]
})

Solution 3 - node.js

order: [
[ db.Sequelize.col('order'), 'DESC'],    /*If you want to order by page module as well you can add this line*/
[ db.Gallery, db.ArtWork, 'order', 'DESC' ]
]

Solution 4 - node.js

This works for me:

let getdata = await categories_recipes.findAll({   
                    order:[ 
                        [{model: recipes, as: 'recipesdetails'},'id', 'DESC'] // change your column name like (id and created_at)
                    ],
                    include:[{
                        model:recipes, as : "recipesdetails",
                        include:[{
                            model:recipe_images, as: "recipesimages",
                        }],
                        where:{
                            user_id:data.id
                        },
                        required: true,
                    }]
                })

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionWandering DigitalView Question on Stackoverflow
Solution 1 - node.jsCalvintwrView Answer on Stackoverflow
Solution 2 - node.jsRelu MesarosView Answer on Stackoverflow
Solution 3 - node.jsShrikantView Answer on Stackoverflow
Solution 4 - node.jsAman KumarView Answer on Stackoverflow