在Sails JS中使用嵌套(关联)模型有一种更好的方法吗? [英] Is there a bettery way to work with nested(associated) models in Sails JS?
问题描述
我已将SailsJs应用程序连接到Mongodb数据库。我正在研究分析应用程序。这些是我的应用程序中的主要模型:
I've connected my SailsJs app to a Mongodb database. I'm working on an analytic application. These are the major models in my application:
用户
项目
报告
活动
用户可以有很多项目,项目可以有很多报告,报告可以有很多事件。我使用模型属性的集合
和模型
属性创建了这些关系。我的问题是为什么找到特定用户的事件是如此困难?我希望我能做到这一点:
A user can have many projects, a project can have many reports and a report can have many events. I have created these relations using collection
and model
properties of my models attributes. My problem is why is it so hard to find events of specific user? I wish I could do this:
User.
find({id: id}).
populate('projects').
populate('reports').
populate('events').
then(function (eventsOfMyUser) {
});
但由于只有项目
是属性我的用户
模型只有第一个填充作品。不应该是一种更容易找到深层模型的方法,而不是在我的控制器或模型代码中编写令人讨厌且令人困惑的异步循环吗?
but since only projects
is an attribute of my User
model only the first populate works. Shouldn't be an easier way to find a deep model rather than writing nasty and confusing async loops in my controller or model code?
推荐答案
<目前,帆没有任何嵌套人口。需要参考的问题: https://github.com/balderdashy/waterline/issues/308
值得一提的是添加嵌套人口的拉取请求: https://github.com/balderdashy/waterline/pull/1052
Worth saying there's a pull request to add nested population: https://github.com/balderdashy/waterline/pull/1052
拉出请求目前尚未合并,但您可以使用它直接安装一个
Pull request isn't merged at the moment but you can use it installing one directly with
npm i Atlantis-Software/waterline#deepPopulate
有了它,你可以做一些像 .populate('projects.reports ...)'
。
With it you can do something like .populate('projects.reports ...)'
.
这篇关于在Sails JS中使用嵌套(关联)模型有一种更好的方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!