不要怂,就是干,撸起袖子干!

Commit 2eadf0c7 by Jan Aagaard Meier

[ci skip] Document include all. Closes #2759

1 parent 6bb91833
Showing with 15 additions and 1 deletions
...@@ -888,7 +888,7 @@ Project.sum('age', { where: { age: { $gt: 5 } } }).then(function(sum) { ...@@ -888,7 +888,7 @@ Project.sum('age', { where: { age: { $gt: 5 } } }).then(function(sum) {
## Eager loading ## Eager loading
When you are retrieving data from the database there is a fair chance that you also want to get associations with the same query - this is called eager loading. The basic idea behind that, is the use of the attribute `include` when you are calling `find` or `findAll`. Lets assume the following setup: When you are retrieving data from the database there is a fair chance that you also want to get associations with the same query - this is called eager loading. The basic idea behind that, is the use of the attribute `include` when you are calling `find` or `findAll`. Lets assume the following setup:
```js ```js
var User = sequelize.define('User', { name: Sequelize.STRING }) var User = sequelize.define('User', { name: Sequelize.STRING })
...@@ -980,6 +980,14 @@ User.findAll({ include: [{ model: Tool, as: 'Instruments' }] }).then(function(us ...@@ -980,6 +980,14 @@ User.findAll({ include: [{ model: Tool, as: 'Instruments' }] }).then(function(us
}) })
``` ```
### Including everything
To include all attributes, you can pass a single object with `all: true`:
```js
User.findAll({ include: [{ all: true }]});
```
### Ordering Eager Loaded Associations ### Ordering Eager Loaded Associations
In the case of a one-to-many relationship. In the case of a one-to-many relationship.
...@@ -1007,6 +1015,7 @@ Company.findAll({ ...@@ -1007,6 +1015,7 @@ Company.findAll({
``` ```
### Nested eager loading ### Nested eager loading
You can used nested eager loading to load all related models of a related model:
```js ```js
User.findAll({ User.findAll({
include: [ include: [
...@@ -1038,6 +1047,11 @@ User.findAll({ ...@@ -1038,6 +1047,11 @@ User.findAll({
}) })
``` ```
Include all also supports nested loading:
```js
User.findAll({ include: [{ all: true, nested: true }]});
```
[0]: #configuration [0]: #configuration
[3]: https://github.com/chriso/validator.js [3]: https://github.com/chriso/validator.js
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!