Categories
JavaScript Answers

How to set the Sequelize findAll sort order in Node.js?

Spread the love

Sometimes, we want to set the Sequelize findAll sort order in Node.js.

In this article, we’ll look at how to set the Sequelize findAll sort order in Node.js.

How to set the Sequelize findAll sort order in Node.js?

To set the Sequelize findAll sort order in Node.js, we can set the order property.

For instance, we write

const getStaticCompanies = () => {
  return Company.findAll({
    where: {
      //...
    },
    order: [
      ['id', 'DESC'],
      ['name', 'ASC'],
    ],
    attributes: ['id', 'logo_version', 'logo_content_type', 'name', 'updated_at']
  });
};

to call Company.findAll with an object that has the order property set an array to the columns and how we want to order them.

Conclusion

To set the Sequelize findAll sort order in Node.js, we can set the order property.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *