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 ce9287fe
authored
Nov 17, 2018
by
Takashi Sasaki
Committed by
Sushant
Nov 17, 2018
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix(association): enable eager load with include all(#9928) (#10173)
1 parent
9ba5f3c2
Show whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
308 additions
and
8 deletions
lib/associations/belongs-to-many.js
lib/associations/belongs-to.js
lib/associations/has-many.js
lib/associations/has-one.js
lib/associations/mixin.js
test/integration/associations/belongs-to-many.test.js
test/integration/associations/belongs-to.test.js
test/integration/associations/has-many.test.js
test/integration/associations/has-one.test.js
lib/associations/belongs-to-many.js
View file @
ce9287f
...
...
@@ -755,6 +755,18 @@ class BelongsToMany extends Association {
sourceInstance
[
association
.
accessors
.
add
](
newAssociatedObject
,
_
.
omit
(
options
,
[
'fields'
])).
return
(
newAssociatedObject
)
);
}
verifyAssociationAlias
(
alias
)
{
if
(
typeof
alias
===
'string'
)
{
return
this
.
as
===
alias
;
}
if
(
alias
&&
alias
.
plural
)
{
return
this
.
as
===
alias
.
plural
;
}
return
!
this
.
isAliased
;
}
}
module
.
exports
=
BelongsToMany
;
...
...
lib/associations/belongs-to.js
View file @
ce9287f
...
...
@@ -231,6 +231,18 @@ class BelongsTo extends Association {
.
then
(()
=>
newAssociatedObject
)
);
}
verifyAssociationAlias
(
alias
)
{
if
(
typeof
alias
===
'string'
)
{
return
this
.
as
===
alias
;
}
if
(
alias
&&
alias
.
singular
)
{
return
this
.
as
===
alias
.
singular
;
}
return
!
this
.
isAliased
;
}
}
module
.
exports
=
BelongsTo
;
...
...
lib/associations/has-many.js
View file @
ce9287f
...
...
@@ -463,6 +463,18 @@ class HasMany extends Association {
if
(
options
.
fields
)
options
.
fields
.
push
(
this
.
foreignKey
);
return
this
.
target
.
create
(
values
,
options
);
}
verifyAssociationAlias
(
alias
)
{
if
(
typeof
alias
===
'string'
)
{
return
this
.
as
===
alias
;
}
if
(
alias
&&
alias
.
plural
)
{
return
this
.
as
===
alias
.
plural
;
}
return
!
this
.
isAliased
;
}
}
module
.
exports
=
HasMany
;
...
...
lib/associations/has-one.js
View file @
ce9287f
...
...
@@ -261,6 +261,18 @@ class HasOne extends Association {
return
this
.
target
.
create
(
values
,
options
);
}
verifyAssociationAlias
(
alias
)
{
if
(
typeof
alias
===
'string'
)
{
return
this
.
as
===
alias
;
}
if
(
alias
&&
alias
.
singular
)
{
return
this
.
as
===
alias
.
singular
;
}
return
!
this
.
isAliased
;
}
}
module
.
exports
=
HasOne
;
lib/associations/mixin.js
View file @
ce9287f
...
...
@@ -80,14 +80,7 @@ const Mixin = {
getAssociationForAlias
(
target
,
alias
)
{
// Two associations cannot have the same alias, so we can use find instead of filter
return
this
.
getAssociations
(
target
).
find
(
association
=>
this
.
verifyAssociationAlias
(
association
,
alias
))
||
null
;
},
verifyAssociationAlias
(
association
,
alias
)
{
if
(
alias
)
{
return
association
.
as
===
alias
;
}
return
!
association
.
isAliased
;
return
this
.
getAssociations
(
target
).
find
(
association
=>
association
.
verifyAssociationAlias
(
alias
))
||
null
;
}
};
...
...
test/integration/associations/belongs-to-many.test.js
View file @
ce9287f
...
...
@@ -2423,4 +2423,86 @@ describe(Support.getTestDialectTeaser('BelongsToMany'), () => {
expect
(
PersonChildren
.
rawAttributes
[
Children
.
otherKey
].
field
).
to
.
equal
(
'child_id'
);
});
});
describe
(
'Eager loading'
,
()
=>
{
beforeEach
(
function
()
{
this
.
Individual
=
this
.
sequelize
.
define
(
'individual'
,
{
name
:
Sequelize
.
STRING
});
this
.
Hat
=
this
.
sequelize
.
define
(
'hat'
,
{
name
:
Sequelize
.
STRING
});
this
.
Event
=
this
.
sequelize
.
define
(
'event'
,
{});
this
.
Individual
.
belongsToMany
(
this
.
Hat
,
{
through
:
this
.
Event
,
as
:
{
singular
:
'personwearinghat'
,
plural
:
'personwearinghats'
}
});
this
.
Hat
.
belongsToMany
(
this
.
Individual
,
{
through
:
this
.
Event
,
as
:
{
singular
:
'hatwornby'
,
plural
:
'hatwornbys'
}
});
});
it
(
'should load with an alias'
,
function
()
{
return
this
.
sequelize
.
sync
({
force
:
true
}).
then
(()
=>
{
return
Promise
.
join
(
this
.
Individual
.
create
({
name
:
'Foo Bar'
}),
this
.
Hat
.
create
({
name
:
'Baz'
}));
}).
then
(([
individual
,
hat
])
=>
{
return
individual
.
addPersonwearinghat
(
hat
);
}).
then
(()
=>
{
return
this
.
Individual
.
findOne
({
where
:
{
name
:
'Foo Bar'
},
include
:
[{
model
:
this
.
Hat
,
as
:
'personwearinghats'
}]
});
}).
then
(
individual
=>
{
expect
(
individual
.
name
).
to
.
equal
(
'Foo Bar'
);
expect
(
individual
.
personwearinghats
.
length
).
to
.
equal
(
1
);
expect
(
individual
.
personwearinghats
[
0
].
name
).
to
.
equal
(
'Baz'
);
}).
then
(()
=>
{
return
this
.
Hat
.
findOne
({
where
:
{
name
:
'Baz'
},
include
:
[{
model
:
this
.
Individual
,
as
:
'hatwornbys'
}]
});
}).
then
(
hat
=>
{
expect
(
hat
.
name
).
to
.
equal
(
'Baz'
);
expect
(
hat
.
hatwornbys
.
length
).
to
.
equal
(
1
);
expect
(
hat
.
hatwornbys
[
0
].
name
).
to
.
equal
(
'Foo Bar'
);
});
});
it
(
'should load all'
,
function
()
{
return
this
.
sequelize
.
sync
({
force
:
true
}).
then
(()
=>
{
return
Promise
.
join
(
this
.
Individual
.
create
({
name
:
'Foo Bar'
}),
this
.
Hat
.
create
({
name
:
'Baz'
}));
}).
then
(([
individual
,
hat
])
=>
{
return
individual
.
addPersonwearinghat
(
hat
);
}).
then
(()
=>
{
return
this
.
Individual
.
findOne
({
where
:
{
name
:
'Foo Bar'
},
include
:
[{
all
:
true
}]
});
}).
then
(
individual
=>
{
expect
(
individual
.
name
).
to
.
equal
(
'Foo Bar'
);
expect
(
individual
.
personwearinghats
.
length
).
to
.
equal
(
1
);
expect
(
individual
.
personwearinghats
[
0
].
name
).
to
.
equal
(
'Baz'
);
}).
then
(()
=>
{
return
this
.
Hat
.
findOne
({
where
:
{
name
:
'Baz'
},
include
:
[{
all
:
true
}]
});
}).
then
(
hat
=>
{
expect
(
hat
.
name
).
to
.
equal
(
'Baz'
);
expect
(
hat
.
hatwornbys
.
length
).
to
.
equal
(
1
);
expect
(
hat
.
hatwornbys
[
0
].
name
).
to
.
equal
(
'Foo Bar'
);
});
});
});
});
test/integration/associations/belongs-to.test.js
View file @
ce9287f
...
...
@@ -975,4 +975,65 @@ describe(Support.getTestDialectTeaser('BelongsTo'), () => {
.
throw
(
'Naming collision between attribute \'person\' and association \'person\' on model car. To remedy this, change either foreignKey or as in your association definition'
);
});
});
describe
(
'Eager loading'
,
()
=>
{
beforeEach
(
function
()
{
this
.
Individual
=
this
.
sequelize
.
define
(
'individual'
,
{
name
:
Sequelize
.
STRING
});
this
.
Hat
=
this
.
sequelize
.
define
(
'hat'
,
{
name
:
Sequelize
.
STRING
});
this
.
Individual
.
belongsTo
(
this
.
Hat
,
{
as
:
'personwearinghat'
});
});
it
(
'should load with an alias'
,
function
()
{
return
this
.
sequelize
.
sync
({
force
:
true
}).
then
(()
=>
{
return
Promise
.
join
(
this
.
Individual
.
create
({
name
:
'Foo Bar'
}),
this
.
Hat
.
create
({
name
:
'Baz'
}));
}).
then
(([
individual
,
hat
])
=>
{
return
individual
.
setPersonwearinghat
(
hat
);
}).
then
(()
=>
{
return
this
.
Individual
.
findOne
({
where
:
{
name
:
'Foo Bar'
},
include
:
[{
model
:
this
.
Hat
,
as
:
'personwearinghat'
}]
});
}).
then
(
individual
=>
{
expect
(
individual
.
name
).
to
.
equal
(
'Foo Bar'
);
expect
(
individual
.
personwearinghat
.
name
).
to
.
equal
(
'Baz'
);
}).
then
(()
=>
{
return
this
.
Individual
.
findOne
({
where
:
{
name
:
'Foo Bar'
},
include
:
[{
model
:
this
.
Hat
,
as
:
{
singular
:
'personwearinghat'
}
}]
});
}).
then
(
individual
=>
{
expect
(
individual
.
name
).
to
.
equal
(
'Foo Bar'
);
expect
(
individual
.
personwearinghat
.
name
).
to
.
equal
(
'Baz'
);
});
});
it
(
'should load all'
,
function
()
{
return
this
.
sequelize
.
sync
({
force
:
true
}).
then
(()
=>
{
return
Promise
.
join
(
this
.
Individual
.
create
({
name
:
'Foo Bar'
}),
this
.
Hat
.
create
({
name
:
'Baz'
}));
}).
then
(([
individual
,
hat
])
=>
{
return
individual
.
setPersonwearinghat
(
hat
);
}).
then
(()
=>
{
return
this
.
Individual
.
findOne
({
where
:
{
name
:
'Foo Bar'
},
include
:
[{
all
:
true
}]
});
}).
then
(
individual
=>
{
expect
(
individual
.
name
).
to
.
equal
(
'Foo Bar'
);
expect
(
individual
.
personwearinghat
.
name
).
to
.
equal
(
'Baz'
);
});
});
});
});
test/integration/associations/has-many.test.js
View file @
ce9287f
...
...
@@ -1601,4 +1601,59 @@ describe(Support.getTestDialectTeaser('HasMany'), () => {
});
});
});
describe
(
'Eager loading'
,
()
=>
{
beforeEach
(
function
()
{
this
.
Individual
=
this
.
sequelize
.
define
(
'individual'
,
{
name
:
Sequelize
.
STRING
});
this
.
Hat
=
this
.
sequelize
.
define
(
'hat'
,
{
name
:
Sequelize
.
STRING
});
this
.
Individual
.
hasMany
(
this
.
Hat
,
{
as
:
{
singular
:
'personwearinghat'
,
plural
:
'personwearinghats'
}
});
});
it
(
'should load with an alias'
,
function
()
{
return
this
.
sequelize
.
sync
({
force
:
true
}).
then
(()
=>
{
return
Promise
.
join
(
this
.
Individual
.
create
({
name
:
'Foo Bar'
}),
this
.
Hat
.
create
({
name
:
'Baz'
}));
}).
then
(([
individual
,
hat
])
=>
{
return
individual
.
addPersonwearinghat
(
hat
);
}).
then
(()
=>
{
return
this
.
Individual
.
findOne
({
where
:
{
name
:
'Foo Bar'
},
include
:
[{
model
:
this
.
Hat
,
as
:
'personwearinghats'
}]
});
}).
then
(
individual
=>
{
expect
(
individual
.
name
).
to
.
equal
(
'Foo Bar'
);
expect
(
individual
.
personwearinghats
.
length
).
to
.
equal
(
1
);
expect
(
individual
.
personwearinghats
[
0
].
name
).
to
.
equal
(
'Baz'
);
});
});
it
(
'should load all'
,
function
()
{
return
this
.
sequelize
.
sync
({
force
:
true
}).
then
(()
=>
{
return
Promise
.
join
(
this
.
Individual
.
create
({
name
:
'Foo Bar'
}),
this
.
Hat
.
create
({
name
:
'Baz'
}));
}).
then
(([
individual
,
hat
])
=>
{
return
individual
.
addPersonwearinghat
(
hat
);
}).
then
(()
=>
{
return
this
.
Individual
.
findOne
({
where
:
{
name
:
'Foo Bar'
},
include
:
[{
all
:
true
}]
});
}).
then
(
individual
=>
{
expect
(
individual
.
name
).
to
.
equal
(
'Foo Bar'
);
expect
(
individual
.
personwearinghats
.
length
).
to
.
equal
(
1
);
expect
(
individual
.
personwearinghats
[
0
].
name
).
to
.
equal
(
'Baz'
);
});
});
});
});
test/integration/associations/has-one.test.js
View file @
ce9287f
...
...
@@ -884,4 +884,65 @@ describe(Support.getTestDialectTeaser('HasOne'), () => {
});
});
});
describe
(
'Eager loading'
,
()
=>
{
beforeEach
(
function
()
{
this
.
Individual
=
this
.
sequelize
.
define
(
'individual'
,
{
name
:
Sequelize
.
STRING
});
this
.
Hat
=
this
.
sequelize
.
define
(
'hat'
,
{
name
:
Sequelize
.
STRING
});
this
.
Individual
.
hasOne
(
this
.
Hat
,
{
as
:
'personwearinghat'
});
});
it
(
'should load with an alias'
,
function
()
{
return
this
.
sequelize
.
sync
({
force
:
true
}).
then
(()
=>
{
return
Promise
.
join
(
this
.
Individual
.
create
({
name
:
'Foo Bar'
}),
this
.
Hat
.
create
({
name
:
'Baz'
}));
}).
then
(([
individual
,
hat
])
=>
{
return
individual
.
setPersonwearinghat
(
hat
);
}).
then
(()
=>
{
return
this
.
Individual
.
findOne
({
where
:
{
name
:
'Foo Bar'
},
include
:
[{
model
:
this
.
Hat
,
as
:
'personwearinghat'
}]
});
}).
then
(
individual
=>
{
expect
(
individual
.
name
).
to
.
equal
(
'Foo Bar'
);
expect
(
individual
.
personwearinghat
.
name
).
to
.
equal
(
'Baz'
);
}).
then
(()
=>
{
return
this
.
Individual
.
findOne
({
where
:
{
name
:
'Foo Bar'
},
include
:
[{
model
:
this
.
Hat
,
as
:
{
singular
:
'personwearinghat'
}
}]
});
}).
then
(
individual
=>
{
expect
(
individual
.
name
).
to
.
equal
(
'Foo Bar'
);
expect
(
individual
.
personwearinghat
.
name
).
to
.
equal
(
'Baz'
);
});
});
it
(
'should load all'
,
function
()
{
return
this
.
sequelize
.
sync
({
force
:
true
}).
then
(()
=>
{
return
Promise
.
join
(
this
.
Individual
.
create
({
name
:
'Foo Bar'
}),
this
.
Hat
.
create
({
name
:
'Baz'
}));
}).
then
(([
individual
,
hat
])
=>
{
return
individual
.
setPersonwearinghat
(
hat
);
}).
then
(()
=>
{
return
this
.
Individual
.
findOne
({
where
:
{
name
:
'Foo Bar'
},
include
:
[{
all
:
true
}]
});
}).
then
(
individual
=>
{
expect
(
individual
.
name
).
to
.
equal
(
'Foo Bar'
);
expect
(
individual
.
personwearinghat
.
name
).
to
.
equal
(
'Baz'
);
});
});
});
});
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