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 c7bee173
authored
Dec 04, 2012
by
Jisoo Park
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix #334 ('include' places a "null" element to the result if no data is related)
1 parent
c05f7acc
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
95 additions
and
2 deletions
lib/dialects/abstract/query.js
lib/utils.js
spec/dao-factory.spec.js
lib/dialects/abstract/query.js
View file @
c7bee17
...
@@ -284,13 +284,15 @@ module.exports = (function() {
...
@@ -284,13 +284,15 @@ module.exports = (function() {
associationData
.
forEach
(
function
(
data
)
{
associationData
.
forEach
(
function
(
data
)
{
var
daoInstance
=
associatedDaoFactory
.
build
(
data
,
{
isNewRecord
:
false
})
var
daoInstance
=
associatedDaoFactory
.
build
(
data
,
{
isNewRecord
:
false
})
,
identifier
=
associatedDaoFactory
.
hasPrimaryKeys
?
Utils
.
firstKeyOfHash
(
associatedDaoFactory
.
primaryKeys
)
:
'id'
if
([
'BelongsTo'
,
'HasOne'
].
indexOf
(
association
.
associationType
)
>
-
1
)
{
if
([
'BelongsTo'
,
'HasOne'
].
indexOf
(
association
.
associationType
)
>
-
1
)
{
accessor
=
Utils
.
singularize
(
accessor
)
accessor
=
Utils
.
singularize
(
accessor
)
dao
[
accessor
]
=
daoInstance
dao
[
accessor
]
=
daoInstance
[
identifier
]
?
daoInstance
:
null
}
else
{
}
else
{
dao
[
accessor
]
=
dao
[
accessor
]
||
[]
dao
[
accessor
]
=
dao
[
accessor
]
||
[]
dao
[
accessor
].
push
(
daoInstance
)
if
(
daoInstance
[
identifier
])
dao
[
accessor
].
push
(
daoInstance
)
}
}
})
})
}
}
...
...
lib/utils.js
View file @
c7bee17
...
@@ -149,6 +149,14 @@ var Utils = module.exports = {
...
@@ -149,6 +149,14 @@ var Utils = module.exports = {
}
}
},
},
firstKeyOfHash
:
function
(
obj
)
{
for
(
var
key
in
obj
)
{
if
(
obj
.
hasOwnProperty
(
key
))
return
key
}
return
null
},
inherit
:
function
(
subClass
,
superClass
)
{
inherit
:
function
(
subClass
,
superClass
)
{
if
(
superClass
.
constructor
==
Function
)
{
if
(
superClass
.
constructor
==
Function
)
{
// Normal Inheritance
// Normal Inheritance
...
@@ -164,6 +172,7 @@ var Utils = module.exports = {
...
@@ -164,6 +172,7 @@ var Utils = module.exports = {
return
subClass
;
return
subClass
;
}
}
}
}
Utils
.
CustomEventEmitter
=
require
(
"./emitters/custom-event-emitter"
)
Utils
.
CustomEventEmitter
=
require
(
"./emitters/custom-event-emitter"
)
...
...
spec/dao-factory.spec.js
View file @
c7bee17
...
@@ -500,6 +500,25 @@ describe(Helpers.getTestDialectTeaser("DAOFactory"), function() {
...
@@ -500,6 +500,25 @@ describe(Helpers.getTestDialectTeaser("DAOFactory"), function() {
}.
bind
(
this
))
//- sequelize.sync
}.
bind
(
this
))
//- sequelize.sync
})
})
it
(
'fetches no associated object if none is set (1st direction)'
,
function
(
done
)
{
this
.
User
.
hasOne
(
this
.
Task
)
this
.
Task
.
belongsTo
(
this
.
User
)
this
.
sequelize
.
sync
({
force
:
true
}).
success
(
function
()
{
this
.
User
.
create
({
name
:
'barfooz'
}).
success
(
function
(
user
)
{
this
.
Task
.
create
({
title
:
'task'
}).
success
(
function
(
task
)
{
this
.
User
.
find
({
where
:
{
'UserWithNames.id'
:
1
},
include
:
[
'Task'
]
}).
success
(
function
(
user
)
{
expect
(
user
.
task
).
toEqual
(
null
)
done
()
})
}.
bind
(
this
))
//- Task.create
}.
bind
(
this
))
//- User.create
}.
bind
(
this
))
//- sequelize.sync
})
it
(
'fetches associated objects via "as" param (1st direction)'
,
function
(
done
)
{
it
(
'fetches associated objects via "as" param (1st direction)'
,
function
(
done
)
{
this
.
User
.
hasOne
(
this
.
Task
,
{
as
:
'Homework'
})
this
.
User
.
hasOne
(
this
.
Task
,
{
as
:
'Homework'
})
this
.
Task
.
belongsTo
(
this
.
User
)
this
.
Task
.
belongsTo
(
this
.
User
)
...
@@ -546,6 +565,27 @@ describe(Helpers.getTestDialectTeaser("DAOFactory"), function() {
...
@@ -546,6 +565,27 @@ describe(Helpers.getTestDialectTeaser("DAOFactory"), function() {
}.
bind
(
this
))
//- sequelize.sync
}.
bind
(
this
))
//- sequelize.sync
})
})
it
(
'fetches no associated object if none is set (2nd direction)'
,
function
(
done
)
{
this
.
User
.
hasOne
(
this
.
Task
)
this
.
Task
.
belongsTo
(
this
.
User
)
this
.
sequelize
.
sync
({
force
:
true
}).
success
(
function
()
{
this
.
User
.
create
({
name
:
'barfooz'
}).
success
(
function
(
user
)
{
this
.
User
.
create
({
name
:
'another user'
}).
success
(
function
(
another_user
)
{
this
.
Task
.
create
({
title
:
'task'
}).
success
(
function
(
task
)
{
this
.
Task
.
find
({
where
:
{
'Tasks.id'
:
1
},
include
:
[
'UserWithName'
]
}).
success
(
function
(
task
)
{
expect
(
task
.
userWithName
).
toEqual
(
null
)
done
()
})
}.
bind
(
this
))
//- Task.create
}.
bind
(
this
))
//- User.create
}.
bind
(
this
))
//- User.create
}.
bind
(
this
))
//- sequelize.sync
})
it
(
'fetches associated object via "as" param (2nd direction)'
,
function
(
done
)
{
it
(
'fetches associated object via "as" param (2nd direction)'
,
function
(
done
)
{
this
.
User
.
hasOne
(
this
.
Task
)
this
.
User
.
hasOne
(
this
.
Task
)
this
.
Task
.
belongsTo
(
this
.
User
,
{
as
:
'Owner'
})
this
.
Task
.
belongsTo
(
this
.
User
,
{
as
:
'Owner'
})
...
@@ -627,6 +667,25 @@ describe(Helpers.getTestDialectTeaser("DAOFactory"), function() {
...
@@ -627,6 +667,25 @@ describe(Helpers.getTestDialectTeaser("DAOFactory"), function() {
}.
bind
(
this
))
//- sequelize.sync
}.
bind
(
this
))
//- sequelize.sync
})
})
it
(
'fetches no associated objects for 1:N associations if none are set (1st direction)'
,
function
(
done
)
{
this
.
User
.
hasMany
(
this
.
Task
)
this
.
Task
.
belongsTo
(
this
.
User
)
this
.
sequelize
.
sync
({
force
:
true
}).
success
(
function
()
{
this
.
User
.
create
({
name
:
'barfooz'
}).
success
(
function
(
user
)
{
this
.
Task
.
create
({
title
:
'task1'
}).
success
(
function
(
task1
)
{
this
.
User
.
find
({
where
:
{
'UserWithNames.id'
:
1
},
include
:
[
'Task'
]
}).
success
(
function
(
user
)
{
expect
(
user
.
tasks
.
length
).
toEqual
(
0
)
done
()
})
}.
bind
(
this
))
//- Task.create
}.
bind
(
this
))
//- User.create
}.
bind
(
this
))
//- sequelize.sync
})
it
(
'fetches associated objects for 1:N associations (2nd direction)'
,
function
(
done
)
{
it
(
'fetches associated objects for 1:N associations (2nd direction)'
,
function
(
done
)
{
this
.
User
.
hasMany
(
this
.
Task
)
this
.
User
.
hasMany
(
this
.
Task
)
this
.
Task
.
belongsTo
(
this
.
User
)
this
.
Task
.
belongsTo
(
this
.
User
)
...
@@ -705,6 +764,29 @@ describe(Helpers.getTestDialectTeaser("DAOFactory"), function() {
...
@@ -705,6 +764,29 @@ describe(Helpers.getTestDialectTeaser("DAOFactory"), function() {
}.
bind
(
this
))
//- sequelize.sync
}.
bind
(
this
))
//- sequelize.sync
})
})
it
(
'fetches no associated objects for N:M associations if none are set (1st direction)'
,
function
(
done
)
{
this
.
User
.
hasMany
(
this
.
Task
)
this
.
Task
.
hasMany
(
this
.
User
)
this
.
sequelize
.
sync
({
force
:
true
}).
success
(
function
()
{
this
.
User
.
create
({
name
:
'barfooz'
}).
success
(
function
(
user1
)
{
this
.
Task
.
create
({
title
:
'task1'
}).
success
(
function
(
task1
)
{
this
.
Task
.
create
({
title
:
'task2'
}).
success
(
function
(
task2
)
{
this
.
User
.
find
({
where
:
{
'UserWithNames.id'
:
user1
.
id
},
include
:
[
'Task'
]
}).
success
(
function
(
user
)
{
expect
(
user
.
tasks
.
length
).
toEqual
(
0
)
done
()
})
}.
bind
(
this
))
//- Task.create
}.
bind
(
this
))
//- Task.create
}.
bind
(
this
))
//- User.create
}.
bind
(
this
))
//- sequelize.sync
})
it
(
'fetches associated objects via "as" param for N:M associations (1st direction)'
,
function
(
done
)
{
it
(
'fetches associated objects via "as" param for N:M associations (1st direction)'
,
function
(
done
)
{
this
.
User
.
hasMany
(
this
.
Task
,
{
as
:
'Homeworks'
})
this
.
User
.
hasMany
(
this
.
Task
,
{
as
:
'Homeworks'
})
this
.
Task
.
hasMany
(
this
.
User
,
{
as
:
'Owners'
})
this
.
Task
.
hasMany
(
this
.
User
,
{
as
:
'Owners'
})
...
...
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