节点Js将原始查询续集为sequelize ORM

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了节点Js将原始查询续集为sequelize ORM相关的知识,希望对你有一定的参考价值。

我需要sequelize原始query改为sequelize ORM

这是我的查询

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});
});

换成这样的

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});
});
答案

试试这个条件:

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

以上是关于节点Js将原始查询续集为sequelize ORM的主要内容,如果未能解决你的问题,请参考以下文章