我如何在sequelizejs中使用此完整查询

时间:2019-07-16 03:02:49

标签: node.js sequelize.js

我想将此代码转换为续集。

where ADDTIME(deleted_at, TIMEDIFF(deleted_at, end_at) * 2) > NOW() OR (end_at < NOW() AND deleted_at is null);

我在下面的sequelize文档中找到了。但这对我的查询还不够。

Post.findAll({
    where: sequelize.where(sequelize.fn('char_length', 
    sequelize.col('status')), 6)
});

我想将此代码转换为续集。

where ADDTIME(deleted_at, TIMEDIFF(deleted_at, end_at) * 2) > NOW() OR (end_at < NOW() AND deleted_at is null);

像这样。我不想使用RAW查询

1 个答案:

答案 0 :(得分:0)

此子句的大部分很容易用Sequelize表示,如下所示:

where : {
     [Op.or] : [
          // 1
          Sequelize.where(
                Sequelize.fn('ADDTIME', 
                    Sequelize.col('deleted_at'), 
                    Sequelize.literal('TIMEDIFF(deleted_at, end_at) * 2')),
                {[Op.gt] : Sequelize.fn('NOW')}
          ),
          // 2
          {
                 end_at      : { [Op.lt]: Sequelize.fn('NOW') },
                 deleted_at  : null
          }
     ]
}

唯一的问题是乘法-不知道如何/是否有没有文字的方法。...

HTH