Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to selectively populate waterline associations via query param in sails.js

By default, sails will populate all relationships within a model when it's corresponding API route is hit. Does anyone know if it's possible to toggle this functionality? If I'm working with a one-to-many association, I may not want to populate the association when doing a listing of all items for performance reasons. But when viewing a single item, it would be nice to complete the population.

For example, say one ticket can have many comments. I don't care about the comments when fetching a case listing but would be important when viewing a specific case. I took a guess at how it could function but it fails:

localhost:1337/tickets?populate=false

Update

I implemented the above functionality within balderdashy/sails#1695. The only change is that you selectively choose which associations to populate using:

localhost:1337/tickets?populate=[]          // Don't populate anything
localhost:1337/tickets?populate=[comments]  // Only populate comments

This would override whatever is defined for populate within your blueprint config.

like image 951
Jason Sims Avatar asked May 14 '14 00:05

Jason Sims


1 Answers

You just need to separate your assosiactions via comma, just like this:

localhost:1337/tickets?populate=comments,owner&SOME_OTHER_PARAMS
like image 123
Moe Far Avatar answered Oct 28 '22 03:10

Moe Far