Chinese Documentation : Querying related models

Overview

REVIEW COMMENT from Rand

Add usage of all the filter params (where, order, fields, include, ...) on included relations per https://github.com/strongloop/loopback-datasource-juggler/pull/324.

Examples:

Find ALL Student and also return ALL their Classes with the Teacher who teaches those Classes and also ALL of the Students enrolled in those Classes...

      Student.find({"filter":
                {
                  "include": {"relation": "classes", "scope": {"include": ["teachers","students"]}}
                }})
_____
Find a specific Teacher and  also return ALL their Classes and also ALL of the Students enrolled in those Classes...
Teacher.find({"filter":
                  {
                    "where": {"id": $state.params.id},
                    "include": {"relation": "classes", "scope": {"include": ["students"]}}
                  }})

A relation defines the connection between two models by connecting a foreign key property to a primary key property.  For each relation type, LoopBack automatically mixes in helper methods to the model class to help navigate and associate the model instances to load or build a data graph.

Often, client applications want to select relevant data from the graph, for example to get user information and recently-placed orders. LoopBack provides a few ways to express such requirements in queries.

The LoopBack Relations example application provides some examples.  For general information on queries, see Querying data.

Inclusion

To include related models in the response for a query, use the ‘include’ property of the query object or use the include() method on the model class. The ‘include’ can be a string, an array, or an object.  For more information, see Include(加载导航属性)过滤器.

The following examples illustrate valid formats.

Load all user posts with only one additional request:

Load all post owners (users), and all orders of each owner:

Load all post owners (users), and all friends and orders of each owner:

Load all post owners (users), all posts (including images), and orders of each owner:

The model class also has an include() method.  For example, the code snippet below will populate the list of user instances with posts:

/server/script.js
User.include(users, 'posts', function() {
  ...
});

Scope

Scoping enables you to define a query as a method to the target model class or prototype. For example,

/server/boot/script.js
User.scope(‘top10Vips’, {where: {vip: true}, limit: 10});
 
User.top10Vips(function(err, vips) {
});

You can create the same function using a custom method too:

/server/boot/script.js
User.top10Vips = function(cb) {
  User.find({where: {vip: true}, limit: 10}, cb);
}