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 077242c0
authored
Jul 03, 2012
by
sdepold
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
clearing associations for many to many is working
1 parent
f0fb303a
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
37 additions
and
31 deletions
lib/associations/has-many-double-linked.js
lib/associations/has-many.js
spec/associations/has-many.spec.js
lib/associations/has-many-double-linked.js
View file @
077242c
...
@@ -28,12 +28,13 @@ module.exports = (function() {
...
@@ -28,12 +28,13 @@ module.exports = (function() {
return
customEventEmitter
.
run
()
return
customEventEmitter
.
run
()
}
}
HasManyDoubleLinked
.
prototype
.
injectSetter
=
function
(
emitter
,
oldAssociations
,
newAssociations
)
{
HasManyDoubleLinked
.
prototype
.
injectSetter
=
function
(
emitter
Proxy
,
oldAssociations
,
newAssociations
)
{
var
self
=
this
var
self
=
this
destroyObsoleteAssociations
.
call
(
this
,
oldAssociations
,
newAssociations
)
destroyObsoleteAssociations
.
error
(
function
(
err
)
{
emitter
.
emit
(
'error'
,
err
)
})
.
call
(
this
,
oldAssociations
,
newAssociations
)
.
on
(
'sql'
,
function
(
sql
)
{
emitter
.
emit
(
'sql'
,
sql
)
})
.
on
(
'sql'
,
function
(
sql
)
{
emitterProxy
.
emit
(
'sql'
,
sql
)
})
.
error
(
function
(
err
)
{
emitterProxy
.
emit
(
'error'
,
err
)
})
.
success
(
function
()
{
.
success
(
function
()
{
var
chainer
=
new
Utils
.
QueryChainer
var
chainer
=
new
Utils
.
QueryChainer
,
association
=
self
.
__factory
.
target
.
associations
[
self
.
__factory
.
associationAccessor
]
,
association
=
self
.
__factory
.
target
.
associations
[
self
.
__factory
.
associationAccessor
]
...
@@ -50,9 +51,9 @@ module.exports = (function() {
...
@@ -50,9 +51,9 @@ module.exports = (function() {
chainer
chainer
.
run
()
.
run
()
.
success
(
function
()
{
emitter
.
emit
(
'success'
,
newAssociations
)
})
.
success
(
function
()
{
emitter
Proxy
.
emit
(
'success'
,
newAssociations
)
})
.
error
(
function
(
err
)
{
emitter
.
emit
(
'error'
,
err
)
})
.
error
(
function
(
err
)
{
emitter
Proxy
.
emit
(
'error'
,
err
)
})
.
on
(
'sql'
,
function
(
sql
)
{
emitter
.
emit
(
'sql'
,
sql
)
})
.
on
(
'sql'
,
function
(
sql
)
{
emitter
Proxy
.
emit
(
'sql'
,
sql
)
})
})
})
}
}
...
@@ -66,7 +67,7 @@ module.exports = (function() {
...
@@ -66,7 +67,7 @@ module.exports = (function() {
var
foreignIdentifier
=
self
.
__factory
.
target
.
associations
[
self
.
__factory
.
associationAccessor
].
identifier
var
foreignIdentifier
=
self
.
__factory
.
target
.
associations
[
self
.
__factory
.
associationAccessor
].
identifier
var
obsoleteAssociations
=
oldAssociations
.
filter
(
function
(
obj
)
{
return
!
obj
.
equalsOneOf
(
newAssociations
)
})
var
obsoleteAssociations
=
oldAssociations
.
filter
(
function
(
obj
)
{
return
!
obj
.
equalsOneOf
(
newAssociations
)
})
if
(
obsoleteAssociations
.
length
==
0
)
{
if
(
obsoleteAssociations
.
length
==
=
0
)
{
return
emitter
.
emit
(
'success'
,
null
)
return
emitter
.
emit
(
'success'
,
null
)
}
}
...
@@ -74,14 +75,21 @@ module.exports = (function() {
...
@@ -74,14 +75,21 @@ module.exports = (function() {
var
where
=
{}
var
where
=
{}
,
primaryKeys
=
Utils
.
_
.
keys
(
self
.
__factory
.
connectorDAO
.
rawAttributes
)
,
primaryKeys
=
Utils
.
_
.
keys
(
self
.
__factory
.
connectorDAO
.
rawAttributes
)
,
foreignKey
=
primaryKeys
.
filter
(
function
(
pk
)
{
return
pk
!=
self
.
__factory
.
identifier
})[
0
]
,
foreignKey
=
primaryKeys
.
filter
(
function
(
pk
)
{
return
pk
!=
self
.
__factory
.
identifier
})[
0
]
,
notFoundEmitters
=
[]
where
[
self
.
__factory
.
identifier
]
=
self
.
instance
.
id
where
[
self
.
__factory
.
identifier
]
=
self
.
instance
.
id
where
[
foreignKey
]
=
associatedObject
.
id
where
[
foreignKey
]
=
associatedObject
.
id
self
.
__factory
.
connectorDAO
.
find
({
where
:
where
}).
success
(
function
(
connector
)
{
self
.
__factory
.
connectorDAO
.
find
({
where
:
where
})
.
success
(
function
(
connector
)
{
if
(
connector
===
null
)
{
notFoundEmitters
.
push
(
null
)
}
else
{
chainer
.
add
(
connector
.
destroy
())
chainer
.
add
(
connector
.
destroy
())
}
if
(
chainer
.
emitters
.
length
==
obsoleteAssociations
.
length
)
{
if
((
chainer
.
emitters
.
length
+
notFoundEmitters
.
length
)
=
==
obsoleteAssociations
.
length
)
{
// found all obsolete connectors and will delete them now
// found all obsolete connectors and will delete them now
chainer
chainer
.
run
()
.
run
()
...
@@ -90,6 +98,8 @@ module.exports = (function() {
...
@@ -90,6 +98,8 @@ module.exports = (function() {
.
on
(
'sql'
,
function
(
sql
)
{
emitter
.
emit
(
'sql'
,
sql
)
})
.
on
(
'sql'
,
function
(
sql
)
{
emitter
.
emit
(
'sql'
,
sql
)
})
}
}
})
})
.
error
(
function
(
err
)
{
emitter
.
emit
(
'error'
,
err
)
})
.
on
(
'sql'
,
function
(
sql
)
{
emitter
.
emit
(
'sql'
,
sql
)
})
})
})
}).
run
()
}).
run
()
}
}
...
...
lib/associations/has-many.js
View file @
077242c
...
@@ -121,16 +121,26 @@ module.exports = (function() {
...
@@ -121,16 +121,26 @@ module.exports = (function() {
var
self
=
this
var
self
=
this
obj
[
this
.
accessors
.
set
]
=
function
(
newAssociatedObjects
)
{
obj
[
this
.
accessors
.
set
]
=
function
(
newAssociatedObjects
)
{
if
(
newAssociatedObjects
===
null
)
{
newAssociatedObjects
=
[]
}
var
instance
=
this
var
instance
=
this
// define the returned customEventEmitter, which will emit the success event once everything is done
// define the returned customEventEmitter, which will emit the success event once everything is done
var
customEventEmitter
=
new
Utils
.
CustomEventEmitter
(
function
()
{
return
new
Utils
.
CustomEventEmitter
(
function
(
emitter
)
{
instance
[
self
.
accessors
.
get
]().
success
(
function
(
oldAssociatedObjects
)
{
instance
[
self
.
accessors
.
get
]()
.
success
(
function
(
oldAssociatedObjects
)
{
var
Class
=
self
.
connectorDAO
?
HasManyMultiLinked
:
HasManySingleLinked
var
Class
=
self
.
connectorDAO
?
HasManyMultiLinked
:
HasManySingleLinked
new
Class
(
self
,
instance
).
injectSetter
(
customEventE
mitter
,
oldAssociatedObjects
,
newAssociatedObjects
)
new
Class
(
self
,
instance
).
injectSetter
(
e
mitter
,
oldAssociatedObjects
,
newAssociatedObjects
)
})
})
.
error
(
function
(
err
)
{
emitter
.
emit
(
'error'
,
err
)
})
})
return
customEventEmitter
.
run
()
.
on
(
'sql'
,
function
(
sql
)
{
emitter
.
emit
(
'sql'
,
sql
)
})
}).
run
()
}
}
obj
[
this
.
accessors
.
add
]
=
function
(
newAssociatedObject
)
{
obj
[
this
.
accessors
.
add
]
=
function
(
newAssociatedObject
)
{
...
...
spec/associations/has-many.spec.js
View file @
077242c
...
@@ -146,6 +146,8 @@ describe('HasMany', function() {
...
@@ -146,6 +146,8 @@ describe('HasMany', function() {
})
})
it
(
"adds three items to the query chainer when calling sync"
,
function
()
{
it
(
"adds three items to the query chainer when calling sync"
,
function
()
{
sequelize
.
daoFactoryManager
.
daos
=
[]
var
User
=
sequelize
.
define
(
'User'
,
{
username
:
Sequelize
.
STRING
})
var
User
=
sequelize
.
define
(
'User'
,
{
username
:
Sequelize
.
STRING
})
,
Task
=
sequelize
.
define
(
'Task'
,
{
title
:
Sequelize
.
STRING
})
,
Task
=
sequelize
.
define
(
'Task'
,
{
title
:
Sequelize
.
STRING
})
...
@@ -160,43 +162,27 @@ describe('HasMany', function() {
...
@@ -160,43 +162,27 @@ describe('HasMany', function() {
expect
(
add
).
toHaveBeenCalledThrice
()
expect
(
add
).
toHaveBeenCalledThrice
()
})
})
it
(
"
=>
clears associations when passing null to the set-method"
,
function
(
done
)
{
it
(
"clears associations when passing null to the set-method"
,
function
(
done
)
{
var
User
=
sequelize
.
define
(
'User'
,
{
username
:
Sequelize
.
STRING
})
var
User
=
sequelize
.
define
(
'User'
,
{
username
:
Sequelize
.
STRING
})
,
Task
=
sequelize
.
define
(
'Task'
,
{
title
:
Sequelize
.
STRING
})
,
Task
=
sequelize
.
define
(
'Task'
,
{
title
:
Sequelize
.
STRING
})
User
.
hasMany
(
Task
)
User
.
hasMany
(
Task
)
Task
.
hasMany
(
User
)
Task
.
hasMany
(
User
)
console
.
log
(
sequelize
.
daoFactoryManager
.
daos
.
length
)
sequelize
.
sync
({
force
:
true
}).
success
(
function
()
{
sequelize
.
sync
({
force
:
true
}).
success
(
function
()
{
console
.
log
(
'sync ok'
)
User
.
create
({
username
:
'foo'
}).
success
(
function
(
user
)
{
User
.
create
({
username
:
'foo'
}).
success
(
function
(
user
)
{
console
.
log
(
'user was created'
)
Task
.
create
({
title
:
'task'
}).
success
(
function
(
task
)
{
Task
.
create
({
title
:
'task'
}).
success
(
function
(
task
)
{
console
.
log
(
'task was created'
)
task
.
setUsers
([
user
]).
success
(
function
()
{
task
.
setUsers
([
user
]).
success
(
function
()
{
console
.
log
(
'set users done'
)
task
.
getUsers
().
success
(
function
(
_users
)
{
task
.
getUsers
().
success
(
function
(
_users
)
{
console
.
log
(
_users
.
length
)
expect
(
_users
.
length
).
toEqual
(
1
)
expect
(
_users
.
length
).
toEqual
(
1
)
task
.
setUsers
(
null
).
success
(
function
()
{
task
.
setUsers
(
null
).
success
(
function
()
{
task
.
getUsers
().
success
(
function
(
_users
)
{
task
.
getUsers
().
success
(
function
(
_users
)
{
expect
(
_users
.
length
).
toEqual
(
0
)
expect
(
_users
.
length
).
toEqual
(
0
)
done
()
done
()
})
})
}).
error
(
function
(
err
)
{
console
.
log
(
err
)
})
}).
error
(
function
(
err
)
{
console
.
log
(
err
)
})
})
})
})
.
error
(
function
(
err
)
{
console
.
log
(
err
)
})
})
})
})
})
})
...
...
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