Populating a Many to Many Through Relation in Sails.js

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.

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.

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]

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

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?

Answers:

Answer

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

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?

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

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.