Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is populate in SailsJs?

I recently began to develop on sailsJs and not understanding the subtleties Please explain to me what is populate in SailsJs and who can please do simple example Thanks in advance ? whats is ?

User.find({ name: 'foo' })
.populate('pets', { name: 'fluffy' })
.exec(function(err, users) {
  if(err) return res.serverError(err);
  res.json(users);
});
like image 804
davron Avatar asked Dec 22 '25 18:12

davron


1 Answers

populate is used for associations. When your model is something like this:

// User.js
module.exports = {
  attributes: {

    name: {
      type: "string"
    },

    pet: {
      model: "pet"
    }

  }
}

Here pet attribute of user collection is a reference to pet table. In user table it will store only the id column of pet. However, when you do a populate while find, then it will fetch the entire record of the pet entry and display it here. This is just for one to one association. You can have many to one associations as well as many to many. See this documentation for more details

like image 72
Mandeep Singh Avatar answered Dec 24 '25 07:12

Mandeep Singh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!