航行解决方法以解决人口稠密的问题 [英] Sails workaround for deep populate
问题描述
我正在尝试填充一个集合.
I'm trying to deep populate a collection.
例如
// UnitType.js
name: { type: 'string' }
// Unit.js
unitType: {
model: 'unitType',
via: '_id',
required: true,
index: true
}
// Product.js
unit: {
model: 'unit',
via: '_id',
required: true,
index: true
},
问题是,据我从互联网研究中了解到的那样,
The problem is, that - as far I know from internet research deep populate like
Product.find().populate('unit.unitType');
当前不支持
.要达到我目前想要的结果
is currently not supported in sails. To achieve the result I want I currently
- 查询具有填充
unit
的产品
- 使用`product.unit.unitType`` 中的ID查询UnitTypes
-
.map()
product.unit.unitType
及其响应
- query Products with populate
unit
- query UnitTypes with the id from `product.unit.unitType``
.map()
product.unit.unitType
with the response
这当然远非理想.我还尝试在模型中使用toJSON
来预填充" unitType
->不起作用,因为这不支持Promises.
This is of course far from ideal. I also tried using toJSON
in the model to "pre-populate" the unitType
-> doesn't work since this doesn't support Promises.
在这个问题上有很多线程,在github上有PR的线程,但是到目前为止我还没有找到解决这个问题的方法.有什么办法可以使它变得更好吗?
There are quite a few threads on so and PR's on github on this issue, but so far I haven't found a solution to this problem. Is there any way to make this better?
推荐答案
您可以尝试替换 Waterline ORM 离岸ORM .帆帆可以做到这一点- sails-hook-orm-offshore .
You could try to replace Waterline ORM with Offshore ORM. There is a hook for sails to do that - sails-hook-orm-offshore.
这很容易实现到您现有的项目中,因为它的Waterline分支具有更多功能.我发现的唯一缺点是 sails-hook-validation 停止工作.
It's quite easy to implement to your existing project, because its fork of Waterline wits some more features. Only cons i found is that sails-hook-validation stopped working.
如何安装
npm install sails-hook-orm-offshore --save
配置
.sailsrc
{
"hooks": {
"orm": false,
"pubsub": false
}
}
默认
config/globals.js
adapters: true,
models: true
用法
现在,您将可以深度填充在您的查询中.例如(来自文档):
Now you will be allowed to deep populate in your queries. For example (from documentation):
User.find()
.populate('foo.bar', { name: 'foo' }) //populate foo into user and bar into foo
.exec(function(err, users) {
console.log(users[0].foo.bar.name) // 'foo'
});
第二个选项
用注水线合并人口稠密
npm i Atlantis-Software/waterline#deepPopulate
这篇关于航行解决方法以解决人口稠密的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!