balderdashy / sails

Realtime MVC Framework for Node.js
https://sailsjs.com
MIT License
22.84k stars 1.95k forks source link

How to get Data from multiple relationship collection in one query #4750

Open vaghela943 opened 5 years ago

vaghela943 commented 5 years ago

Node version9: Sails version1.1 (sails): ORM hook version (sails-hook-orm): Sockets hook version (sails-hook-sockets): Organics hook version (sails-hook-organics): Grunt hook version (sails-hook-grunt): Uploads hook version (sails-hook-uploads): DB adapter & version (e.g. sails-mysql@5.55.5): Skipper adapter & version (e.g. skipper-s3@5.55.5):


sailsbot commented 5 years ago

@vaghela943 Thanks for posting! We'll take a look as soon as possible.

In the mean time, there are a few ways you can help speed things along:

Please remember: never post in a public forum if you believe you've found a genuine security vulnerability. Instead, disclose it responsibly.

For help with questions about Sails, click here.

vaghela943 commented 5 years ago

In Sails 1.1 deep populate not working. So How I can get data from multiple relationship collection.

Please help

raqem commented 5 years ago

Hi @vaghela943 that is the expected behavior. Sails does not support deep populate. If you would like to get the full explanation why see here.

In order to 'populate a populate' try this method:

var bones = await Bone.find();
var pets = await Pet.find({id: {in: _.pluck(bones, 'pet')}}).populate('owner');
bones = bones.map((bone) => Object.assign(bone, {pet: _.find(pets, {id: bone.pet})||null}));

So a "bone" has a pet and a pet has an "owner". The above method lets you populate the pet.owner.