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 d9bd7a51
authored
Jun 09, 2013
by
Jan Aagaard Meier
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
hasMany setter uses bulk create and destroy to create and remove associations
1 parent
5e68ef9d
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
64 additions
and
91 deletions
lib/associations/has-many-double-linked.js
lib/associations/has-many-single-linked.js
lib/dialects/mysql/query-generator.js
lib/associations/has-many-double-linked.js
View file @
d9bd7a5
...
@@ -42,35 +42,52 @@ module.exports = (function() {
...
@@ -42,35 +42,52 @@ module.exports = (function() {
HasManyDoubleLinked
.
prototype
.
injectSetter
=
function
(
emitterProxy
,
oldAssociations
,
newAssociations
)
{
HasManyDoubleLinked
.
prototype
.
injectSetter
=
function
(
emitterProxy
,
oldAssociations
,
newAssociations
)
{
var
self
=
this
var
self
=
this
,
chainer
=
new
Utils
.
QueryChainer
()
destroyObsoleteAssociations
,
association
=
self
.
__factory
.
target
.
associations
[
self
.
__factory
.
associationAccessor
]
.
call
(
this
,
oldAssociations
,
newAssociations
)
,
foreignIdentifier
=
association
.
isSelfAssociation
?
association
.
foreignIdentifier
:
association
.
identifier
.
on
(
'sql'
,
function
(
sql
)
{
emitterProxy
.
emit
(
'sql'
,
sql
)
})
,
obsoleteAssociations
=
oldAssociations
.
filter
(
function
(
old
)
{
.
error
(
function
(
err
)
{
emitterProxy
.
emit
(
'error'
,
err
)
})
// Return only those old associations that are not found in new
.
success
(
function
()
{
return
!
Utils
.
_
.
find
(
newAssociations
,
function
(
obj
)
{
var
chainer
=
new
Utils
.
QueryChainer
return
obj
.
id
===
old
.
id
,
association
=
self
.
__factory
.
target
.
associations
[
self
.
__factory
.
associationAccessor
]
})
,
foreignIdentifier
=
association
.
isSelfAssociation
?
association
.
foreignIdentifier
:
association
.
identifier
,
unassociatedObjects
=
newAssociations
.
filter
(
function
(
obj
)
{
return
!
Utils
.
_
.
find
(
oldAssociations
,
function
(
old
)
{
return
obj
.
id
===
old
.
id
})
})
unassociatedObjects
.
forEach
(
function
(
unassociatedObject
)
{
var
attributes
=
{}
attributes
[
self
.
__factory
.
identifier
]
=
self
.
instance
.
id
attributes
[
foreignIdentifier
]
=
unassociatedObject
.
id
chainer
.
add
(
self
.
__factory
.
connectorDAO
.
create
(
attributes
))
})
})
,
unassociatedObjects
=
newAssociations
.
filter
(
function
(
obj
)
{
return
!
Utils
.
_
.
find
(
oldAssociations
,
function
(
old
)
{
return
obj
.
id
===
old
.
id
})
})
if
(
obsoleteAssociations
.
length
>
0
)
{
var
foreignIds
=
obsoleteAssociations
.
map
(
function
(
associatedObject
)
{
return
associatedObject
.
id
})
,
primaryKeys
=
Object
.
keys
(
self
.
__factory
.
connectorDAO
.
rawAttributes
)
,
foreignKey
=
primaryKeys
.
filter
(
function
(
pk
)
{
return
pk
!=
self
.
__factory
.
identifier
})[
0
]
chainer
var
where
=
{}
.
run
()
where
[
self
.
__factory
.
identifier
]
=
self
.
instance
.
id
.
success
(
function
()
{
emitterProxy
.
emit
(
'success'
,
newAssociations
)
})
where
[
foreignKey
]
=
foreignIds
.
error
(
function
(
err
)
{
emitterProxy
.
emit
(
'error'
,
err
)
})
.
on
(
'sql'
,
function
(
sql
)
{
emitterProxy
.
emit
(
'sql'
,
sql
)
})
chainer
.
add
(
self
.
__factory
.
connectorDAO
.
destroy
(
where
))
}
if
(
unassociatedObjects
.
length
>
0
)
{
var
bulk
=
unassociatedObjects
.
map
(
function
(
unassociatedObject
)
{
var
attributes
=
{}
attributes
[
self
.
__factory
.
identifier
]
=
self
.
instance
.
id
attributes
[
foreignIdentifier
]
=
unassociatedObject
.
id
return
attributes
})
})
chainer
.
add
(
self
.
__factory
.
connectorDAO
.
bulkCreate
(
bulk
))
}
chainer
.
run
()
.
success
(
function
()
{
emitterProxy
.
emit
(
'success'
,
newAssociations
)
})
.
error
(
function
(
err
)
{
emitterProxy
.
emit
(
'error'
,
err
)
})
.
on
(
'sql'
,
function
(
sql
)
{
emitterProxy
.
emit
(
'sql'
,
sql
)
})
}
}
HasManyDoubleLinked
.
prototype
.
injectAdder
=
function
(
emitterProxy
,
newAssociation
)
{
HasManyDoubleLinked
.
prototype
.
injectAdder
=
function
(
emitterProxy
,
newAssociation
)
{
...
@@ -87,57 +104,5 @@ module.exports = (function() {
...
@@ -87,57 +104,5 @@ module.exports = (function() {
.
on
(
'sql'
,
function
(
sql
)
{
emitterProxy
.
emit
(
'sql'
,
sql
)
})
.
on
(
'sql'
,
function
(
sql
)
{
emitterProxy
.
emit
(
'sql'
,
sql
)
})
}
}
// private
var
destroyObsoleteAssociations
=
function
(
oldAssociations
,
newAssociations
)
{
var
self
=
this
return
new
Utils
.
CustomEventEmitter
(
function
(
emitter
)
{
var
chainer
=
new
Utils
.
QueryChainer
()
var
foreignIdentifier
=
self
.
__factory
.
target
.
associations
[
self
.
__factory
.
associationAccessor
].
identifier
var
obsoleteAssociations
=
oldAssociations
.
filter
(
function
(
old
)
{
// Return only those old associations that are not found in new
return
!
Utils
.
_
.
find
(
newAssociations
,
function
(
obj
)
{
return
obj
.
id
===
old
.
id
})
})
if
(
obsoleteAssociations
.
length
===
0
)
{
return
emitter
.
emit
(
'success'
,
null
)
}
obsoleteAssociations
.
forEach
(
function
(
associatedObject
)
{
var
where
=
{}
,
primaryKeys
=
Object
.
keys
(
self
.
__factory
.
connectorDAO
.
rawAttributes
)
,
foreignKey
=
primaryKeys
.
filter
(
function
(
pk
)
{
return
pk
!=
self
.
__factory
.
identifier
})[
0
]
,
notFoundEmitters
=
[]
where
[
self
.
__factory
.
identifier
]
=
self
.
instance
.
id
where
[
foreignKey
]
=
associatedObject
.
id
self
.
__factory
.
connectorDAO
.
find
({
where
:
where
})
.
success
(
function
(
connector
)
{
if
(
connector
===
null
)
{
notFoundEmitters
.
push
(
null
)
}
else
{
chainer
.
add
(
connector
.
destroy
())
}
if
((
chainer
.
emitters
.
length
+
notFoundEmitters
.
length
)
===
obsoleteAssociations
.
length
)
{
// found all obsolete connectors and will delete them now
chainer
.
run
()
.
success
(
function
()
{
emitter
.
emit
(
'success'
,
null
)
})
.
error
(
function
(
err
)
{
emitter
.
emit
(
'error'
,
err
)
})
.
on
(
'sql'
,
function
(
sql
)
{
emitter
.
emit
(
'sql'
,
sql
)
})
}
})
.
error
(
function
(
err
)
{
emitter
.
emit
(
'error'
,
err
)
})
.
on
(
'sql'
,
function
(
sql
)
{
emitter
.
emit
(
'sql'
,
sql
)
})
})
}).
run
()
}
return
HasManyDoubleLinked
return
HasManyDoubleLinked
})()
})()
lib/associations/has-many-single-linked.js
View file @
d9bd7a5
...
@@ -7,7 +7,8 @@ module.exports = (function() {
...
@@ -7,7 +7,8 @@ module.exports = (function() {
}
}
HasManySingleLinked
.
prototype
.
injectGetter
=
function
(
options
)
{
HasManySingleLinked
.
prototype
.
injectGetter
=
function
(
options
)
{
var
where
=
{},
options
=
options
||
{}
var
where
=
{}
options
=
options
||
{}
where
[
this
.
__factory
.
identifier
]
=
this
.
instance
.
id
where
[
this
.
__factory
.
identifier
]
=
this
.
instance
.
id
...
@@ -17,7 +18,6 @@ module.exports = (function() {
...
@@ -17,7 +18,6 @@ module.exports = (function() {
HasManySingleLinked
.
prototype
.
injectSetter
=
function
(
emitter
,
oldAssociations
,
newAssociations
)
{
HasManySingleLinked
.
prototype
.
injectSetter
=
function
(
emitter
,
oldAssociations
,
newAssociations
)
{
var
self
=
this
var
self
=
this
,
options
=
this
.
__factory
.
options
,
chainer
=
new
Utils
.
QueryChainer
()
,
chainer
=
new
Utils
.
QueryChainer
()
,
obsoleteAssociations
=
oldAssociations
.
filter
(
function
(
old
)
{
,
obsoleteAssociations
=
oldAssociations
.
filter
(
function
(
old
)
{
return
!
Utils
.
_
.
find
(
newAssociations
,
function
(
obj
)
{
return
!
Utils
.
_
.
find
(
newAssociations
,
function
(
obj
)
{
...
@@ -29,23 +29,31 @@ module.exports = (function() {
...
@@ -29,23 +29,31 @@ module.exports = (function() {
return
obj
.
id
===
old
.
id
return
obj
.
id
===
old
.
id
})
})
})
})
,
update
// clear the old associations
if
(
obsoleteAssociations
.
length
>
0
)
{
obsoleteAssociations
.
forEach
(
function
(
associatedObject
)
{
// clear the old associations
associatedObject
[
self
.
__factory
.
identifier
]
=
null
var
obsoleteIds
=
obsoleteAssociations
.
map
(
function
(
associatedObject
)
{
return
associatedObject
.
id
})
chainer
.
add
(
associatedObject
.
save
())
})
// set the new associations
update
=
{}
unassociatedObjects
.
forEach
(
function
(
associatedObject
)
{
update
[
self
.
__factory
.
identifier
]
=
null
associatedObject
[
self
.
__factory
.
identifier
]
=
self
.
instance
.
id
chainer
.
add
(
this
.
__factory
.
target
.
update
(
update
,
{
id
:
obsoleteIds
}))
chainer
.
add
(
associatedObject
.
save
())
}
})
if
(
unassociatedObjects
.
length
>
0
)
{
// set the new associations
var
unassociatedIds
=
unassociatedObjects
.
map
(
function
(
associatedObject
)
{
return
associatedObject
.
id
})
update
=
{}
update
[
self
.
__factory
.
identifier
]
=
self
.
instance
.
id
chainer
.
add
(
this
.
__factory
.
target
.
update
(
update
,
{
id
:
unassociatedIds
}))
}
chainer
chainer
.
run
()
.
run
()
.
success
(
function
()
{
emitter
.
emit
(
'success'
,
newAssociations
)
})
.
success
(
function
()
{
emitter
.
emit
(
'success'
,
newAssociations
)
})
.
error
(
function
(
err
)
{
emitter
.
emit
(
'error'
,
err
)
})
.
error
(
function
(
err
)
{
emitter
.
emit
(
'error'
,
err
)
})
.
on
(
'sql'
,
function
(
sql
)
{
emitterProxy
.
emit
(
'sql'
,
sql
)
})
}
}
HasManySingleLinked
.
prototype
.
injectAdder
=
function
(
emitterProxy
,
newAssociation
)
{
HasManySingleLinked
.
prototype
.
injectAdder
=
function
(
emitterProxy
,
newAssociation
)
{
...
...
lib/dialects/mysql/query-generator.js
View file @
d9bd7a5
...
@@ -431,7 +431,7 @@ module.exports = (function() {
...
@@ -431,7 +431,7 @@ module.exports = (function() {
result
.
push
([
_key
,
_value
].
join
(
" IN "
))
result
.
push
([
_key
,
_value
].
join
(
" IN "
))
}
else
if
((
value
)
&&
(
typeof
value
==
'object'
)
&&
!
(
value
instanceof
Date
))
{
}
else
if
((
value
)
&&
(
typeof
value
==
'object'
)
&&
!
(
value
instanceof
Date
))
{
// is value an object?
// is value an object?
//using as sentinel for join column => value
//using as sentinel for join column => value
_value
=
this
.
quoteIdentifiers
(
value
.
join
)
_value
=
this
.
quoteIdentifiers
(
value
.
join
)
result
.
push
([
_key
,
_value
].
join
(
"="
))
result
.
push
([
_key
,
_value
].
join
(
"="
))
...
...
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