Find Records

Returns a list of records from the model as a JSON array of objects.

  1. GET /:model

Results may be filtered, paginated, and sorted based on the blueprint configuration and/or parameters sent in the request.

If the action was triggered via a socket request, the requesting socket will be “subscribed” to all records returned. If any of the returned records are subsequently updated or deleted, a message will be sent to that socket’s client informing them of the change. See the docs for Model.subscribe() for details.

Parameters

All parameters are optional.

Parameter Type Details
* ((string)) To filter results based on a particular attribute, specify a query parameter with the same name as the attribute defined on your model.

For instance, if our Purchase model has an amount attribute, we could send GET /purchase?amount=99.99 to return a list of $99.99 purchases.
where ((string)) Instead of filtering based on a specific attribute, you may instead choose to provide a where parameter with a Waterline WHERE criteria object, 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
callback ((number)) If specified, a JSONP response will be sent (instead of JSON). This is the name of a client-side javascript function to call, to which results will be passed as the first (and only) argument

e.g. ?callback=my_JSONP_data_receiver_fn

find Example

Find the 30 newest purchases in our database.

  1. [
  2. {
  3. "amount": 49.99,
  4. "id": 1,
  5. "createdAt": "2013-10-18T01:22:56.000Z",
  6. "updatedAt": "2013-10-18T01:22:56.000Z"
  7. },
  8. {
  9. "amount": 99.99,
  10. "id": 47,
  11. "createdAt": "2013-10-14T01:22:00.000Z",
  12. "updatedAt": "2013-10-15T01:20:54.000Z"
  13. }
  14. ]

Using jQuery:

  1. $.get('/purchase?sort=createdAt DESC', function (purchases) {
  2. console.log(purchases);
  3. });

Using Angular:

  1. $http.get('/purchase?sort=createdAt DESC')
  2. .then(function (res) {
  3. var purchases = res.data;
  4. console.log(purchases);
  5. });

Using sails.io.js:

  1. io.socket.get('/purchase?sort=createdAt DESC', function (purchases) {
  2. console.log(purchases);
  3. });

Using cURL:

  1. curl http://localhost:1337/purchase?sort=createdAt%20DESC

Notes

  • The example above assumes “rest” blueprints are enabled, and that your project contains a Purchase model and an empty PurchaseController. You can quickly achieve this by running:

    1. $ sails new foo
    2. $ cd foo
    3. $ sails generate api purchase