I need to sequelize raw query change to sequelize ORM

This is my query

db.sequelize.query("SELECT count(*) as count FROM cubbersclosure WHERE CAST('"+ fromDate +"' as date) <= toDate AND CAST('"+ toDate +"' as date) >= fromDate", { type: sequelize.QueryTypes.SELECT}).then(closureData=>{
    res.send(closureData);

}).catch(error=>{
    res.status(403).send({status: 'error', resCode:200, msg:'Internal Server Error...!', data:error});
});

Change to like this

CubbersClosure.findAndCountAll({
    where:{
        // condtion here         
    }
}).then(closureData=>{        
    res.send(closureData);
}).catch(error=>{
    res.status(403).send({status: 'error', resCode:200, msg:'Internal Server Error...!', data:error});
});

1 Answers

1
Dmytro Mysak On

try this condition:

where: {
  toDate: { $gte: sequelize.cast(req.body.toDate, 'date') },
  fromDate: { $gte: sequelize.cast(req.body.fromDate, 'date') },
},