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 8aa5d609
authored
Aug 24, 2013
by
Jan Aagaard Meier
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Initial commit for join table model
1 parent
d633069a
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
90 additions
and
34 deletions
lib/associations/has-many-double-linked.js
lib/associations/has-many.js
lib/associations/has-many-double-linked.js
View file @
8aa5d60
...
@@ -18,14 +18,13 @@ module.exports = (function() {
...
@@ -18,14 +18,13 @@ module.exports = (function() {
where
[
self
.
__factory
.
connectorDAO
.
tableName
+
"."
+
self
.
__factory
.
identifier
]
=
self
.
instance
[
instancePrimaryKey
]
where
[
self
.
__factory
.
connectorDAO
.
tableName
+
"."
+
self
.
__factory
.
identifier
]
=
self
.
instance
[
instancePrimaryKey
]
var
primaryKeys
=
Object
.
keys
(
self
.
__factory
.
connectorDAO
.
rawAttribute
s
)
var
primaryKeys
=
Object
.
keys
(
self
.
__factory
.
connectorDAO
.
primaryKey
s
)
,
foreignKey
=
primaryKeys
.
filter
(
function
(
pk
)
{
return
pk
!=
self
.
__factory
.
identifier
})[
0
]
,
foreignKey
=
primaryKeys
.
filter
(
function
(
pk
)
{
return
pk
!=
self
.
__factory
.
identifier
})[
0
]
,
foreignPrimary
=
Object
.
keys
(
self
.
__factory
.
target
.
primaryKeys
)
,
foreignPrimary
=
Object
.
keys
(
self
.
__factory
.
target
.
primaryKeys
)
foreignPrimary
=
foreignPrimary
.
length
===
1
?
foreignPrimary
[
0
]
:
'id'
foreignPrimary
=
foreignPrimary
.
length
===
1
?
foreignPrimary
[
0
]
:
'id'
where
[
self
.
__factory
.
connectorDAO
.
tableName
+
"."
+
foreignKey
]
=
{
join
:
self
.
__factory
.
target
.
tableName
+
"."
+
foreignPrimary
}
where
[
self
.
__factory
.
connectorDAO
.
tableName
+
"."
+
foreignKey
]
=
{
join
:
self
.
__factory
.
target
.
tableName
+
"."
+
foreignPrimary
}
if
(
options
.
where
)
{
if
(
options
.
where
)
{
if
(
Array
.
isArray
(
options
.
where
))
{
if
(
Array
.
isArray
(
options
.
where
))
{
smart
=
Utils
.
smartWhere
([
where
,
options
.
where
],
self
.
__factory
.
target
.
daoFactoryManager
.
sequelize
.
options
.
dialect
)
smart
=
Utils
.
smartWhere
([
where
,
options
.
where
],
self
.
__factory
.
target
.
daoFactoryManager
.
sequelize
.
options
.
dialect
)
...
@@ -53,23 +52,40 @@ module.exports = (function() {
...
@@ -53,23 +52,40 @@ module.exports = (function() {
return
customEventEmitter
.
run
()
return
customEventEmitter
.
run
()
}
}
HasManyDoubleLinked
.
prototype
.
injectSetter
=
function
(
emitterProxy
,
oldAssociations
,
newAssociations
)
{
HasManyDoubleLinked
.
prototype
.
injectSetter
=
function
(
emitterProxy
,
oldAssociations
,
newAssociations
,
defaultAttributes
)
{
var
self
=
this
var
self
=
this
,
chainer
=
new
Utils
.
QueryChainer
()
,
chainer
=
new
Utils
.
QueryChainer
()
,
association
=
self
.
__factory
.
target
.
associations
[
self
.
__factory
.
associationAccessor
]
,
association
=
self
.
__factory
.
target
.
associations
[
self
.
__factory
.
associationAccessor
]
,
foreignIdentifier
=
association
.
isSelfAssociation
?
association
.
foreignIdentifier
:
association
.
identifier
;
,
foreignIdentifier
=
association
.
isSelfAssociation
?
association
.
foreignIdentifier
:
association
.
identifier
,
obsoleteAssociations
=
[]
,
changedAssociations
=
[]
,
unassociatedObjects
;
unassociatedObjects
=
newAssociations
.
filter
(
function
(
obj
)
{
return
!
Utils
.
_
.
find
(
oldAssociations
,
function
(
old
)
{
return
(
!!
obj
[
foreignIdentifier
]
&&
!!
old
[
foreignIdentifier
]
?
obj
[
foreignIdentifier
]
===
old
[
foreignIdentifier
]
:
obj
.
id
===
old
.
id
)
})
})
var
obsoleteAssociations
=
oldAssociations
.
filter
(
function
(
old
)
{
oldAssociations
.
forEach
(
function
(
old
)
{
// Return only those old associations that are not found in new
var
newObj
=
Utils
.
_
.
find
(
newAssociations
,
function
(
obj
)
{
return
!
Utils
.
_
.
find
(
newAssociations
,
function
(
obj
)
{
return
(
!!
obj
[
foreignIdentifier
]
&&
!!
old
[
foreignIdentifier
]
?
obj
[
foreignIdentifier
]
===
old
[
foreignIdentifier
]
:
obj
.
id
===
old
.
id
)
return
(
!!
obj
[
foreignIdentifier
]
&&
!!
old
[
foreignIdentifier
]
?
obj
[
foreignIdentifier
]
===
old
[
foreignIdentifier
]
:
obj
.
id
===
old
.
id
)
})
})
})
if
(
!
newObj
)
{
,
unassociatedObjects
=
newAssociations
.
filter
(
function
(
obj
)
{
obsoleteAssociations
.
push
(
obj
)
return
!
Utils
.
_
.
find
(
oldAssociations
,
function
(
old
)
{
}
else
if
(
association
.
customJoinTableModel
)
{
return
(
!!
obj
[
foreignIdentifier
]
&&
!!
old
[
foreignIdentifier
]
?
obj
[
foreignIdentifier
]
===
old
[
foreignIdentifier
]
:
obj
.
id
===
old
.
id
)
var
changedAssociation
=
{
})
where
:
{},
})
attributes
:
Utils
.
_
.
defaults
({},
newObj
[
self
.
__factory
.
connectorDAO
.
name
],
defaultAttributes
)
}
changedAssociation
.
where
[
self
.
__factory
.
identifier
]
=
self
.
instance
[
self
.
__factory
.
identifier
]
||
self
.
instance
.
id
changedAssociation
.
where
[
foreignIdentifier
]
=
newObj
[
foreignIdentifier
]
||
newObj
.
id
changedAssociations
.
push
(
changedAssociation
)
}
})
if
(
obsoleteAssociations
.
length
>
0
)
{
if
(
obsoleteAssociations
.
length
>
0
)
{
var
foreignIds
=
obsoleteAssociations
.
map
(
function
(
associatedObject
)
{
var
foreignIds
=
obsoleteAssociations
.
map
(
function
(
associatedObject
)
{
...
@@ -91,12 +107,22 @@ module.exports = (function() {
...
@@ -91,12 +107,22 @@ module.exports = (function() {
attributes
[
self
.
__factory
.
identifier
]
=
self
.
instance
[
self
.
__factory
.
identifier
]
||
self
.
instance
.
id
attributes
[
self
.
__factory
.
identifier
]
=
self
.
instance
[
self
.
__factory
.
identifier
]
||
self
.
instance
.
id
attributes
[
foreignIdentifier
]
=
unassociatedObject
[
foreignIdentifier
]
||
unassociatedObject
.
id
attributes
[
foreignIdentifier
]
=
unassociatedObject
[
foreignIdentifier
]
||
unassociatedObject
.
id
if
(
association
.
customJoinTableModel
)
{
attributes
=
Utils
.
_
.
defaults
(
attributes
,
unassociatedObject
[
association
.
connectorDAO
.
name
],
defaultAttributes
)
}
return
attributes
return
attributes
})
})
chainer
.
add
(
self
.
__factory
.
connectorDAO
.
bulkCreate
(
bulk
))
chainer
.
add
(
self
.
__factory
.
connectorDAO
.
bulkCreate
(
bulk
))
}
}
if
(
changedAssociations
.
length
>
0
)
{
changedAssociations
.
forEach
(
function
(
assoc
)
{
chainer
.
add
(
self
.
__factory
.
connectorDAO
.
update
(
assoc
.
attributes
,
assoc
.
where
))
})
}
chainer
chainer
.
run
()
.
run
()
.
success
(
function
()
{
emitterProxy
.
emit
(
'success'
,
newAssociations
)
})
.
success
(
function
()
{
emitterProxy
.
emit
(
'success'
,
newAssociations
)
})
...
@@ -104,7 +130,7 @@ module.exports = (function() {
...
@@ -104,7 +130,7 @@ module.exports = (function() {
.
on
(
'sql'
,
function
(
sql
)
{
emitterProxy
.
emit
(
'sql'
,
sql
)
})
.
on
(
'sql'
,
function
(
sql
)
{
emitterProxy
.
emit
(
'sql'
,
sql
)
})
}
}
HasManyDoubleLinked
.
prototype
.
injectAdder
=
function
(
emitterProxy
,
newAssociation
)
{
HasManyDoubleLinked
.
prototype
.
injectAdder
=
function
(
emitterProxy
,
newAssociation
,
additionalAttributes
,
exists
)
{
var
attributes
=
{}
var
attributes
=
{}
,
association
=
this
.
__factory
.
target
.
associations
[
this
.
__factory
.
associationAccessor
]
,
association
=
this
.
__factory
.
target
.
associations
[
this
.
__factory
.
associationAccessor
]
,
foreignIdentifier
=
association
.
isSelfAssociation
?
association
.
foreignIdentifier
:
association
.
identifier
;
,
foreignIdentifier
=
association
.
isSelfAssociation
?
association
.
foreignIdentifier
:
association
.
identifier
;
...
@@ -112,10 +138,21 @@ module.exports = (function() {
...
@@ -112,10 +138,21 @@ module.exports = (function() {
attributes
[
this
.
__factory
.
identifier
]
=
this
.
instance
[
this
.
__factory
.
identifier
]
||
this
.
instance
.
id
attributes
[
this
.
__factory
.
identifier
]
=
this
.
instance
[
this
.
__factory
.
identifier
]
||
this
.
instance
.
id
attributes
[
foreignIdentifier
]
=
newAssociation
[
foreignIdentifier
]
||
newAssociation
.
id
attributes
[
foreignIdentifier
]
=
newAssociation
[
foreignIdentifier
]
||
newAssociation
.
id
this
.
__factory
.
connectorDAO
.
create
(
attributes
)
if
(
exists
)
{
// implies customJoinTableModel === true
.
success
(
function
()
{
emitterProxy
.
emit
(
'success'
,
newAssociation
)
})
var
where
=
attributes
.
error
(
function
(
err
)
{
emitterProxy
.
emit
(
'error'
,
err
)
})
attributes
=
Utils
.
_
.
defaults
({},
newAssociation
[
association
.
connectorDAO
.
name
],
additionalAttributes
)
.
on
(
'sql'
,
function
(
sql
)
{
emitterProxy
.
emit
(
'sql'
,
sql
)
})
association
.
connectorDAO
.
update
(
attributes
,
where
).
proxy
(
emitterProxy
)
}
else
{
if
(
association
.
customJoinTableModel
===
true
)
{
attributes
=
Utils
.
_
.
defaults
(
attributes
,
newAssociation
[
association
.
connectorDAO
.
name
],
additionalAttributes
)
}
this
.
__factory
.
connectorDAO
.
create
(
attributes
)
.
success
(
function
()
{
emitterProxy
.
emit
(
'success'
,
newAssociation
)
})
.
error
(
function
(
err
)
{
emitterProxy
.
emit
(
'error'
,
err
)
})
.
on
(
'sql'
,
function
(
sql
)
{
emitterProxy
.
emit
(
'sql'
,
sql
)
})
}
}
}
return
HasManyDoubleLinked
return
HasManyDoubleLinked
...
...
lib/associations/has-many.js
View file @
8aa5d60
...
@@ -13,13 +13,20 @@ module.exports = (function() {
...
@@ -13,13 +13,20 @@ module.exports = (function() {
this
.
options
=
options
this
.
options
=
options
this
.
useJunctionTable
=
this
.
options
.
useJunctionTable
===
undefined
?
true
:
this
.
options
.
useJunctionTable
this
.
useJunctionTable
=
this
.
options
.
useJunctionTable
===
undefined
?
true
:
this
.
options
.
useJunctionTable
this
.
isSelfAssociation
=
(
this
.
source
.
tableName
===
this
.
target
.
tableName
)
this
.
isSelfAssociation
=
(
this
.
source
.
tableName
===
this
.
target
.
tableName
)
this
.
customJoinTableModel
=
!!
this
.
options
.
joinTableModel
var
combinedTableName
=
Utils
.
combineTableNames
(
var
combinedTableName
;
this
.
source
.
tableName
,
if
(
this
.
customJoinTableModel
)
{
this
.
isSelfAssociation
?
(
this
.
options
.
as
||
this
.
target
.
tableName
)
:
this
.
target
.
tableName
combinedTableName
=
this
.
options
.
joinTableModel
.
tableName
)
}
else
if
(
this
.
options
.
joinTableName
)
{
this
.
associationAccessor
=
this
.
combinedName
=
(
this
.
options
.
joinTableName
||
combinedTableName
)
combinedTableName
=
this
.
options
.
joinTableName
this
.
options
.
tableName
=
this
.
combinedName
}
else
{
Utils
.
combineTableNames
(
this
.
source
.
tableName
,
this
.
isSelfAssociation
?
(
this
.
options
.
as
||
this
.
target
.
tableName
)
:
this
.
target
.
tableName
)
}
this
.
options
.
tableName
=
this
.
associationAccessor
=
this
.
combinedName
=
combinedTableName
var
as
=
(
this
.
options
.
as
||
Utils
.
pluralize
(
this
.
target
.
tableName
,
this
.
target
.
options
.
language
))
var
as
=
(
this
.
options
.
as
||
Utils
.
pluralize
(
this
.
target
.
tableName
,
this
.
target
.
options
.
language
))
...
@@ -58,7 +65,17 @@ module.exports = (function() {
...
@@ -58,7 +65,17 @@ module.exports = (function() {
combinedTableAttributes
[
this
.
identifier
]
=
{
type
:
keyType
,
primaryKey
:
true
}
combinedTableAttributes
[
this
.
identifier
]
=
{
type
:
keyType
,
primaryKey
:
true
}
combinedTableAttributes
[
this
.
foreignIdentifier
]
=
{
type
:
keyType
,
primaryKey
:
true
}
combinedTableAttributes
[
this
.
foreignIdentifier
]
=
{
type
:
keyType
,
primaryKey
:
true
}
this
.
connectorDAO
=
this
.
source
.
daoFactoryManager
.
sequelize
.
define
(
this
.
combinedName
,
combinedTableAttributes
,
this
.
options
)
if
(
this
.
customJoinTableModel
===
true
)
{
this
.
connectorDAO
=
this
.
options
.
joinTableModel
this
.
connectorDAO
.
rawAttributes
=
Utils
.
_
.
merge
(
this
.
connectorDAO
.
rawAttributes
,
combinedTableAttributes
)
// FIXME - Should we do this? Normally join tables does not have an id, and it is not needed,
// but users might wonder what hapened if they defined the ID themselves
delete
this
.
connectorDAO
.
rawAttributes
.
id
this
.
connectorDAO
.
init
(
this
.
connectorDAO
.
daoFactoryManager
)
}
else
{
this
.
connectorDAO
=
this
.
source
.
daoFactoryManager
.
sequelize
.
define
(
this
.
combinedName
,
combinedTableAttributes
,
this
.
options
)
}
if
(
!
this
.
isSelfAssociation
)
{
if
(
!
this
.
isSelfAssociation
)
{
this
.
target
.
associations
[
this
.
associationAccessor
].
connectorDAO
=
this
.
connectorDAO
this
.
target
.
associations
[
this
.
associationAccessor
].
connectorDAO
=
this
.
connectorDAO
...
@@ -89,7 +106,7 @@ module.exports = (function() {
...
@@ -89,7 +106,7 @@ module.exports = (function() {
return
new
Class
(
self
,
this
).
injectGetter
(
options
)
return
new
Class
(
self
,
this
).
injectGetter
(
options
)
}
}
obj
[
this
.
accessors
.
hasAll
]
=
function
(
objects
)
{
obj
[
this
.
accessors
.
hasAll
]
=
function
(
objects
)
{
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
]()
...
@@ -132,7 +149,7 @@ module.exports = (function() {
...
@@ -132,7 +149,7 @@ module.exports = (function() {
HasMany
.
prototype
.
injectSetter
=
function
(
obj
)
{
HasMany
.
prototype
.
injectSetter
=
function
(
obj
)
{
var
self
=
this
var
self
=
this
obj
[
this
.
accessors
.
set
]
=
function
(
newAssociatedObjects
)
{
obj
[
this
.
accessors
.
set
]
=
function
(
newAssociatedObjects
,
defaultAttributes
)
{
if
(
newAssociatedObjects
===
null
)
{
if
(
newAssociatedObjects
===
null
)
{
newAssociatedObjects
=
[]
newAssociatedObjects
=
[]
}
}
...
@@ -144,7 +161,7 @@ module.exports = (function() {
...
@@ -144,7 +161,7 @@ module.exports = (function() {
instance
[
self
.
accessors
.
get
]()
instance
[
self
.
accessors
.
get
]()
.
success
(
function
(
oldAssociatedObjects
)
{
.
success
(
function
(
oldAssociatedObjects
)
{
var
Class
=
self
.
connectorDAO
?
HasManyMultiLinked
:
HasManySingleLinked
var
Class
=
self
.
connectorDAO
?
HasManyMultiLinked
:
HasManySingleLinked
new
Class
(
self
,
instance
).
injectSetter
(
emitter
,
oldAssociatedObjects
,
newAssociatedObjects
)
new
Class
(
self
,
instance
).
injectSetter
(
emitter
,
oldAssociatedObjects
,
newAssociatedObjects
,
defaultAttributes
)
})
})
.
error
(
function
(
err
)
{
.
error
(
function
(
err
)
{
emitter
.
emit
(
'error'
,
err
)
emitter
.
emit
(
'error'
,
err
)
...
@@ -155,20 +172,21 @@ module.exports = (function() {
...
@@ -155,20 +172,21 @@ module.exports = (function() {
}).
run
()
}).
run
()
}
}
obj
[
this
.
accessors
.
add
]
=
function
(
newAssociatedObject
)
{
obj
[
this
.
accessors
.
add
]
=
function
(
newAssociatedObject
,
additionalAttributes
)
{
var
instance
=
this
var
instance
=
this
,
primaryKeys
=
Object
.
keys
(
newAssociatedObject
.
daoFactory
.
primaryKeys
||
{})
,
primaryKeys
=
Object
.
keys
(
newAssociatedObject
.
daoFactory
.
primaryKeys
||
{})
,
primaryKey
=
primaryKeys
.
length
===
1
?
primaryKeys
[
0
]
:
'id'
,
primaryKey
=
primaryKeys
.
length
===
1
?
primaryKeys
[
0
]
:
'id'
,
where
=
{}
,
where
=
{}
where
[
newAssociatedObject
.
daoFactory
.
tableName
+
'.'
+
primaryKey
]
=
newAssociatedObject
[
primaryKey
]
where
[
newAssociatedObject
.
daoFactory
.
tableName
+
'.'
+
primaryKey
]
=
newAssociatedObject
[
primaryKey
]
return
new
Utils
.
CustomEventEmitter
(
function
(
emitter
)
{
return
new
Utils
.
CustomEventEmitter
(
function
(
emitter
)
{
instance
[
self
.
accessors
.
get
]({
where
:
where
})
instance
[
self
.
accessors
.
get
]({
where
:
where
})
.
error
(
function
(
err
){
emitter
.
emit
(
'error'
,
err
)})
.
error
(
function
(
err
){
emitter
.
emit
(
'error'
,
err
)})
.
success
(
function
(
currentAssociatedObjects
)
{
.
success
(
function
(
currentAssociatedObjects
)
{
if
(
currentAssociatedObjects
.
length
===
0
)
{
if
(
currentAssociatedObjects
.
length
===
0
||
self
.
customJoinTableModel
===
true
)
{
var
Class
=
self
.
connectorDAO
?
HasManyMultiLinked
:
HasManySingleLinked
var
Class
=
self
.
connectorDAO
?
HasManyMultiLinked
:
HasManySingleLinked
new
Class
(
self
,
instance
).
injectAdder
(
emitter
,
newAssociatedObject
)
new
Class
(
self
,
instance
).
injectAdder
(
emitter
,
newAssociatedObject
,
additionalAttributes
,
!!
currentAssociatedObjects
.
length
)
}
else
{
}
else
{
emitter
.
emit
(
'success'
,
newAssociatedObject
);
emitter
.
emit
(
'success'
,
newAssociatedObject
);
}
}
...
@@ -183,8 +201,9 @@ module.exports = (function() {
...
@@ -183,8 +201,9 @@ module.exports = (function() {
var
newAssociations
=
[]
var
newAssociations
=
[]
currentAssociatedObjects
.
forEach
(
function
(
association
)
{
currentAssociatedObjects
.
forEach
(
function
(
association
)
{
if
(
!
Utils
.
_
.
isEqual
(
oldAssociatedObject
.
identifiers
,
association
.
identifiers
))
if
(
!
Utils
.
_
.
isEqual
(
oldAssociatedObject
.
identifiers
,
association
.
identifiers
))
{
newAssociations
.
push
(
association
)
newAssociations
.
push
(
association
)
}
})
})
instance
[
self
.
accessors
.
set
](
newAssociations
)
instance
[
self
.
accessors
.
set
](
newAssociations
)
...
...
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