[关闭]
@WrRan 2016-11-10T11:08:41.000000Z 字数 3644 阅读 1068

waterline

Waterline Query Interface

The Waterline Query Interface allows you to interact with your models the same way no matter which
adapter they are using. This means you can use the same query language whether your data lives in
MySQL, MongoDB, Twitter, etc.

The Query Interface exposes the following methods:

See Query Methods for more information on their use.

Query Language

Waterline exposes a normalized language for finding records no matter which datastore the records
live in. The following options are available on all find and findOne queries.

Each option will return an instance of the deferred object used to create the query, so options
can be chained together to create complex queries.

See Query Language for more information on the options available in the
query language.

  1. User.find()
  2. .where({ name: { contains: 'foo' }})
  3. .populate('animals', { type: 'dog', limit: 10 })
  4. .skip(20)
  5. .limit(10)
  6. .exec(function(err, users) {});

For convience, promises are supported if you choose to use them. Promises use the Bluebird library,
so anything you do after the first then call (or spread, or catch), will be a complete Bluebird promise
object. Remember, you must end the query somehow (by calling then or one of the other functions)
in order to complete the database request.

  1. User.findOne()
  2. .where({ id: 2 })
  3. .then(function(user){
  4. var comments = Comment.find({userId: user.id}).then(function(comments){
  5. return comments;
  6. });
  7. return [user.id, user.friendsList, comments];
  8. })
  9. .spread(function(userId, friendsList, comments){
  10. })
  11. .catch(function(err){
  12. // An error occured
  13. });

.where()

where is the primary criteria for your query. Here you specify what you would like to search for
using any of the supported Query Language.

Description Accepted Data Types Required ?
Criteria Object {} Yes
  1. User.find()
  2. .where({ name: { startsWith: 'w' }})
  3. .exec(function(err, results) {});

.populate()

populate is used with associations to include any related values specified in a model definition.
If a collection attribute is defined in a many-to-many, one-to-many or many-to-many-through
association the populate option also accepts a full criteria object. This allows you
to filter associations and run limit and skip on the results.

Description Accepted Data Types Required ?
Attribute Name string Yes
Criteria Object {} No
  1. // Simple Population
  2. User.find()
  3. .populate('foo')
  4. .exec(function(err, users) {});
  1. // Collection Filtering
  2. User.find()
  3. .populate('foo', { type: 'bar', limit: 20 })
  4. .exec(function(err, users) {});

.limit()

limit will restrict the number of records returned by the query.

Description Accepted Data Types Required ?
Number to Return int Yes
  1. User.find()
  2. .limit(10)
  3. .exec(function(err, users) {});

.skip()

skip will skip over n results when returning the results.

Description Accepted Data Types Required ?
Number to Skip int Yes
  1. User.find()
  2. .skip(10)
  3. .exec(function(err, users) {});

.paginate()

When skip and limit are put together, they create the ability to paginate through records as you
would for pages. For example, if I wanted 'page 2' of a given record set, and I only want to see 10
records at a time, I know that I need to skip(10) and limit(10) like so:

  1. User.find()
  2. .skip(10)
  3. .limit(10)
  4. .exec(function(err, users) {});

But, while we are thinking in terms of pagination, or pages, it might be easier to use the
paginate helper:

  1. User.find()
  2. .paginate({ page: 2, limit: 10 })
  3. .exec(function(err, users) {});

Paginate has several options:

.sort()

sort will return a sorted set of values. Simply specify an attribute name for natural (ascending)
sort, or specify an asc or desc flag for ascending or descending orders respectively.

  1. User.find()
  2. .sort('roleId asc')
  3. .sort({ createdAt: 'desc' })
  4. .exec(function(err, users) {});

.exec()

exec will run the query and return the results to the supplied callback. It should be the last
method in the chain.

Description Accepted Data Types Required ?
Callback function Yes
  1. User.find()
  2. .exec(function(err, users) {});
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注