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 c53c2f2e
authored
Jan 08, 2014
by
Sascha Gehlich
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added transaction support for `.createAssociation`
1 parent
93353db2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
71 additions
and
5 deletions
lib/associations/has-many.js
test/associations/has-many.test.js
lib/associations/has-many.js
View file @
c53c2f2
var
Utils
=
require
(
"./../utils"
)
var
Utils
=
require
(
"./../utils"
)
,
DataTypes
=
require
(
'./../data-types'
)
,
DataTypes
=
require
(
'./../data-types'
)
,
Helpers
=
require
(
'./helpers'
)
,
Helpers
=
require
(
'./helpers'
)
,
_
=
require
(
'lodash'
)
,
_
=
require
(
'lodash'
)
,
Transaction
=
require
(
'../transaction'
)
var
HasManySingleLinked
=
require
(
"./has-many-single-linked"
)
var
HasManySingleLinked
=
require
(
"./has-many-single-linked"
)
,
HasManyDoubleLinked
=
require
(
"./has-many-double-linked"
)
,
HasManyDoubleLinked
=
require
(
"./has-many-double-linked"
)
...
@@ -329,13 +330,20 @@ module.exports = (function() {
...
@@ -329,13 +330,20 @@ module.exports = (function() {
obj
[
this
.
accessors
.
create
]
=
function
(
values
,
fieldsOrOptions
)
{
obj
[
this
.
accessors
.
create
]
=
function
(
values
,
fieldsOrOptions
)
{
var
instance
=
this
var
instance
=
this
,
options
=
{}
if
((
fieldsOrOptions
||
{}).
transaction
instanceof
Transaction
)
{
options
.
transaction
=
fieldsOrOptions
.
transaction
delete
fieldsOrOptions
.
transaction
}
return
new
Utils
.
CustomEventEmitter
(
function
(
emitter
)
{
return
new
Utils
.
CustomEventEmitter
(
function
(
emitter
)
{
// Create the related model instance
// Create the related model instance
self
.
target
self
.
target
.
create
(
values
,
fieldsOrOptions
)
.
create
(
values
,
fieldsOrOptions
)
.
proxy
(
emitter
,
{
events
:
[
'error'
,
'sql'
]
})
.
proxy
(
emitter
,
{
events
:
[
'error'
,
'sql'
]
})
.
success
(
function
(
newAssociatedObject
)
{
.
success
(
function
(
newAssociatedObject
)
{
instance
[
self
.
accessors
.
add
](
newAssociatedObject
)
instance
[
self
.
accessors
.
add
](
newAssociatedObject
,
options
)
.
proxy
(
emitter
)
.
proxy
(
emitter
)
})
})
}).
run
()
}).
run
()
...
...
test/associations/has-many.test.js
View file @
c53c2f2
...
@@ -352,6 +352,34 @@ describe(Support.getTestDialectTeaser("HasMany"), function() {
...
@@ -352,6 +352,34 @@ describe(Support.getTestDialectTeaser("HasMany"), function() {
})
})
})
})
})
})
it
(
'supports transactions'
,
function
(
done
)
{
var
self
=
this
Support
.
prepareTransactionTest
(
this
.
sequelize
,
function
(
sequelize
)
{
var
Article
=
sequelize
.
define
(
'Article'
,
{
'title'
:
DataTypes
.
STRING
})
,
Label
=
sequelize
.
define
(
'Label'
,
{
'text'
:
DataTypes
.
STRING
})
Article
.
hasMany
(
Label
)
Article
.
sync
({
force
:
true
}).
success
(
function
()
{
Label
.
sync
({
force
:
true
}).
success
(
function
()
{
Article
.
create
({
title
:
'foo'
}).
success
(
function
(
article
)
{
sequelize
.
transaction
(
function
(
t
)
{
article
.
createLabel
({
text
:
'bar'
},
{
transaction
:
t
}).
success
(
function
(
label
)
{
Label
.
findAll
({
where
:
{
ArticleId
:
article
.
id
}}).
success
(
function
(
labels
)
{
expect
(
labels
.
length
).
to
.
equal
(
0
)
Label
.
findAll
({
where
:
{
ArticleId
:
article
.
id
}},
{
transaction
:
t
}).
success
(
function
(
labels
)
{
expect
(
labels
.
length
).
to
.
equal
(
1
)
t
.
rollback
().
success
(
function
()
{
done
()
})
})
})
})
})
})
})
})
})
})
})
})
describe
(
"getting assocations with options"
,
function
()
{
describe
(
"getting assocations with options"
,
function
()
{
...
@@ -701,6 +729,36 @@ describe(Support.getTestDialectTeaser("HasMany"), function() {
...
@@ -701,6 +729,36 @@ describe(Support.getTestDialectTeaser("HasMany"), function() {
})
})
})
})
})
})
it
(
'supports transactions'
,
function
(
done
)
{
var
self
=
this
Support
.
prepareTransactionTest
(
this
.
sequelize
,
function
(
sequelize
)
{
var
User
=
sequelize
.
define
(
'User'
,
{
username
:
DataTypes
.
STRING
})
,
Task
=
sequelize
.
define
(
'Task'
,
{
title
:
DataTypes
.
STRING
})
User
.
hasMany
(
Task
)
Task
.
hasMany
(
User
)
User
.
sync
({
force
:
true
}).
success
(
function
()
{
Task
.
sync
({
force
:
true
}).
success
(
function
()
{
Task
.
create
({
title
:
'task'
}).
success
(
function
(
task
)
{
sequelize
.
transaction
(
function
(
t
)
{
task
.
createUser
({
username
:
'foo'
},
{
transaction
:
t
}).
success
(
function
()
{
task
.
getUsers
().
success
(
function
(
users
)
{
expect
(
users
).
to
.
have
.
length
(
0
)
task
.
getUsers
({
transaction
:
t
}).
success
(
function
(
users
)
{
expect
(
users
).
to
.
have
.
length
(
1
)
t
.
rollback
().
success
(
function
()
{
done
()
})
})
})
})
})
})
})
})
})
})
})
})
describe
(
'addAssociations'
,
function
()
{
describe
(
'addAssociations'
,
function
()
{
...
...
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