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

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.