2

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?

Karma
  • 2,196
  • 1
  • 22
  • 30
  • I have a similar problem where I want to emit an event after the blueprint assembles its data (in my case for the purposes of activity logging). For now, I think I'm stuck with copying the Sails blueprint controllers verbatim. – Andrew Eddie Apr 22 '15 at 07:12
  • 1
    Just as a side note you don't have to **GET /products/:productid** n times. Do a **GET /products/** with a WHERE IN clause and an array of ids (and an appropriate limit). Then on the front end set the products based on the results. It'll save you a ton of network overhead. – Jeffrey Van Alstine Oct 18 '16 at 16:44

1 Answers1

0

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

Community
  • 1
  • 1
qualbeen
  • 1,534
  • 4
  • 16
  • 27