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 27ba66e5
authored
Nov 17, 2013
by
Sascha Depold
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
transaction support for 1:N association
1 parent
6b0d90dd
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
58 additions
and
21 deletions
lib/associations/has-one.js
test/associations/has-one.test.js
lib/associations/has-one.js
View file @
27ba66e
...
...
@@ -77,31 +77,38 @@ module.exports = (function() {
HasOne
.
prototype
.
injectSetter
=
function
(
obj
)
{
var
self
=
this
obj
[
this
.
accessors
.
set
]
=
function
(
associatedObject
)
{
var
instance
=
this
obj
[
this
.
accessors
.
set
]
=
function
(
associatedObject
,
options
)
{
var
instance
=
this
,
instanceKeys
=
Object
.
keys
(
instance
.
daoFactory
.
primaryKeys
)
,
instanceKey
=
instanceKeys
.
length
===
1
?
instanceKeys
[
0
]
:
'id'
,
instanceKey
=
instanceKeys
.
length
===
1
?
instanceKeys
[
0
]
:
'id'
return
new
Utils
.
CustomEventEmitter
(
function
(
emitter
)
{
instance
[
self
.
accessors
.
get
]().
success
(
function
(
oldObj
)
{
if
(
oldObj
)
{
oldObj
[
self
.
identifier
]
=
null
oldObj
.
save
([
self
.
identifier
],
{
allowNull
:
[
self
.
identifier
]}).
success
(
function
()
{
if
(
associatedObject
)
{
associatedObject
[
self
.
identifier
]
=
instance
[
instanceKey
]
associatedObject
.
save
()
.
success
(
function
()
{
emitter
.
emit
(
'success'
,
associatedObject
)
})
.
error
(
function
(
err
)
{
emitter
.
emit
(
'error'
,
err
)
})
}
else
{
emitter
.
emit
(
'success'
,
null
)
}
})
oldObj
.
save
(
Utils
.
_
.
extend
(
options
,
{
fields
:
[
self
.
identifier
],
allowNull
:
[
self
.
identifier
]
})
)
.
success
(
function
()
{
if
(
associatedObject
)
{
associatedObject
[
self
.
identifier
]
=
instance
[
instanceKey
]
associatedObject
.
save
(
options
)
.
success
(
function
()
{
emitter
.
emit
(
'success'
,
associatedObject
)
})
.
error
(
function
(
err
)
{
emitter
.
emit
(
'error'
,
err
)
})
}
else
{
emitter
.
emit
(
'success'
,
null
)
}
})
}
else
{
if
(
associatedObject
)
{
associatedObject
[
self
.
identifier
]
=
instance
[
instanceKey
]
associatedObject
.
save
()
.
save
(
options
)
.
success
(
function
()
{
emitter
.
emit
(
'success'
,
associatedObject
)
})
.
error
(
function
(
err
)
{
emitter
.
emit
(
'error'
,
err
)
})
}
else
{
...
...
test/associations/has-one.test.js
View file @
27ba66e
...
...
@@ -21,6 +21,37 @@ describe(Support.getTestDialectTeaser("HasOne"), function() {
})
describe
(
'getAssocation'
,
function
()
{
it
(
'supports transactions'
,
function
(
done
)
{
Support
.
prepareTransactionTest
(
this
.
sequelize
,
function
(
sequelize
)
{
var
User
=
sequelize
.
define
(
'User'
,
{
username
:
Support
.
Sequelize
.
STRING
})
,
Group
=
sequelize
.
define
(
'Group'
,
{
name
:
Support
.
Sequelize
.
STRING
})
Group
.
hasOne
(
User
)
sequelize
.
sync
({
force
:
true
}).
success
(
function
()
{
User
.
create
({
username
:
'foo'
}).
success
(
function
(
user
)
{
Group
.
create
({
name
:
'bar'
}).
success
(
function
(
group
)
{
sequelize
.
transaction
(
function
(
t
)
{
group
.
setUser
(
user
,
{
transaction
:
t
}).
success
(
function
()
{
Group
.
all
().
success
(
function
(
groups
)
{
groups
[
0
].
getUser
().
success
(
function
(
associatedUser
)
{
expect
(
associatedUser
).
to
.
be
.
null
Group
.
all
({
transaction
:
t
}).
success
(
function
(
groups
)
{
groups
[
0
].
getUser
({
transaction
:
t
}).
success
(
function
(
associatedUser
)
{
expect
(
associatedUser
).
to
.
be
.
not
.
null
t
.
rollback
().
success
(
function
()
{
done
()
})
})
})
})
})
})
})
})
})
})
})
})
it
(
'should be able to handle a where object that\'s a first class citizen.'
,
function
(
done
)
{
var
User
=
this
.
sequelize
.
define
(
'UserXYZ'
,
{
username
:
Sequelize
.
STRING
})
,
Task
=
this
.
sequelize
.
define
(
'TaskXYZ'
,
{
title
:
Sequelize
.
STRING
,
status
:
Sequelize
.
STRING
})
...
...
@@ -244,15 +275,15 @@ describe(Support.getTestDialectTeaser("HasOne"), function() {
describe
(
"Association column"
,
function
()
{
it
(
'has correct type for non-id primary keys with non-integer type'
,
function
(
done
)
{
var
User
=
this
.
sequelize
.
define
(
'UserPKBT'
,
{
username
:
{
var
User
=
this
.
sequelize
.
define
(
'UserPKBT'
,
{
username
:
{
type
:
Sequelize
.
STRING
}
})
,
self
=
this
var
Group
=
this
.
sequelize
.
define
(
'GroupPKBT'
,
{
name
:
{
var
Group
=
this
.
sequelize
.
define
(
'GroupPKBT'
,
{
name
:
{
type
:
Sequelize
.
STRING
,
primaryKey
:
true
}
...
...
@@ -293,4 +324,4 @@ describe(Support.getTestDialectTeaser("HasOne"), function() {
})
})
})
\ No newline at end of file
})
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