在文档中找不到我想要的东西,所以我将把它留在这里,希望能得到一些帮助!
我有一个特定的查询,我需要在我的用户模型上运行很多包含,并且这些包含都包含其中的 where 语句......
所以,我想整理我的 Controller 并把这个逻辑User.findById(userId, {include: [{
model: Model1,
where: { aCondition: false}
},{
model: Model2,
where: { aCondition: false}
},{
model: Model3,
where: { aCondition: false}
},{
model: Model4,
where: { aCondition: false}
},{
model: Model5,
where: { aCondition: false}
}]}).then()
进入模型本身,这样我就可以打电话User.findByIdWithIncludes(userId).then()
在我的 Controller 内部。
这个功能是否存在而我只是想念它?
任何帮助/建议将不胜感激。
谢谢!
最佳答案
这种选择可以使用“范围”来完成,
http://sequelize.readthedocs.org/en/latest/docs/scopes/
通过这样定义你的模型:
var Project = sequelize.define('Model1', {
// Attributes
}, {
defaultScope: {
where: {
aCondition: false
}
}
});
它将与每个 find() 一起使用。
您可以定义多个范围。
关于model - 自定义 .find() 绑定(bind)到 Sequelize 模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30956539/