對於我認爲你想要做的事有不同的方式。我也建議不要直接使用這些模型,而是使用你想要的服務(通過調用他們的service methods)。你也想熟悉API for hooks。然後,你有一些選擇:
1)修改現有的成果
要修改現有服務調用的結果,修改hook.result
在after
鉤:
module.exports = function() {
return function (hook) {
let productService = hook.app.service('product');
return productService.find({
paginate: false // Turn off pagination
}).then(products => {
hook.result.products = products;
return hook;
});
};
};
然後
const myHook = require('../../hooks/myhook');
app.service('products-master').hooks({
after: {
get: [ myHook() ]
}
});
這將增加一個products
財產時得到一個唱歌從products-master
服務的項目。
2)設置hook.result
要跳過數據庫調用,您可以設置hook.result
到要在before
鉤回什麼。請記住,所有after
掛鉤仍將運行。
3)使用自定義服務
在custom service你可以做任何你喜歡的,例如得到所有你需要的東西,然後手動將它們放在一起:
app.use('/all-products', {
find() {
return this.app.service('products-master').find(params).then(page => {
const promises = page.data.map(masterProduct => {
return this.app.service('products').find({
paginate: false,
query: {
masterProductId: masterProduct._id
}
}).then(products => {
// Add to masterProduct
masterProduct.products = products;
});
});
return Promise.all(promises).then(() => {
// Return `page` which has the modified data
return page;
});
});
},
setup(app) {
this.app = app;
}
});
發電機可以通過選擇custom service
並修改servicename.class.js
來創建自定義服務。
4)填入鉤
因爲我覺得你只是想填充相關的實體,您也可以看看populate common hook。它可以自動完成這些事情。
5)用貓鼬$populate
的Mongoose Feathers adapter還支持$populate query parameter它允許你在你在你的貓鼬模型中定義的關係拉。
欲瞭解羽毛協會的更多信息,請參閱this FAQ。
謝謝@Daff! – Rahul