Back to Sails

Populate (blueprint)

docs/reference/blueprint-api/Populate.md

12.12.20004.9 KB
Original Source

Populate (blueprint)

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

usage
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.

ParameterTypeDetails
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:

text
`GET /purchase/47/cashier`

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

Using jQuery:

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

Using Angular:

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

Using sails.io.js:

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

Using cURL:

bash
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
json
[
  {
    "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:

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

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

<docmeta name="displayName" value="populate where"> <docmeta name="pageType" value="endpoint">