绑定到 Sequelize 模型的自定义 .find()
Custom .find() tied to a Sequelize Model
在文档中找不到我想要的东西,所以我就把它留在这里,希望能得到一些帮助!
我有一个特定的查询,我需要 运行 对我的用户模型进行大量包含,这些包含都在其中包含 where 语句...
所以,我想整理我的控制器并把这个逻辑
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()
在我的控制器里面。
是否存在此功能,我只是想念它?
任何 help/advice 将不胜感激。
谢谢!
看看http://sequelize.readthedocs.org/en/latest/docs/models-definition/#expansion-of-models
这绝对让您有机会在模型上创建 findByIdWithIncludes
方法
这种select可以用"Scopes"来完成,
http://sequelize.readthedocs.org/en/latest/docs/scopes/
通过这样定义您的模型:
var Project = sequelize.define('Model1', {
// Attributes
}, {
defaultScope: {
where: {
aCondition: false
}
}
});
它将与每个 find() 一起使用。
您可以定义多个范围。
在文档中找不到我想要的东西,所以我就把它留在这里,希望能得到一些帮助!
我有一个特定的查询,我需要 运行 对我的用户模型进行大量包含,这些包含都在其中包含 where 语句... 所以,我想整理我的控制器并把这个逻辑
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()
在我的控制器里面。
是否存在此功能,我只是想念它?
任何 help/advice 将不胜感激。
谢谢!
看看http://sequelize.readthedocs.org/en/latest/docs/models-definition/#expansion-of-models
这绝对让您有机会在模型上创建 findByIdWithIncludes
方法
这种select可以用"Scopes"来完成, http://sequelize.readthedocs.org/en/latest/docs/scopes/
通过这样定义您的模型:
var Project = sequelize.define('Model1', {
// Attributes
}, {
defaultScope: {
where: {
aCondition: false
}
}
});
它将与每个 find() 一起使用。 您可以定义多个范围。