How to execute a query based on the "date" column in sqlite3 using sequelize?

I am using Sequelize with sqlite3, but I'm having problems querying data based on date type columns.

Here is my model definition:

var sensorData = sequelize.define('Data', 
{
    time: Sequelize.DATE,
    value: Sequelize.FLOAT,
    type: Sequelize.STRING,
    source: Sequelize.STRING
},
{
    timestamps : false
});

Now I want to get all records with "time" after a certain time in this function:

function selectData(start_date, num_of_records, callback)
{
    sensorData.findAll({
        limit: num_of_records, 
        where: [time, '???'], <====== How to specify the condition here?
        order: 'time'}).success(callback);
}

I cannot find anything on the Sequelize website about requesting a query based on date type.

Any idea?

+4
source share
2 answers

Just as Sumya says you can use $gt, $lt, $gteor $ltewith the date:

model.findAll({
  where: {
    start_datetime: {
      $gte: new Date()
    }
  }
})
+3
source

You can use sequelize.fnas follows:

where:{timestamp:    gte:sequelize.fn('date_format', fromDate, '%Y-%m-%dT%H:%i:%s')
            ,lte:sequelize.fn('date_format', toDate, '%Y-%m-%dT%H:%i:%s')
}
0

Source: https://habr.com/ru/post/1538823/


All Articles