Finding all records that are greater than 30 minutes old in Sequelize

Viewed 90

In Sequelize, how can I:

Only return entries that are at least 30 minutes old or older (as indicated by their created_at datetime field).

This isn't working properly (using postgreSQL).

where: {
  [Op.and]: [
    {
      active: true,
    },
    {
      updated_at: { // DataTypes.DATE

        // Return only if this datetime value is >= 30 minutes

        [Op.gte]: Sequelize.literal("NOW() - (INTERVAL '30 MINUTE')"), 
      },
    }
}

Does not seem to be working properly.

Should I be using EXTRACT or taking the diff between the updated_at and new Date(updated_at) - 30?

Sequelize.where(
  Sequelize.literal(`EXTRACT(MINUTE FROM TIMESTAMP '???')`),
  {
    [Op.gte]: 30,
  }
 )
1 Answers

The first query updated_at: { [Op.gte]: Sequelize.literal("NOW() - (INTERVAL '30 MINUTE')"),} is specifying updated_at >= new() - interval '30 minute', i.e. entries that have been updated in the last 30 minutes.

To get the old entries you should take the entries that have been modified before 30 that time, e.g. updated_at: { [Op.lt]: Sequelize.literal("NOW() - (INTERVAL '30 MINUTE')")}

Regarding the second attempt

EXTRACT(minute from date) will give only the minute part of the date.

Assuming that extract(minute from updated_at){ [Op.gte]:30 } generates WHERE extarct(minute from updated_at) >= 30, this will get all the rows from where the minute component of the timestamp is greater than 30, e.g. 11:34, 15:55, etc.

Related