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 af848776
authored
Dec 30, 2013
by
Mick Hansen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add support for simple nested belongsTo -> hasMany include
1 parent
e0804c74
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
76 additions
and
21 deletions
lib/dialects/abstract/query.js
lib/dialects/postgres/query.js
lib/dialects/sqlite/query.js
test/include.test.js
lib/dialects/abstract/query.js
View file @
af84877
...
@@ -220,7 +220,7 @@ module.exports = (function() {
...
@@ -220,7 +220,7 @@ module.exports = (function() {
// Queries with include
// Queries with include
}
else
if
(
this
.
options
.
hasJoin
===
true
)
{
}
else
if
(
this
.
options
.
hasJoin
===
true
)
{
results
=
groupJoinData
(
results
,
this
.
options
)
results
=
groupJoinData
(
results
,
this
.
options
)
//
console.log(JSON.stringify(results, null, '\t'));
console
.
log
(
JSON
.
stringify
(
results
,
null
,
'\t'
));
result
=
results
.
map
(
function
(
result
)
{
result
=
results
.
map
(
function
(
result
)
{
return
this
.
callee
.
build
(
result
,
{
return
this
.
callee
.
build
(
result
,
{
isNewRecord
:
false
,
isNewRecord
:
false
,
...
@@ -328,7 +328,10 @@ module.exports = (function() {
...
@@ -328,7 +328,10 @@ module.exports = (function() {
calleeData
=
_
.
omit
(
row
,
options
.
includeNames
)
calleeData
=
_
.
omit
(
row
,
options
.
includeNames
)
existingResult
=
_
.
find
(
results
,
function
(
result
)
{
existingResult
=
_
.
find
(
results
,
function
(
result
)
{
return
Utils
.
_
.
isEqual
(
_
.
omit
(
result
,
options
.
includeNames
),
calleeData
)
if
(
options
.
includeNames
)
{
return
Utils
.
_
.
isEqual
(
_
.
omit
(
result
,
options
.
includeNames
.
concat
([
'__children'
])),
calleeData
)
}
return
Utils
.
_
.
isEqual
(
result
,
calleeData
)
})
})
if
(
!
existingResult
)
{
if
(
!
existingResult
)
{
...
@@ -337,22 +340,22 @@ module.exports = (function() {
...
@@ -337,22 +340,22 @@ module.exports = (function() {
for
(
var
attrName
in
row
)
{
for
(
var
attrName
in
row
)
{
if
(
row
.
hasOwnProperty
(
attrName
))
{
if
(
row
.
hasOwnProperty
(
attrName
))
{
child
=
Object
(
row
[
attrName
])
===
row
[
attrName
]
&&
options
.
includeMap
[
attrName
]
child
=
Object
(
row
[
attrName
])
===
row
[
attrName
]
&&
options
.
includeMap
&&
options
.
includeMap
[
attrName
]
if
(
child
)
{
if
(
child
)
{
existingResult
[
attrName
]
=
existingResult
[
attrName
]
||
[]
if
(
!
existingResult
.
__children
)
existingResult
.
__children
=
{}
var
attrRowExists
=
existingResult
[
attrName
].
some
(
function
(
attrRow
)
{
if
(
!
existingResult
.
__children
[
attrName
])
existingResult
.
__children
[
attrName
]
=
[];
return
Utils
.
_
.
isEqual
(
attrRow
,
row
[
attrName
])
})
existingResult
.
__children
[
attrName
].
push
(
row
[
attrName
])
if
(
!
attrRowExists
)
{
if
(
child
.
include
)
{
existingResult
[
attrName
].
push
(
groupJoinData
([
row
[
attrName
]],
child
)[
0
])
}
else
{
existingResult
[
attrName
].
push
(
row
[
attrName
])
}
}
}
}
}
}
}
}
})
results
.
forEach
(
function
(
result
)
{
_
.
each
(
result
.
__children
,
function
(
children
,
key
)
{
result
[
key
]
=
groupJoinData
(
children
,
options
.
includeMap
[
key
])
})
delete
result
.
__children
})
})
return
results
return
results
}
}
...
...
lib/dialects/postgres/query.js
View file @
af84877
...
@@ -142,10 +142,8 @@ module.exports = (function() {
...
@@ -142,10 +142,8 @@ module.exports = (function() {
this
.
callee
[
key
]
=
record
this
.
callee
[
key
]
=
record
}
}
}
}
this
.
emit
(
'success'
,
this
.
callee
)
}
}
else
{
this
.
emit
(
'success'
,
this
.
callee
)
this
.
emit
(
'success'
,
rows
)
}
}
else
if
(
this
.
send
(
'isUpdateQuery'
))
{
}
else
if
(
this
.
send
(
'isUpdateQuery'
))
{
if
(
this
.
callee
!==
null
)
{
// may happen for bulk updates
if
(
this
.
callee
!==
null
)
{
// may happen for bulk updates
for
(
var
key
in
rows
[
0
])
{
for
(
var
key
in
rows
[
0
])
{
...
...
lib/dialects/sqlite/query.js
View file @
af84877
...
@@ -89,7 +89,7 @@ module.exports = (function() {
...
@@ -89,7 +89,7 @@ module.exports = (function() {
}
}
var
onSuccess
=
function
(
results
,
metaData
)
{
var
onSuccess
=
function
(
results
,
metaData
)
{
var
result
=
this
.
callee
||
results
var
result
=
this
.
callee
// add the inserted row id to the instance
// add the inserted row id to the instance
if
(
this
.
send
(
'isInsertQuery'
,
results
,
metaData
))
{
if
(
this
.
send
(
'isInsertQuery'
,
results
,
metaData
))
{
...
...
test/include.test.js
View file @
af84877
...
@@ -161,7 +161,61 @@ describe(Support.getTestDialectTeaser("Include"), function () {
...
@@ -161,7 +161,61 @@ describe(Support.getTestDialectTeaser("Include"), function () {
user
.
tasks
.
forEach
(
function
(
task
)
{
user
.
tasks
.
forEach
(
function
(
task
)
{
expect
(
task
.
project
).
to
.
be
.
ok
expect
(
task
.
project
).
to
.
be
.
ok
})
})
done
()
})
})
})
})
it
(
'should support a simple nested belongsTo -> hasMany include'
,
function
(
done
)
{
var
Task
=
this
.
sequelize
.
define
(
'Task'
,
{})
,
Worker
=
this
.
sequelize
.
define
(
'Worker'
,
{})
,
Project
=
this
.
sequelize
.
define
(
'Project'
,
{})
Worker
.
belongsTo
(
Project
)
Project
.
hasMany
(
Task
)
this
.
sequelize
.
sync
({
force
:
true
}).
done
(
function
()
{
async
.
auto
({
worker
:
function
(
callback
)
{
Worker
.
create
().
done
(
callback
)
},
project
:
function
(
callback
)
{
Project
.
create
().
done
(
callback
)
},
tasks
:
function
(
callback
)
{
Task
.
bulkCreate
([
{},
{},
{},
{}
]).
done
(
function
()
{
Task
.
findAll
().
done
(
callback
)
})
},
projectTasks
:
[
'project'
,
'tasks'
,
function
(
callback
,
results
)
{
results
.
project
.
setTasks
(
results
.
tasks
).
done
(
callback
)
}],
projectWorker
:
[
'project'
,
'worker'
,
function
(
callback
,
results
)
{
results
.
worker
.
setProject
(
results
.
project
).
done
(
callback
)
}]
},
function
(
err
,
results
)
{
Worker
.
find
({
where
:
{
id
:
results
.
worker
.
id
},
include
:
[
{
model
:
Project
,
include
:
[
{
model
:
Task
}
]}
]
}).
done
(
function
(
err
,
worker
)
{
expect
(
err
).
not
.
to
.
be
.
ok
expect
(
worker
.
project
).
to
.
be
.
ok
expect
(
worker
.
project
.
tasks
).
to
.
be
.
ok
expect
(
worker
.
project
.
tasks
.
length
).
to
.
equal
(
4
)
done
()
done
()
})
})
})
})
...
...
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