SailsJS manually populate posts using Model.query
says I have this example as my application http://sailsjs.org/#!/documentation/concepts/ORM/Associations/OnetoMany.html
For some big (tricky) reasons I can't use Model.populate () and I'm stuck with using Model.query ()
Does anyone know how to get the result as User.find().populate('pets')
Using Model.query()
Please
thank
+3
source to share
1 answer
You can do it like waterline adapters to fill OneToMany:
- Get parents:
select * from user ...
- Get the children of each parent in only one request, to avoid overloading the database:
select * from pet where user = user1.id union select * from pet where user = user2.id union ... union select * from pet where user = userN.id
. -
Rearrange the children with parentPk (you can use lodash or underscore.js functions for this) Example:
users.forEach(function(user){ user.pets = _.filter(pets,function(pet){ return pet.user === user.id; }); });
+1
source to share