Edit Page

Populate (blueprint)

Populate and return foreign record(s) for the given association of this record.

GET /:model/:id/:association

If the specified association is plural ("collection"), this action returns the list of associated records as a JSON-encoded array of dictionaries (plain JavaScript objects). If the specified association is singular ("model"), this action returns the associated record as a JSON-encoded dictionary.

Parameter Type Details
model String The identity of the containing model.

e.g. 'purchase' (in GET /purchase/47/cashier)
id String The primary key of the parent record.

e.g. '47' (in GET /purchase/47/cashier)
association String The name of the association.

e.g. 'cashier' (in GET /purchase/47/cashier) or 'products' (in GET /purchase/47/products)
where String? Instead of filtering based on a specific attribute, you may instead choose to provide a where parameter with the WHERE piece of a Waterline criteria, encoded as a JSON string. This allows you to take advantage of contains, startsWith, and other sub-attribute criteria modifiers for more powerful find() queries.

e.g. ?where={"name":{"contains":"theodore"}}
limit Number? The maximum number of records to send back (useful for pagination). Defaults to 30.

e.g. ?limit=100
skip Number? The number of records to skip (useful for pagination).

e.g. ?skip=30
sort String? The sort order. By default, returned records are sorted by primary key value in ascending order.

e.g. ?sort=lastName%20ASC
select String? The attributes to include in each record in the result, specified as a comma-delimited list. By default, all attributes are selected. Not valid for plural (“collection”) association attributes.

e.g. ?select=name,age.
omit String? The attributes to exclude from each record in the result, specified as a comma-delimited list. Cannot be used in conjuction with select. Not valid for plural (“collection”) association attributes.

e.g. ?omit=favoriteColor,address.

Example

#

Populate the cashier who conducted purchase #47:

`GET /purchase/47/cashier`

Run in Postman

Expected response
#
{
  "name": "Dolly",
  "id": 7,
  "createdAt": 1485462079725,
  "updatedAt": 1485476060873,
}

Using jQuery:

$.get('/purchase/47/cashier', function (cashier) {
  console.log(cashier);
});

Using Angular:

$http.get('/purchase/47/cashier')
.then(function (cashier) {
  console.log(cashier);
});

Using sails.io.js:

io.socket.get('/purchase/47/cashier', function (cashier) {
  console.log(cashier);
});

Using cURL:

curl http://localhost:1337/purchase/47/cashier

Populating a collection

#

You can also populate a collection. For example, to populate the involvedInPurchases of employee #7:

GET /employee/7/involvedInPurchases

Expected response
#
[
  {
    "amount": 10000,
    "createdAt": 1485476060873,
    "updatedAt": 1485476060873,
    "id": 47,
    "cashier": 7
  },
  {
    "amount": 50,
    "createdAt": 1487015460792,
    "updatedAt": 1487015476357,
    "id": 52,
    "cashier": 7
  }
]

Notes

#
  • In the first example above, if purchase #47 did not have a cashier (i.e. null), then this action would respond with a 404 status code.
  • The examples above assume "rest" blueprint routing is enabled (or that you've bound this blueprint action as a comparable custom route), and that your project contains at least an empty Employee model as well as a Purchase model, and that Employee has the association attribute: involvedInPurchases: {model: 'Purchase'} and that Purchase has cashier: {model: 'Employee'}. You can quickly achieve this by running:

    $ sails new foo
    $ cd foo
    $ sails generate model purchase
    $ sails generate model employee
    

    ...then editing api/models/Employee.js and api/models/Purchase.js.

Is something missing?

If you notice something we've missed or could be improved on, please follow this link and submit a pull request to the sails repo. Once we merge it, the changes will be reflected on the website the next time it is deployed.

Reference

Reference