Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
public
/
sequelize
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
不要怂,就是干,撸起袖子干!
Commit 04583cf7
authored
Aug 29, 2016
by
Alexander Mochalin
Committed by
Mick Hansen
Aug 29, 2016
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
support string in include (in association field too) (#6512)
1 parent
e0e21726
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
51 additions
and
6 deletions
docs/docs/models-usage.md
lib/model.js
test/unit/model/include.test.js
docs/docs/models-usage.md
View file @
04583cf
...
...
@@ -523,6 +523,26 @@ User.findAll({ include: ['Instruments'] }).then(function(users) {
}]
*/
})
User
.
findAll
({
include
:
[{
association
:
'Instruments'
}]
}).
then
(
function
(
users
)
{
console
.
log
(
JSON
.
stringify
(
users
))
/*
[{
"name": "John Doe",
"id": 1,
"createdAt": "2013-03-20T20:31:45.000Z",
"updatedAt": "2013-03-20T20:31:45.000Z",
"Instruments": [{
"name": "Toothpick",
"id": 1,
"createdAt": null,
"updatedAt": null,
"userId": 1
}]
}]
*/
})
```
When eager loading we can also filter the associated model using
`where`
. This will return all
`User`
s in which the
`where`
clause of
`Tool`
model matches rows.
...
...
lib/model.js
View file @
04583cf
...
...
@@ -182,18 +182,23 @@ class Model {
options
.
include
=
options
.
include
.
map
(
include
=>
this
.
_conformInclude
(
include
,
self
));
}
static
_conformInclude
(
include
,
self
)
{
let
model
;
if
(
include
.
_pseudo
)
return
include
;
static
_transformStringAssociation
(
include
,
self
)
{
if
(
self
&&
typeof
include
===
'string'
)
{
if
(
!
self
.
associations
.
hasOwnProperty
(
include
))
{
throw
new
Error
(
'Association with alias "'
+
include
+
'" does not exists'
);
}
include
=
self
.
associations
[
include
];
return
self
.
associations
[
include
];
}
return
include
;
}
static
_conformInclude
(
include
,
self
)
{
let
model
;
if
(
include
.
_pseudo
)
return
include
;
include
=
this
.
_transformStringAssociation
(
include
,
self
);
if
(
include
instanceof
Association
)
{
if
(
self
&&
include
.
target
.
name
===
self
.
name
)
{
model
=
include
.
source
;
...
...
@@ -206,6 +211,9 @@ class Model {
include
=
{
model
:
include
};
}
else
if
(
_
.
isPlainObject
(
include
))
{
if
(
include
.
association
)
{
include
.
association
=
this
.
_transformStringAssociation
(
include
.
association
,
self
);
if
(
self
&&
include
.
association
.
target
.
name
===
self
.
name
)
{
model
=
include
.
association
.
source
;
}
else
{
...
...
test/unit/model/include.test.js
View file @
04583cf
...
...
@@ -266,6 +266,23 @@ describe(Support.getTestDialectTeaser('Model'), function() {
as
:
'Owner'
});
});
it
(
'should expand string association'
,
function
()
{
const
options
=
{
include
:
[{
association
:
'Owner'
,
attributes
:
[
'id'
]
}]
};
Sequelize
.
Model
.
_conformOptions
(
options
,
this
.
Company
);
expect
(
options
.
include
[
0
]).
to
.
deep
.
equal
({
model
:
this
.
User
,
association
:
this
.
Company
.
Owner
,
attributes
:
[
'id'
],
as
:
'Owner'
});
});
});
describe
(
'subQuery'
,
function
()
{
...
...
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment