node.jssequelize.jsnestjssequelize-typescript

Sequelize Op.or with two Op.and


I need get WHERE query like this:

WHERE (x = 1 AND y = 2) OR (x = 2 AND y = 1)

In sequelize i write:

where: {
          [Op.or]: {
            [Op.and]: {
              ownerId: candidate.recipientId,
              recipientId: candidate.senderId,
            },
            [Op.and]: {
              recipientId: candidate.recipientId,
              ownerId: candidate.senderId,
            },
          },
        },

But i get:

WHERE (("Chat"."recipient_id" = 2 AND "Chat"."owner_id" = 1))

Solution

  • Try this way:

    where: {
      [Op.or]: [{
        ownerId: candidate.recipientId,
        recipientId: candidate.senderId,
      },{
        recipientId: candidate.recipientId,
        ownerId: candidate.senderId,
      }]
    }