在 Sails.js 中通过关系填充多对多 [英] Populating a Many to Many Through Relation in Sails.js

查看:22
本文介绍了在 Sails.js 中通过关系填充多对多的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Sails.js 提供了一种通过其蓝图 API 以多对多关系填充对象的方法.因此,GET/categorymodel/:categoryid/productscollection 将返回属于指定类别的所有产品.链接.

Sails.js offers a way to populate objects in Many to Many relationship via its Blueprint API. Thus, GET /categorymodel/:categoryid/productscollection will return all products that belong to the specified category. Link.

对于多对多通过关系,文档建议通过中介模型使用多个一对多关系来实现所需的组织.这样做的缺点是无法通过内置的 Blueprint API 填充对象.链接.

For Many to Many Through relations, the docs advises to use multiple One to Many relations via an intermediary model to achieve the desired organization. The downside of this is there's no way to populate objects via the built-in Blueprint API. Link.

因此,在查询属于特定类别的产品时,我查询 GET/categorymodel/:categoryid/intermediarycollection 以获得类似

So, while querying for products belonging to a particular category, I query GET /categorymodel/:categoryid/intermediarycollection to get something like

[{categoryid: 1, productid: 1, id: 1}, {categoryid: 1, productid: 2, id: 2}, {categoryid: 1, productid: 3, id: 3}, ... n]

这意味着客户端必须GET/products/:productid n 次.

This means the client will have to GET /products/:productid n times.

在发送到客户端之前,如何处理 populate() 结果(而不是编辑/覆盖它)以添加产品对象?有没有更好的方法来做到这一点?

How do I process the populate() result (and not edit/override it) to add the product object before sending to the client? Is there a better way of doing this?

推荐答案

是的!这是可能的,但它是一种黑客..?

Yes! It is possible, but it is kind of an hack..?

蓝图的底层行为是对来自数据库的结果调用 res.ok.如果我们更改 res.ok 的行为,并添加过滤器或类似的自定义需求会怎样?

The underlaying behaviour of the blueprints, is to call res.ok on your result from database. What if we changed the behavior of res.ok, and added a filter or similar custom needs?

查看此答案以了解更多详细信息:https://stackoverflow.com/a/41932352/36975

Take a look on this answer for more details: https://stackoverflow.com/a/41932352/36975

这篇关于在 Sails.js 中通过关系填充多对多的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆