Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sequilize: How to get record with createdat greater than 1 hour

I am new in sequelize, and I don't know how to get the records with createdAt column less than 1 hours with the current date and here is my code :

models.Trip.findAll({
    where: {
      createdAt: {
        $gt:  -------> createdAt <= 1 hour with current date
      }
    },
    include: [
      models.User,
      models.Vehicle,
    ],
  })

How to do that, please help me. Thanks!

like image 330
beginerdeveloper Avatar asked Jan 01 '23 20:01

beginerdeveloper


1 Answers

Use sequelize.literal() to query based on the database server time.

Using the server:

created_at: {
  [Op.gt]: sequelize.literal("NOW() - INTERVAL '24 HOURS'")),
}

Use a Date object set to one hour in the past by subtracting milliseconds from Date.now().

Using Sequelize.Op

created_at: {
  [Op.gt]: new Date(Date.now() - (60 * 60 * 1000)),
},

Using old style operator aliases:

created_at: {
  $gt: new Date(Date.now() - (60 * 60 * 1000)),
},
like image 180
doublesharp Avatar answered Jan 05 '23 07:01

doublesharp