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 9fdc0b80
authored
Mar 04, 2013
by
Sascha Depold
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add the eagerly loaded data to the toJSON and values method
1 parent
d259cec7
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
44 additions
and
12 deletions
lib/dao.js
lib/dialects/abstract/query.js
spec-jasmine/dao.spec.js
spec/dao.spec.js
lib/dao.js
View file @
9fdc0b8
...
@@ -5,10 +5,12 @@ var Utils = require("./utils")
...
@@ -5,10 +5,12 @@ var Utils = require("./utils")
module
.
exports
=
(
function
()
{
module
.
exports
=
(
function
()
{
var
DAO
=
function
(
values
,
options
,
isNewRecord
)
{
var
DAO
=
function
(
values
,
options
,
isNewRecord
)
{
var
self
=
this
;
var
self
=
this
this
.
__options
=
options
;
this
.
hasPrimaryKeys
=
options
.
hasPrimaryKeys
;
this
.
__options
=
options
this
.
selectedValues
=
values
;
this
.
hasPrimaryKeys
=
options
.
hasPrimaryKeys
this
.
selectedValues
=
values
this
.
__eagerlyLoadedAssociations
=
[]
initAttributes
.
call
(
this
,
values
,
isNewRecord
)
initAttributes
.
call
(
this
,
values
,
isNewRecord
)
...
@@ -51,7 +53,7 @@ module.exports = (function() {
...
@@ -51,7 +53,7 @@ module.exports = (function() {
var
result
=
{}
var
result
=
{}
,
self
=
this
,
self
=
this
this
.
attributes
.
forEach
(
function
(
attr
)
{
this
.
attributes
.
concat
(
this
.
__eagerlyLoadedAssociations
).
forEach
(
function
(
attr
)
{
result
[
attr
]
=
self
[
attr
]
result
[
attr
]
=
self
[
attr
]
})
})
...
...
lib/dialects/abstract/query.js
View file @
9fdc0b8
...
@@ -318,9 +318,14 @@ module.exports = (function() {
...
@@ -318,9 +318,14 @@ module.exports = (function() {
dao
[
accessor
]
=
isEmpty
?
null
:
daoInstance
dao
[
accessor
]
=
isEmpty
?
null
:
daoInstance
}
else
{
}
else
{
dao
[
accessor
]
=
dao
[
accessor
]
||
[]
dao
[
accessor
]
=
dao
[
accessor
]
||
[]
if
(
!
isEmpty
)
if
(
!
isEmpty
)
{
dao
[
accessor
].
push
(
daoInstance
)
dao
[
accessor
].
push
(
daoInstance
)
}
}
}
// add the accessor to the eagerly loaded associations array
dao
.
__eagerlyLoadedAssociations
=
Utils
.
_
.
uniq
(
dao
.
__eagerlyLoadedAssociations
.
concat
([
accessor
]))
})
})
}
}
...
...
spec-jasmine/dao.spec.js
View file @
9fdc0b8
...
@@ -334,7 +334,6 @@ describe('DAO', function() {
...
@@ -334,7 +334,6 @@ describe('DAO', function() {
})
})
})
})
})
})
})
})
})
})
})
})
spec/dao.spec.js
View file @
9fdc0b8
...
@@ -274,12 +274,14 @@ describe(Helpers.getTestDialectTeaser("DAO"), function() {
...
@@ -274,12 +274,14 @@ describe(Helpers.getTestDialectTeaser("DAO"), function() {
username
:
Helpers
.
Sequelize
.
STRING
,
username
:
Helpers
.
Sequelize
.
STRING
,
age
:
Helpers
.
Sequelize
.
INTEGER
,
age
:
Helpers
.
Sequelize
.
INTEGER
,
isAdmin
:
Helpers
.
Sequelize
.
BOOLEAN
isAdmin
:
Helpers
.
Sequelize
.
BOOLEAN
},
{
},
{
timestamps
:
false
})
timestamps
:
false
,
logging
:
true
this
.
Project
=
this
.
sequelize
.
define
(
'NiceProject'
,
{
title
:
Helpers
.
Sequelize
.
STRING
},
{
timestamps
:
false
})
})
this
.
User
.
hasMany
(
this
.
Project
,
{
as
:
'Projects'
})
this
.
Project
.
belongsTo
(
this
.
User
,
{
as
:
'LovelyUser'
})
this
.
User
.
sync
({
force
:
true
}).
success
(
done
)
this
.
sequelize
.
sync
({
force
:
true
}).
success
(
done
)
})
})
it
(
'returns an object containing all values'
,
function
()
{
it
(
'returns an object containing all values'
,
function
()
{
...
@@ -296,6 +298,30 @@ describe(Helpers.getTestDialectTeaser("DAO"), function() {
...
@@ -296,6 +298,30 @@ describe(Helpers.getTestDialectTeaser("DAO"), function() {
var
user
=
this
.
User
.
build
({
username
:
'test.user'
,
age
:
99
,
isAdmin
:
true
})
var
user
=
this
.
User
.
build
({
username
:
'test.user'
,
age
:
99
,
isAdmin
:
true
})
expect
(
JSON
.
parse
(
JSON
.
stringify
(
user
))).
toEqual
({
username
:
'test.user'
,
age
:
99
,
isAdmin
:
true
,
id
:
null
})
expect
(
JSON
.
parse
(
JSON
.
stringify
(
user
))).
toEqual
({
username
:
'test.user'
,
age
:
99
,
isAdmin
:
true
,
id
:
null
})
})
})
it
(
'includes the eagerly loaded associations'
,
function
(
done
)
{
this
.
User
.
create
({
username
:
'fnord'
,
age
:
1
,
isAdmin
:
true
}).
success
(
function
(
user
)
{
this
.
Project
.
create
({
title
:
'fnord'
}).
success
(
function
(
project
)
{
user
.
setProjects
([
project
]).
success
(
function
()
{
this
.
User
.
findAll
({
include
:
[
{
model
:
this
.
Project
,
as
:
'Projects'
}
]}).
success
(
function
(
users
)
{
var
_user
=
users
[
0
]
expect
(
_user
.
projects
).
toBeDefined
()
expect
(
JSON
.
parse
(
JSON
.
stringify
(
_user
)).
projects
).
toBeDefined
()
this
.
Project
.
findAll
({
include
:
[
{
model
:
this
.
User
,
as
:
'LovelyUser'
}
]}).
success
(
function
(
projects
)
{
var
_project
=
projects
[
0
]
expect
(
_project
.
lovelyUser
).
toBeDefined
()
expect
(
JSON
.
parse
(
JSON
.
stringify
(
_project
)).
lovelyUser
).
toBeDefined
()
done
()
})
}.
bind
(
this
))
}.
bind
(
this
))
}.
bind
(
this
))
}.
bind
(
this
))
})
})
})
describe
(
'findAll'
,
function
findAll
()
{
describe
(
'findAll'
,
function
findAll
()
{
...
...
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