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 be4e58ea
authored
Apr 29, 2011
by
Sascha Depold
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactored hasMany; split into multiple files for single and double linked associations
1 parent
35f9f304
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
94 additions
and
61 deletions
lib/sequelize/associations/has-many-double-linked.js
lib/sequelize/associations/has-many-single-linked.js
lib/sequelize/associations/has-many.js
lib/sequelize/associations/has-many-double-linked.js
0 → 100644
View file @
be4e58e
var
Utils
=
require
(
'./../utils'
)
var
HasManyDoubleLinked
=
module
.
exports
=
function
(
definition
,
instance
)
{
this
.
definition
=
definition
this
.
instance
=
instance
}
HasManyDoubleLinked
.
prototype
.
injectGetter
=
function
()
{
var
self
=
this
var
customEventEmitter
=
new
Utils
.
CustomEventEmitter
(
function
()
{
var
where
=
{}
where
[
self
.
definition
.
identifier
]
=
self
.
instance
.
id
self
.
definition
.
connectorModel
.
findAll
({
where
:
where
}).
on
(
'success'
,
function
(
associatedObjects
)
{
customEventEmitter
.
emit
(
'success'
,
associatedObjects
)
})
})
return
customEventEmitter
.
run
()
}
HasManyDoubleLinked
.
prototype
.
injectSetter
=
function
(
emitter
,
oldAssociations
,
newAssociations
)
{
var
self
=
this
// destroy the old association objects
var
foreignIdentifier
=
self
.
definition
.
target
.
associations
[
self
.
definition
.
source
.
tableName
].
identifier
var
destroyChainer
=
new
Utils
.
QueryChainer
oldAssociations
.
forEach
(
function
(
associatedObject
)
{
destroyChainer
.
add
(
associatedObject
.
destroy
())
})
destroyChainer
.
run
()
.
on
(
'failure'
,
function
(
err
)
{
emitter
.
emit
(
'failure'
,
err
)
})
.
on
(
'success'
,
function
()
{
// create new one
var
createChainer
=
new
Utils
.
QueryChainer
newAssociations
.
forEach
(
function
(
associatedObject
)
{
var
attributes
=
{}
attributes
[
self
.
definition
.
identifier
]
=
self
.
instance
.
id
attributes
[
foreignIdentifier
]
=
associatedObject
.
id
createChainer
.
add
(
self
.
definition
.
connectorModel
.
create
(
attributes
))
})
createChainer
.
run
()
.
on
(
'success'
,
function
()
{
emitter
.
emit
(
'success'
,
null
)
})
.
on
(
'failure'
,
function
(
err
)
{
emitter
.
emit
(
'failure'
,
err
)
})
})
}
\ No newline at end of file
lib/sequelize/associations/has-many-single-linked.js
0 → 100644
View file @
be4e58e
var
Utils
=
require
(
'./../utils'
)
var
HasManySingleLinked
=
module
.
exports
=
function
(
definition
,
instance
)
{
this
.
definition
=
definition
this
.
instance
=
instance
}
HasManySingleLinked
.
prototype
.
injectGetter
=
function
()
{
var
where
=
{}
where
[
this
.
definition
.
identifier
]
=
this
.
instance
.
id
return
this
.
definition
.
target
.
findAll
({
where
:
where
})
}
HasManySingleLinked
.
prototype
.
injectSetter
=
function
(
emitter
,
oldAssociations
,
newAssociations
)
{
var
self
=
this
// clear the old associations
oldAssociations
.
forEach
(
function
(
associatedObject
)
{
associatedObject
[
self
.
definition
.
identifier
]
=
null
associatedObject
.
save
()
})
// set the new one
var
chainer
=
new
Utils
.
QueryChainer
newAssociations
.
forEach
(
function
(
associatedObject
)
{
associatedObject
[
self
.
definition
.
identifier
]
=
self
.
instance
.
id
chainer
.
add
(
associatedObject
.
save
())
})
chainer
.
run
()
.
on
(
'success'
,
function
()
{
emitter
.
emit
(
'success'
,
null
)
})
.
on
(
'failure'
,
function
()
{
emitter
.
emit
(
'failure'
,
null
)
})
}
\ No newline at end of file
lib/sequelize/associations/has-many.js
View file @
be4e58e
var
Utils
=
require
(
"./../utils"
)
,
DataTypes
=
require
(
'./../data-types'
)
var
HasManySingleLinked
=
require
(
"./has-many-single-linked"
)
,
HasManyMultiLinked
=
require
(
"./has-many-double-linked"
)
var
HasMany
=
module
.
exports
=
function
(
srcModel
,
targetModel
,
options
)
{
this
.
source
=
srcModel
this
.
target
=
targetModel
...
...
@@ -46,23 +49,8 @@ HasMany.prototype.injectGetter = function(obj) {
var
self
=
this
obj
[
this
.
accessors
.
get
]
=
function
()
{
var
instance
=
this
if
(
self
.
connectorModel
)
{
var
customEventEmitter
=
new
Utils
.
CustomEventEmitter
(
function
()
{
var
where
=
{}
where
[
self
.
identifier
]
=
instance
.
id
self
.
connectorModel
.
findAll
({
where
:
where
}).
on
(
'success'
,
function
(
associatedObjects
)
{
customEventEmitter
.
emit
(
'success'
,
associatedObjects
)
})
})
return
customEventEmitter
.
run
()
}
else
{
var
where
=
{}
where
[
self
.
identifier
]
=
this
.
id
return
self
.
target
.
findAll
({
where
:
where
})
}
var
Class
=
self
.
connectorModel
?
HasManyMultiLinked
:
HasManySingleLinked
return
new
Class
(
self
,
this
).
injectGetter
()
}
return
this
...
...
@@ -77,50 +65,8 @@ HasMany.prototype.injectSetter = function(obj) {
// define the returned customEventEmitter, which will emit the success event once everything is done
var
customEventEmitter
=
new
Utils
.
CustomEventEmitter
(
function
()
{
instance
[
self
.
accessors
.
get
]().
on
(
'success'
,
function
(
oldAssociatedObjects
)
{
if
(
self
.
connectorModel
)
{
var
foreignIdentifier
=
self
.
target
.
associations
[
self
.
source
.
tableName
].
identifier
var
destroyChainer
=
new
Utils
.
QueryChainer
// destroy the old association objects
oldAssociatedObjects
.
forEach
(
function
(
associatedObject
)
{
destroyChainer
.
add
(
associatedObject
.
destroy
())
})
destroyChainer
.
run
()
.
on
(
'failure'
,
function
(
err
)
{
customEventEmitter
.
emit
(
'failure'
,
err
)
})
.
on
(
'success'
,
function
()
{
// create new one
var
createChainer
=
new
Utils
.
QueryChainer
newAssociatedObjects
.
forEach
(
function
(
associatedObject
)
{
var
attributes
=
{}
attributes
[
self
.
identifier
]
=
instance
.
id
attributes
[
foreignIdentifier
]
=
associatedObject
.
id
createChainer
.
add
(
self
.
connectorModel
.
create
(
attributes
))
})
createChainer
.
run
()
.
on
(
'success'
,
function
()
{
customEventEmitter
.
emit
(
'success'
,
null
)
})
.
on
(
'failure'
,
function
(
err
)
{
customEventEmitter
.
emit
(
'failure'
,
err
)
})
})
}
else
{
// clear the old associations
oldAssociatedObjects
.
forEach
(
function
(
associatedObject
)
{
associatedObject
[
self
.
identifier
]
=
null
associatedObject
.
save
()
})
// set the new one
var
chainer
=
new
Utils
.
QueryChainer
newAssociatedObjects
.
forEach
(
function
(
associatedObject
)
{
associatedObject
[
self
.
identifier
]
=
instance
.
id
chainer
.
add
(
associatedObject
.
save
())
})
chainer
.
run
()
.
on
(
'success'
,
function
()
{
customEventEmitter
.
emit
(
'success'
,
null
)
})
.
on
(
'failure'
,
function
()
{
customEventEmitter
.
emit
(
'failure'
,
null
)
})
}
var
Class
=
self
.
connectorModel
?
HasManyMultiLinked
:
HasManySingleLinked
new
Class
(
self
,
instance
).
injectSetter
(
customEventEmitter
,
oldAssociatedObjects
,
newAssociatedObjects
)
})
})
return
customEventEmitter
.
run
()
...
...
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