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 0691ced4
authored
Dec 27, 2013
by
Mick Hansen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Make sure eager loading validater can correctly identify associations after setting default alias
1 parent
5816f037
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
55 additions
and
3 deletions
lib/associations/belongs-to.js
lib/associations/has-many.js
lib/associations/has-one.js
lib/dao-factory.js
lib/dao.js
test/dao-factory.test.js
lib/associations/belongs-to.js
View file @
0691ced
...
...
@@ -8,6 +8,7 @@ module.exports = (function() {
this
.
source
=
source
this
.
target
=
target
this
.
options
=
options
this
.
isSingleAssociation
=
true
this
.
isSelfAssociation
=
(
this
.
source
.
tableName
==
this
.
target
.
tableName
)
if
(
this
.
isSelfAssociation
&&
!
this
.
options
.
foreignKey
&&
!!
this
.
options
.
as
)
{
...
...
lib/associations/has-many.js
View file @
0691ced
...
...
@@ -17,6 +17,7 @@ module.exports = (function() {
this
.
options
=
options
this
.
sequelize
=
source
.
daoFactoryManager
.
sequelize
this
.
through
=
options
.
through
this
.
isMultiAssociation
=
true
this
.
isSelfAssociation
=
(
this
.
source
.
tableName
===
this
.
target
.
tableName
)
this
.
doubleLinked
=
false
this
.
combinedTableName
=
Utils
.
combineTableNames
(
...
...
lib/associations/has-one.js
View file @
0691ced
...
...
@@ -8,6 +8,7 @@ module.exports = (function() {
this
.
source
=
srcDAO
this
.
target
=
targetDAO
this
.
options
=
options
this
.
isSingleAssociation
=
true
this
.
isSelfAssociation
=
(
this
.
source
.
tableName
==
this
.
target
.
tableName
)
if
(
this
.
isSelfAssociation
&&
!
this
.
options
.
foreignKey
&&
!!
this
.
options
.
as
)
{
...
...
lib/dao-factory.js
View file @
0691ced
...
...
@@ -561,8 +561,9 @@ module.exports = (function() {
if
(
options
.
hasOwnProperty
(
'include'
)
&&
(
!
options
.
includeValidated
||
!
options
.
includeNames
))
{
options
.
includeNames
=
[]
options
.
include
=
options
.
include
.
map
(
function
(
include
)
{
include
=
validateIncludedElement
.
call
(
this
,
include
)
options
.
includeNames
.
push
(
include
.
as
)
return
validateIncludedElement
.
call
(
this
,
include
)
return
include
}.
bind
(
this
))
}
...
...
@@ -1223,6 +1224,11 @@ module.exports = (function() {
var
usesAlias
=
(
include
.
as
!==
include
.
daoFactory
.
tableName
)
,
association
=
(
usesAlias
?
this
.
getAssociationByAlias
(
include
.
as
)
:
this
.
getAssociation
(
include
.
daoFactory
))
// If single (1:1) association, we singularize the alias, so it will match the automatically generated alias of belongsTo/HasOne
if
(
association
&&
!
usesAlias
&&
association
.
isSingleAssociation
)
{
include
.
as
=
Utils
.
singularize
(
include
.
daoFactory
.
tableName
,
include
.
daoFactory
.
options
.
language
)
}
// check if the current daoFactory is actually associated with the passed daoFactory
if
(
!!
association
&&
(
!
association
.
options
.
as
||
(
association
.
options
.
as
===
include
.
as
)))
{
include
.
association
=
association
...
...
lib/dao.js
View file @
0691ced
...
...
@@ -530,7 +530,7 @@ module.exports = (function() {
for
(
key
in
attrs
)
{
if
(
options
.
include
&&
options
.
includeNames
.
indexOf
(
key
)
!==
-
1
)
{
var
include
=
_
.
find
(
options
.
include
,
function
(
include
)
{
return
include
.
as
===
key
return
include
.
as
===
key
||
(
include
.
as
.
slice
(
0
,
1
).
toLowerCase
()
+
include
.
as
.
slice
(
1
))
===
key
})
var
association
=
include
.
association
,
self
=
this
...
...
@@ -544,7 +544,7 @@ module.exports = (function() {
var
daoInstance
=
include
.
daoFactory
.
build
(
data
,
{
isNewRecord
:
false
,
isDirty
:
false
})
,
isEmpty
=
!
Utils
.
firstValueOfHash
(
daoInstance
.
identifiers
)
if
(
[
'BelongsTo'
,
'HasOne'
].
indexOf
(
association
.
associationType
)
>
-
1
)
{
if
(
association
.
isSingleAssociation
)
{
accessor
=
Utils
.
singularize
(
accessor
,
self
.
sequelize
.
language
)
self
.
dataValues
[
accessor
]
=
isEmpty
?
null
:
daoInstance
self
[
accessor
]
=
self
.
dataValues
[
accessor
]
...
...
test/dao-factory.test.js
View file @
0691ced
...
...
@@ -368,6 +368,49 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
expect
(
p
.
price2
).
to
.
equal
(
20
)
done
()
})
/*describe('include', function () {
it('should support basic includes', function () {
var Product = this.sequelize.define('Product', {
title: Sequelize.STRING
})
var Tag = this.sequelize.define('Tag', {
name: Sequelize.STRING
})
var User = this.sequelize.define('User', {
first_name: Sequelize.STRING,
last_name: Sequelize.STRING
})
Product.hasMany(Tag)
Product.belongsTo(User)
var product = Product.build({
id: 1,
title: 'Chair',
tags: [
{id: 1, name: 'Alpha'},
{id: 2, name: 'Beta'}
],
user: {
id: 1,
first_name: 'Mick',
last_name: 'Hansen'
}
}, {
include: [
User,
Tag
]
})
expect(product.tags).to.be.ok
expect(product.tags.length).to.equal(2)
expect(product.tags[0].Model).to.equal(Tag)
expect(product.user).to.be.ok
expect(product.user.Model).to.equal(User)
})
})*/
})
describe
(
'findOrInitialize'
,
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