tattr2
tattr2

Reputation: 13

Running sequelize with two where conditions

I have a mysql db instance with a table consisting of a various fields. Relevant fields are start, start time, and status

If I want to get a list of all entries that don't have status = 'cancelled' and that occur today or after, I would write this:

  return {
  where: {
    status: {
      $ne: 'cancelled'
    },
    $or: {
      start: { $gte: moment().utc().format('YYYY-MM-DD') },
      $and: {
        isRepeating: 1,
        $or: [{
          end: {
            $gte: moment().format(),
          }
        },
        {
          end: {
            $eq: null,
          }
        }]
      },
    }
  },

I am trying to modify this query to not only give me entries that occur today or after, but also greater than right now (time wise, UTC). My attempt was to first filter based on startTime, and then filter based on startDate, but it does not seem to be working:

    return {
  where: {
    status: {
      $ne: 'cancelled'
    },
    $or: {
      startTime: { $gt: moment.utc().format('HH:mm:ss') },
      $and: {
      
          start: { $gte: moment().utc().format('YYYY-MM-DD') },
        
        $and: {
        isRepeating: 1,
        $or: [{
          end: {
            $gte: moment().format(),
          }
        },
        {
          end: {
            $eq: null,
          }
        }]
      }
      },
    }
  },

(does not work, because it just returns everything!)

I also cannot do something more simple like

where: {
startTime: { $gt: moment.utc().format('HH:mm:ss') },
start: { $gte: moment().utc().format('YYYY-MM-DD') },
}

Because then it will ignore, for example, entries that occur tomorrow date wise, but occur earlier in the day than the current timestamp.

Thanks!

Upvotes: 1

Views: 134

Answers (1)

Derek Wang
Derek Wang

Reputation: 10204

You can use Op.and operator to combine those conditions.

const { Op } = require("sequelize");

...
where: {
  [Op.and]: [
    startTime: { $gt: moment.utc().format('HH:mm:ss') },
    start: { $gte: moment().utc().format('YYYY-MM-DD') }
  ]
}
...

Upvotes: 1

Related Questions