续集:$like包含多个模型



我们在 API 中有一个过滤器和返回事件的端点。

event有以下关系:

  • 事件模型
    • 音频模型
      • 设备型号
    • 用户型号

这意味着event与一个audio和一个user相关联,audio与一个device相关联。


这是来自过滤器事件的终结点的缩短代码片段:

eventModel.findAndCountAll({
where: { /*...*/ },
include: [{
model: audioModel,
attributes: ['guid', 'measured_at'],
where: { /*...*/ },
include: [{
model: deviceModel,
attributes: ['guid', 'shortname'],
where: { /*...*/ },
}]
},
{
model: userModel,
attributes: ['guid', 'firstname', 'lastname', 'email'],
where: { /*...*/ },
},
]
});

每个模型都有where属性,该属性按日期,Null和其他一些内容过滤掉项目。我已经剪了,因为这对我而言并不重要。

我需要的是添加一个search属性,该属性将按多个嵌套属性过滤事件:

eventModel.guid$likesearchdeviceModel.guid$likesearchdeviceModel.shortname$likesearchuserModel.firstname$likesearchuserModel.lastname$likesearch

我的问题是我无法将$like放入包含模型中的每个where属性中,因为在这种情况下,条件将作为AND而不是OR工作。

有没有办法在不迁移到原始查询的情况下实现这些东西?

有一种方法可以在不使用RAW查询的情况下做到这一点。在下面找到 :

var condition = " eventModel.guid like '%"+searck_keyword+"%' ";
condition += " OR deviceModel.guid like '%"+searck_keyword+"%' "
condition += " OR deviceModel.shortname like '%"+searck_keyword+"%' "
condition += " OR userModel.firstname like '%"+searck_keyword+"%' "
condition += " OR userModel.lastname like '%"+searck_keyword+"%' "

并在where(如where: [condition](中传递条件,并删除所有包含模型的where

最新更新