Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use "interval" in SequelizeJS?

Tags:

sequelize.js

I need to convert MySQL query below to SequelizeJS query

WHERE createdAt < now() - interval 5 hour
like image 592
Sandesh B Suvarna Avatar asked Apr 26 '17 12:04

Sandesh B Suvarna


1 Answers

I got it working using Sequelize.literal:

Model.find({
  where: {
    [Sequelize.Op.and]: [
      Sequelize.literal(`created_at > NOW() - INTERVAL '5h'`),
    ],
  },
  logging: console.log,
})

I would love if anybody could enrich this answer by mixing in Sequelize.col() so to automatically get the correct column name inside the literal.

like image 161
marcopeg Avatar answered Jan 04 '23 00:01

marcopeg