Populated Values

In addition to basic attribute data like email addresses, phone numbers, and birthdates, Waterline can dynamically store and retrieve linked sets of records using associations. When .populate() is called on a query, each of the resulting records will contain one or more populated values. Each one of those populated values is a snapshot of the record(s) linked to that particular association at the time of the query.

The type of a populated value is either:

  • null, or a plain old JavaScript object (POJO), or (if it corresponds to a “model” association)
  • an empty array, or an array of plain old JavaScript objects (if it corresonds to a “collection” association)

For example, assuming we’re dealing with orders of adorable wolf puppies:

  1. Order.find()
  2. .populate('buyers') // a "collection" association
  3. .populate('seller') // a "model" association
  4. .exec(function (err, orders){
  5. // this array is a snapshot of the Customers who are associated with the first Order as "buyers"
  6. orders[0].buyers;
  7. // => [ {id: 1, name: 'Rob Stark'}, {id: 6, name: 'Arya Stark'} ]
  8. // this object is a snapshot of the Company that is associated with the first Order as the "seller"
  9. orders[0].seller;
  10. // => { id: 42941, corporateName: 'WolvesRUs Inc.' }
  11. // this array is empty because the second Order doesn't have any "buyers"
  12. orders[1].buyers;
  13. // => []
  14. // this is `null` because there is no "seller" associated with the second Order
  15. orders[1].seller;
  16. // => null
  17. });

Modifying populated values

Changes to populated values are persisted (i.e. saved to the database) by calling .save() on the record they are attached to. You cannot call .save() directly on a populated value.

Changing or remove the linked record of a “model” association can be accomplished by simply setting the property directly on the original record:

  1. orders[1].seller = { corporateName: 'Wolf Orphanage' };

“collection” associations, on the other hand, do have a couple of special (non-enumerable) methods for associating and disassociating linked records. However, .save() must still be called on the original record in order for changes to be persisted to the database.

  1. orders[1].buyers.add({ name: 'Jon Snow' });
  2. orders[1].save(function (err) { ... });

Example

Finally, to put it all together:

  1. Order.find()
  2. .populate('buyers')
  3. .exec(function (err, orders){
  4. orders[1].buyers.add({ name: 'Jon Snow' });
  5. orders[1].seller = { corporateName: 'Wolf Orphanage' };
  6. orders[1].save(function (err) {
  7. // We successfully created a new Customer named Jon and added
  8. // him to `order[1]` as one of its "buyers".
  9. // We also created a new company and set it as `order[1]`'s "seller".
  10. //
  11. // If we had provided only a primary key value instead of an object,
  12. // in both cases Waterline would have tried to associate existing
  13. // Customer and Company records rather than creating new ones.
  14. });
  15. });