How to make a query with OR condition with Node.js Sequelize?

Sometimes, we want to make a query with OR condition with Node.js Sequelize.

In this article, we’ll look at how to make a query with OR condition with Node.js Sequelize.

How to make a query with OR condition with Node.js Sequelize?

To make a query with OR condition with Node.js Sequelize, we can use Op.or as the key.

For instance, w write

const {
  Op
} = require('Sequelize');

const r = await to(Student.findAll({
  where: {
    LastName: "Doe",
    FirstName: {
      [Op.or]: ["John", "Jane"]
    },
    Age: {
      [Op.between]: [18, 24]
    }
  }
}));

to call findAll with the where property set to an object with the fields we’re filtering by.

And we use the Op.or property set to an array of values to search for entries with FirstName set to 'John' or 'Jane'.

We use Op.between to search for entries with Age between 18 and 24.

Each property in where will be joined together with OR.

Conclusion

To make a query with OR condition with Node.js Sequelize, we can use Op.or as the key.