Categories
JavaScript Answers

How to make join queries using Sequelize on Node.js?

Spread the love

Sometimes, we want to make join queries using Sequelize on Node.js.

In this article, we’ll look at how to make join queries using Sequelize on Node.js.

How to make join queries using Sequelize on Node.js?

To make join queries using Sequelize on Node.js, we can use the include option with findAll.

For instance, we write

const posts = await Posts.findAll({
  include: [{
    model: User,
    required: true
  }]
})

//...

to call Posts.findAll with an object that has the include property set to an array with the model set to User to join the User entity when we make a query for Posts.

required is set to true so we do an inner join.

To do a left outer join, we set required to false.

Conclusion

To make join queries using Sequelize on Node.js, we can use the include option with findAll.

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 *