How can I use datediff of mysql with sequelizejs

I’m trying to implement this SQL to my Sequelize

SELECT file_id FROM table WHERE datediff(curdate(),create_date) > 5;

   Here is my Sequelize

   findOverPeriodFile: function () {
    return table.findAll({
        where:{
            60: {lt: Sequelize.fn('')}
        }
    });
}

Im new to Seuelize and I have tried to search Google, but it doesn’t help. Does anyone have an answer to this question? I don’t know what to put in the WHERE statement. Sorry for my bad English.

Answer

Here you go :

You can generate WHERE datediff(curdate(),create_date) > 5; by using :

{ 
    where: sequelize.where(sequelize.fn('datediff', sequelize.fn("NOW") , sequelize.col('create_date')), {
        $gt : 5 // OR [Op.gt] : 5
    })
}

For adding more condition

{
    where: {
        $and : [
            sequelize.where(sequelize.fn('datediff', sequelize.fn("NOW") , sequelize.col('create_date')), {
                $gt : 5 // OR [Op.gt] : 5
            }) ,
            { status : 'mystatus' }
        ]
    }
} 

Leave a Reply

Your email address will not be published. Required fields are marked *