user3662456
user3662456

Reputation: 265

How to use multiple $or in sequelize

Im writing a query where i need multiple 'or' statements.

this is my whereClause:

{
  date_added: {
    '$lte': '2019-04-18', '$gte': '2019-04-11'
  },
  '$or': [ 
    { id: [Array], secondary_id:[Array] }
  ],
  '$and': [ 
    { third_id: [Array], fourth_id:[Array] }
  ],///make it into $or
}

such that there where statements changes from

WHERE ("date_added" <= '2019-04-18' AND "date_added" >= '2019-04-11') 
  AND ("id" IN ('10') or "secondary_id" in ('11')) 
  AND ("third_id" IN ('12') and "fourth_id" in ('13')) 

INTO

WHERE ("date_added" <= '2019-04-18' AND "date_added" >= '2019-04-11') 
  AND ("id" IN ('10') or "secondary_id" in ('11')) 
  AND ("third_id" IN ('12') OR "fourth_id" in ('13')) 

Another way to say it would how could i use sequelize to do (A or B) and (C or D)

Upvotes: 0

Views: 1016

Answers (1)

doublesharp
doublesharp

Reputation: 27687

You should use the Sequelize operator symbols and not the string values in your example, they have been deprecated. To recreate the query in your SQL, you would want to do something like the following:

const Op = Sequelize.Op

where: {
  date_added: {
    [Op.between]: ['2019-04-18', '2019-04-11'],
  },
  id: {
    [Op.in]: arrayOfIds,
  }
  [Op.or]: [
    {
      channel_type: {
        [Op.like]: {
          [Op.any]: ['facebook', 'instagram'],
        },
      },
    },
  ],
}

SQL

WHERE date_added BETWEEN '2019-04-18' AND '2019-04-11'
  AND id IN (... your id array)
  AND (
    channel_type LIKE 'facebook'
    OR channel_type LIKE 'instagram'
  )

Upvotes: 1

Related Questions