我们在 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
$like
search
或deviceModel.guid
$like
search
或deviceModel.shortname
$like
search
或userModel.firstname
$like
search
或userModel.lastname
$like
search
我的问题是我无法将$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