节点Js序列化原始查询以序列化ORM

时间:2019-04-16 12:46:49

标签: mysql node.js select orm sequelize.js

我需要将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});
});

1 个答案:

答案 0 :(得分:1)

尝试这种情况:

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