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 6b0d90dd
authored
Nov 17, 2013
by
Sascha Depold
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
transaction support for n:m associations
1 parent
3c31bca0
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
58 additions
and
17 deletions
lib/associations/has-many-double-linked.js
lib/dao-factory.js
test/associations/has-many.test.js
lib/associations/has-many-double-linked.js
View file @
6b0d90d
var
Utils
=
require
(
'./../utils'
)
var
Utils
=
require
(
'./../utils'
)
,
Transaction
=
require
(
'./../transaction'
)
module
.
exports
=
(
function
()
{
var
HasManyDoubleLinked
=
function
(
definition
,
instance
)
{
...
...
@@ -46,14 +47,14 @@ module.exports = (function() {
if
(
options
.
joinTableAttributes
)
{
options
.
joinTableAttributes
.
forEach
(
function
(
elem
)
{
options
.
attributes
.
push
(
self
.
QueryInterface
.
quoteIdentifiers
(
connectorDAO
.
tableName
+
'.'
+
elem
)
+
' as '
+
self
.
QueryInterface
.
quoteIdentifiers
(
connectorDAO
.
tableName
+
'.'
+
elem
)
+
' as '
+
self
.
QueryInterface
.
quoteIdentifier
(
connectorDAO
.
name
+
'.'
+
elem
,
true
)
)
})
}
else
{
Utils
.
_
.
forOwn
(
connectorDAO
.
rawAttributes
,
function
(
elem
,
key
)
{
options
.
attributes
.
push
(
self
.
QueryInterface
.
quoteIdentifiers
(
connectorDAO
.
tableName
+
'.'
+
key
)
+
' as '
+
self
.
QueryInterface
.
quoteIdentifiers
(
connectorDAO
.
tableName
+
'.'
+
key
)
+
' as '
+
self
.
QueryInterface
.
quoteIdentifier
(
connectorDAO
.
name
+
'.'
+
key
,
true
)
)
})
...
...
@@ -88,16 +89,22 @@ module.exports = (function() {
}
HasManyDoubleLinked
.
prototype
.
injectSetter
=
function
(
emitterProxy
,
oldAssociations
,
newAssociations
,
defaultAttributes
)
{
var
self
=
this
,
chainer
=
new
Utils
.
QueryChainer
()
,
association
=
self
.
__factory
.
target
.
associations
[
self
.
__factory
.
associationAccessor
]
,
foreignIdentifier
=
association
.
isSelfAssociation
?
association
.
foreignIdentifier
:
association
.
identifier
,
sourceKeys
=
Object
.
keys
(
self
.
__factory
.
source
.
primaryKeys
)
,
targetKeys
=
Object
.
keys
(
self
.
__factory
.
target
.
primaryKeys
)
var
self
=
this
,
chainer
=
new
Utils
.
QueryChainer
()
,
association
=
self
.
__factory
.
target
.
associations
[
self
.
__factory
.
associationAccessor
]
,
foreignIdentifier
=
association
.
isSelfAssociation
?
association
.
foreignIdentifier
:
association
.
identifier
,
sourceKeys
=
Object
.
keys
(
self
.
__factory
.
source
.
primaryKeys
)
,
targetKeys
=
Object
.
keys
(
self
.
__factory
.
target
.
primaryKeys
)
,
obsoleteAssociations
=
[]
,
changedAssociations
=
[]
,
changedAssociations
=
[]
,
options
=
{}
,
unassociatedObjects
;
if
((
defaultAttributes
||
{}).
transaction
instanceof
Transaction
)
{
options
.
transaction
=
defaultAttributes
.
transaction
delete
defaultAttributes
.
transaction
}
unassociatedObjects
=
newAssociations
.
filter
(
function
(
obj
)
{
return
!
Utils
.
_
.
find
(
oldAssociations
,
function
(
old
)
{
return
(
!!
obj
[
foreignIdentifier
]
&&
!!
old
[
foreignIdentifier
]
?
obj
[
foreignIdentifier
]
===
old
[
foreignIdentifier
]
:
obj
.
id
===
old
.
id
)
...
...
@@ -119,7 +126,7 @@ module.exports = (function() {
changedAssociation
.
where
[
self
.
__factory
.
identifier
]
=
self
.
instance
[
self
.
__factory
.
identifier
]
||
self
.
instance
.
id
changedAssociation
.
where
[
foreignIdentifier
]
=
newObj
[
foreignIdentifier
]
||
newObj
.
id
changedAssociations
.
push
(
changedAssociation
)
}
})
...
...
@@ -130,31 +137,33 @@ module.exports = (function() {
})
var
where
=
{}
where
[
self
.
__factory
.
identifier
]
=
((
sourceKeys
.
length
===
1
)
?
self
.
instance
[
sourceKeys
[
0
]]
:
self
.
instance
.
id
)
where
[
foreignIdentifier
]
=
foreignIds
chainer
.
add
(
self
.
__factory
.
connectorDAO
.
destroy
(
where
))
chainer
.
add
(
self
.
__factory
.
connectorDAO
.
destroy
(
where
,
options
))
}
if
(
unassociatedObjects
.
length
>
0
)
{
var
bulk
=
unassociatedObjects
.
map
(
function
(
unassociatedObject
)
{
var
attributes
=
{}
attributes
[
self
.
__factory
.
identifier
]
=
((
sourceKeys
.
length
===
1
)
?
self
.
instance
[
sourceKeys
[
0
]]
:
self
.
instance
.
id
)
attributes
[
foreignIdentifier
]
=
((
targetKeys
.
length
===
1
)
?
unassociatedObject
[
targetKeys
[
0
]]
:
unassociatedObject
.
id
)
if
(
association
.
hasJoinTableModel
)
{
attributes
=
Utils
.
_
.
defaults
(
attributes
,
unassociatedObject
[
association
.
connectorDAO
.
name
],
defaultAttributes
)
}
return
attributes
})
chainer
.
add
(
self
.
__factory
.
connectorDAO
.
bulkCreate
(
bulk
))
chainer
.
add
(
self
.
__factory
.
connectorDAO
.
bulkCreate
(
bulk
,
options
))
}
if
(
changedAssociations
.
length
>
0
)
{
changedAssociations
.
forEach
(
function
(
assoc
)
{
chainer
.
add
(
self
.
__factory
.
connectorDAO
.
update
(
assoc
.
attributes
,
assoc
.
where
))
chainer
.
add
(
self
.
__factory
.
connectorDAO
.
update
(
assoc
.
attributes
,
assoc
.
where
,
options
))
})
}
...
...
@@ -189,7 +198,7 @@ module.exports = (function() {
this
.
__factory
.
connectorDAO
.
create
(
attributes
)
.
success
(
function
()
{
emitterProxy
.
emit
(
'success'
,
newAssociation
)
})
.
error
(
function
(
err
)
{
emitterProxy
.
emit
(
'error'
,
err
)
})
.
on
(
'sql'
,
function
(
sql
)
{
emitterProxy
.
emit
(
'sql'
,
sql
)
})
.
on
(
'sql'
,
function
(
sql
)
{
emitterProxy
.
emit
(
'sql'
,
sql
)
})
}
}
...
...
lib/dao-factory.js
View file @
6b0d90d
...
...
@@ -372,7 +372,7 @@ module.exports = (function() {
return
this
.
QueryInterface
.
select
(
this
,
[
this
.
getTableName
(),
joinTableName
],
optcpy
,
Utils
.
_
.
defaults
({
type
:
'SELECT'
},
queryOptions
))
},
queryOptions
,
{
transaction
:
(
options
||
{}).
transaction
}
))
}
/**
...
...
test/associations/has-many.test.js
View file @
6b0d90d
...
...
@@ -459,6 +459,38 @@ describe(Support.getTestDialectTeaser("HasMany"), function() {
})
})
it
(
'supports transactions'
,
function
(
done
)
{
Support
.
prepareTransactionTest
(
this
.
sequelize
,
function
(
sequelize
)
{
var
Article
=
sequelize
.
define
(
'Article'
,
{
'title'
:
DataTypes
.
STRING
})
,
Label
=
sequelize
.
define
(
'Label'
,
{
'text'
:
DataTypes
.
STRING
})
Article
.
hasMany
(
Label
)
Label
.
hasMany
(
Article
)
sequelize
.
sync
({
force
:
true
}).
success
(
function
()
{
Article
.
create
({
title
:
'foo'
}).
success
(
function
(
article
)
{
Label
.
create
({
text
:
'bar'
}).
success
(
function
(
label
)
{
sequelize
.
transaction
(
function
(
t
)
{
article
.
setLabels
([
label
],
{
transaction
:
t
}).
success
(
function
()
{
Article
.
all
({
transaction
:
t
}).
success
(
function
(
articles
)
{
articles
[
0
].
getLabels
().
success
(
function
(
labels
)
{
expect
(
labels
).
to
.
have
.
length
(
0
)
Article
.
all
({
transaction
:
t
}).
success
(
function
(
articles
)
{
articles
[
0
].
getLabels
({
transaction
:
t
}).
success
(
function
(
labels
)
{
expect
(
labels
).
to
.
have
.
length
(
1
)
t
.
rollback
().
success
(
function
()
{
done
()
})
})
})
})
})
})
})
})
})
})
})
})
it
(
"gets all associated objects when no options are passed"
,
function
(
done
)
{
this
.
User
.
find
({
where
:
{
username
:
'John'
}}).
success
(
function
(
john
)
{
john
.
getTasks
().
success
(
function
(
tasks
)
{
...
...
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