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 9f99f42d
authored
Nov 17, 2013
by
Sascha Depold
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
transaction support for 1:N associations
1 parent
42ede1e9
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
77 additions
and
29 deletions
lib/associations/has-many-single-linked.js
lib/associations/has-many.js
test/associations/has-many.test.js
lib/associations/has-many-single-linked.js
View file @
9f99f42
var
Utils
=
require
(
'./../utils'
)
var
Utils
=
require
(
'./../utils'
)
,
Transaction
=
require
(
'./../transaction'
)
module
.
exports
=
(
function
()
{
module
.
exports
=
(
function
()
{
var
HasManySingleLinked
=
function
(
definition
,
instance
)
{
var
HasManySingleLinked
=
function
(
definition
,
instance
)
{
...
@@ -28,23 +29,29 @@ module.exports = (function() {
...
@@ -28,23 +29,29 @@ module.exports = (function() {
return
this
.
__factory
.
target
.
all
(
options
)
return
this
.
__factory
.
target
.
all
(
options
)
}
}
HasManySingleLinked
.
prototype
.
injectSetter
=
function
(
emitter
,
oldAssociations
,
newAssociations
)
{
HasManySingleLinked
.
prototype
.
injectSetter
=
function
(
emitter
,
oldAssociations
,
newAssociations
,
defaultAttributes
)
{
var
self
=
this
var
self
=
this
,
associationKeys
=
Object
.
keys
((
oldAssociations
[
0
]
||
newAssociations
[
0
]
||
{
daoFactory
:
{
primaryKeys
:
{}}}).
daoFactory
.
primaryKeys
||
{})
,
associationKeys
=
Object
.
keys
((
oldAssociations
[
0
]
||
newAssociations
[
0
]
||
{
daoFactory
:
{
primaryKeys
:
{}}}).
daoFactory
.
primaryKeys
||
{})
,
associationKey
=
associationKeys
.
length
===
1
?
associationKeys
[
0
]
:
'id'
,
associationKey
=
(
associationKeys
.
length
===
1
)
?
associationKeys
[
0
]
:
'id'
,
chainer
=
new
Utils
.
QueryChainer
()
,
chainer
=
new
Utils
.
QueryChainer
()
,
options
=
{}
,
obsoleteAssociations
=
oldAssociations
.
filter
(
function
(
old
)
{
,
obsoleteAssociations
=
oldAssociations
.
filter
(
function
(
old
)
{
return
!
Utils
.
_
.
find
(
newAssociations
,
function
(
obj
)
{
return
!
Utils
.
_
.
find
(
newAssociations
,
function
(
obj
)
{
return
obj
[
associationKey
]
===
old
[
associationKey
]
return
obj
[
associationKey
]
===
old
[
associationKey
]
})
})
})
})
,
unassociatedObjects
=
newAssociations
.
filter
(
function
(
obj
)
{
,
unassociatedObjects
=
newAssociations
.
filter
(
function
(
obj
)
{
return
!
Utils
.
_
.
find
(
oldAssociations
,
function
(
old
)
{
return
!
Utils
.
_
.
find
(
oldAssociations
,
function
(
old
)
{
return
obj
[
associationKey
]
===
old
[
associationKey
]
return
obj
[
associationKey
]
===
old
[
associationKey
]
})
})
})
})
,
update
,
update
if
((
defaultAttributes
||
{}).
transaction
instanceof
Transaction
)
{
options
.
transaction
=
defaultAttributes
.
transaction
delete
defaultAttributes
.
transaction
}
if
(
obsoleteAssociations
.
length
>
0
)
{
if
(
obsoleteAssociations
.
length
>
0
)
{
// clear the old associations
// clear the old associations
var
obsoleteIds
=
obsoleteAssociations
.
map
(
function
(
associatedObject
)
{
var
obsoleteIds
=
obsoleteAssociations
.
map
(
function
(
associatedObject
)
{
...
@@ -54,21 +61,26 @@ module.exports = (function() {
...
@@ -54,21 +61,26 @@ module.exports = (function() {
update
=
{}
update
=
{}
update
[
self
.
__factory
.
identifier
]
=
null
update
[
self
.
__factory
.
identifier
]
=
null
var
primaryKeys
=
Object
.
keys
(
this
.
__factory
.
target
.
primaryKeys
)
var
primaryKeys
=
Object
.
keys
(
this
.
__factory
.
target
.
primaryKeys
)
,
primaryKey
=
primaryKeys
.
length
===
1
?
primaryKeys
[
0
]
:
'id'
,
primaryKey
=
primaryKeys
.
length
===
1
?
primaryKeys
[
0
]
:
'id'
,
updateWhere
=
{}
,
updateWhere
=
{}
updateWhere
[
primaryKey
]
=
obsoleteIds
updateWhere
[
primaryKey
]
=
obsoleteIds
chainer
.
add
(
this
.
__factory
.
target
.
update
(
update
,
updateWhere
,
{
allowNull
:
[
self
.
__factory
.
identifier
]}))
chainer
.
add
(
this
.
__factory
.
target
.
update
(
update
,
updateWhere
,
Utils
.
_
.
extend
(
options
,
{
allowNull
:
[
self
.
__factory
.
identifier
]
})
))
}
}
if
(
unassociatedObjects
.
length
>
0
)
{
if
(
unassociatedObjects
.
length
>
0
)
{
// For the self.instance
// For the self.instance
var
pkeys
=
Object
.
keys
(
self
.
instance
.
daoFactory
.
primaryKeys
)
var
pkeys
=
Object
.
keys
(
self
.
instance
.
daoFactory
.
primaryKeys
)
,
pkey
=
pkeys
.
length
===
1
?
pkeys
[
0
]
:
'id'
,
pkey
=
pkeys
.
length
===
1
?
pkeys
[
0
]
:
'id'
// For chainer
// For chainer
,
primaryKeys
=
Object
.
keys
(
this
.
__factory
.
target
.
primaryKeys
)
,
primaryKeys
=
Object
.
keys
(
this
.
__factory
.
target
.
primaryKeys
)
,
primaryKey
=
primaryKeys
.
length
===
1
?
primaryKeys
[
0
]
:
'id'
,
primaryKey
=
primaryKeys
.
length
===
1
?
primaryKeys
[
0
]
:
'id'
,
updateWhere
=
{}
,
updateWhere
=
{}
// set the new associations
// set the new associations
...
@@ -77,10 +89,15 @@ module.exports = (function() {
...
@@ -77,10 +89,15 @@ module.exports = (function() {
return
associatedObject
[
associationKey
]
return
associatedObject
[
associationKey
]
})
})
update
=
{}
update
=
{}
update
[
self
.
__factory
.
identifier
]
=
(
newAssociations
.
length
<
1
?
null
:
self
.
instance
[
pkey
]
||
self
.
instance
.
id
)
update
[
self
.
__factory
.
identifier
]
=
(
newAssociations
.
length
<
1
?
null
:
self
.
instance
[
pkey
]
||
self
.
instance
.
id
)
updateWhere
[
primaryKey
]
=
unassociatedIds
updateWhere
[
primaryKey
]
=
unassociatedIds
chainer
.
add
(
this
.
__factory
.
target
.
update
(
update
,
updateWhere
,
{
allowNull
:
[
self
.
__factory
.
identifier
]}))
chainer
.
add
(
this
.
__factory
.
target
.
update
(
update
,
updateWhere
,
Utils
.
_
.
extend
(
options
,
{
allowNull
:
[
self
.
__factory
.
identifier
]
})
))
}
}
chainer
chainer
...
...
lib/associations/has-many.js
View file @
9f99f42
...
@@ -128,26 +128,26 @@ module.exports = (function() {
...
@@ -128,26 +128,26 @@ module.exports = (function() {
var
instance
=
this
;
var
instance
=
this
;
var
customEventEmitter
=
new
Utils
.
CustomEventEmitter
(
function
()
{
var
customEventEmitter
=
new
Utils
.
CustomEventEmitter
(
function
()
{
instance
[
self
.
accessors
.
get
]()
instance
[
self
.
accessors
.
get
]()
.
error
(
function
(
err
){
customEventEmitter
.
emit
(
'error'
,
err
)})
.
error
(
function
(
err
)
{
customEventEmitter
.
emit
(
'error'
,
err
)
})
.
success
(
function
(
associatedObjects
)
{
.
success
(
function
(
associatedObjects
)
{
customEventEmitter
.
emit
(
'success'
,
customEventEmitter
.
emit
(
'success'
,
Utils
.
_
.
all
(
objects
,
function
(
o
)
{
Utils
.
_
.
all
(
objects
,
function
(
o
)
{
return
Utils
.
_
.
any
(
associatedObjects
,
function
(
associatedObject
)
{
return
Utils
.
_
.
any
(
associatedObjects
,
function
(
associatedObject
)
{
return
Utils
.
_
.
all
(
associatedObject
.
identifiers
,
function
(
key
,
identifier
)
{
return
Utils
.
_
.
all
(
associatedObject
.
identifiers
,
function
(
key
,
identifier
)
{
return
o
[
identifier
]
==
associatedObject
[
identifier
];
return
o
[
identifier
]
==
associatedObject
[
identifier
];
});
});
})
})
})
})
)
)
})
})
})
})
return
customEventEmitter
.
run
()
return
customEventEmitter
.
run
()
}
}
obj
[
this
.
accessors
.
hasSingle
]
=
function
(
o
)
{
obj
[
this
.
accessors
.
hasSingle
]
=
function
(
o
,
options
)
{
var
instance
=
this
;
var
instance
=
this
var
customEventEmitter
=
new
Utils
.
CustomEventEmitter
(
function
()
{
var
customEventEmitter
=
new
Utils
.
CustomEventEmitter
(
function
()
{
instance
[
self
.
accessors
.
get
]()
instance
[
self
.
accessors
.
get
](
options
)
.
error
(
function
(
err
){
customEventEmitter
.
emit
(
'error'
,
err
)})
.
error
(
function
(
err
){
customEventEmitter
.
emit
(
'error'
,
err
)})
.
success
(
function
(
associatedObjects
)
{
.
success
(
function
(
associatedObjects
)
{
customEventEmitter
.
emit
(
'success'
,
customEventEmitter
.
emit
(
'success'
,
...
...
test/associations/has-many.test.js
View file @
9f99f42
...
@@ -41,6 +41,37 @@ describe(Support.getTestDialectTeaser("HasMany"), function() {
...
@@ -41,6 +41,37 @@ 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
)
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
].
hasLabel
(
label
).
success
(
function
(
hasLabel
)
{
expect
(
hasLabel
).
to
.
be
.
false
Article
.
all
({
transaction
:
t
}).
success
(
function
(
articles
)
{
articles
[
0
].
hasLabel
(
label
,
{
transaction
:
t
}).
success
(
function
(
hasLabel
)
{
expect
(
hasLabel
).
to
.
be
.
true
t
.
rollback
().
success
(
function
()
{
done
()
})
})
})
})
})
})
})
})
})
})
})
})
it
(
'does not have any labels assigned to it initially'
,
function
(
done
)
{
it
(
'does not have any labels assigned to it initially'
,
function
(
done
)
{
var
chainer
=
new
Sequelize
.
Utils
.
QueryChainer
([
var
chainer
=
new
Sequelize
.
Utils
.
QueryChainer
([
this
.
Article
.
create
({
title
:
'Articl2e'
}),
this
.
Article
.
create
({
title
:
'Articl2e'
}),
...
...
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