如何在sequelize中使用非列变量

How to use non-column variable in sequelize

我正在开发试用 Node.JS 服务器,它使用 Sequelize ORM 与 postgres 数据库交互。

我有 table 个答案,其中一些已确认但有些未确认(“已确认”是“answers" table).

如果普通用户请求我,我只想发送确认的答案,否则发送所有答案;我识别使用查询参数的普通用户,我称之为 isAdmin(如果 isAdmin 为 0,则它是普通用户)。

那么如何在 Sequelize 的 where 子句中使用 isAdmin 参数?

我最好的尝试如下:

const isAdmin = req.params.isadmin
try{
    const result = await AnswersModel.Answer.findAll({
         where:
                sequelize.or(
                sequelize.and(
                    {confirmed: true},
                    isAdmin == false
                ),
                sequelize.and(
                    1==1,
                    isAdmin == true
                )
            )
}catch(error){
...
}

我没有收到任何错误,但 isAdmin 条件无效:(!

注意:我知道我可以使用“行查询”或简单的 if(){}else{} 样式来做到这一点,但我只是想避免它

不要尝试将所有条件过滤器构建逻辑包含到 where 选项中。它必须尽可能简单。

const where = {}
if (!isAdmin) {
  where.confirmed = true
}
const result = await AnswersModel.Answer.findAll({
 where
})