Search code examples
node.jssequelize.jssequelize-cli

How to run AND and Or operator with Sequelize in Node.js


I am new to Node.js, am trying to run a 'And' and 'OR' operator in Sequelize, the MySQL query is this

SELECT * from User WHERE (role = 'INSTRUCTOR') AND ((pix <> null) OR (description <> null)) OEDER BY id DESC

The above MySQL query is what I want to run with Sequelize but it didn't work.

Below is my Sequelize code:

return await models.User.findAll({
        where: {role: 'INSTRUCTOR'}, [Op.or]: [{pix: {[Op.ne]: null}}, {description: {[Op.ne]: null,}}], order: [['id', 'DESC']]
      })

How can I run that query in Sequelize?


Solution

  • Formatting the code can sometimes help ... Seems you had a misplaced }

    return await models.User.findAll({
            where: {
                role: { [Op.eq]: 'INSTRUCTOR' },
                [Op.or]: [
                     { pix: { [Op.ne]: null} }, 
                     { description: { [Op.ne]: null } }
                ]
            }, 
            order: [ ['id', 'DESC'] ]
          })