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 96976330
authored
Sep 08, 2010
by
Sascha Depold
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
a lot of stuff in order to get the new assoc system run
1 parent
3a6b4f55
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
53 additions
and
32 deletions
lib/sequelize/Factory.js
lib/sequelize/SequelizeTable.js
lib/sequelize/Factory.js
View file @
9697633
...
@@ -2,12 +2,17 @@ module.exports.Factory = function(Sequelize, sequelize) {
...
@@ -2,12 +2,17 @@ module.exports.Factory = function(Sequelize, sequelize) {
return
{
return
{
createManyToManyGetter
:
function
(
table1
,
table2
,
assocName
,
backAssocName
)
{
createManyToManyGetter
:
function
(
table1
,
table2
,
assocName
,
backAssocName
)
{
return
function
(
callback
)
{
return
function
(
callback
)
{
var
Association
=
sequelize
.
tables
[
Sequelize
.
Helper
.
SQL
.
manyToManyTableName
(
assocName
,
backAssocName
)].
klass
var
Association
=
sequelize
.
tables
[
Sequelize
.
Helper
.
SQL
.
manyToManyTableName
(
assocName
,
backAssocName
)].
klass
,
var
whereConditions
=
[
table1
.
identifier
,
this
.
id
].
join
(
"="
)
assocNameAsTableIdentifier
=
Sequelize
.
Helper
.
SQL
.
asTableIdentifier
(
backAssocName
),
whereConditions
=
[
assocNameAsTableIdentifier
,
this
.
id
].
join
(
"="
)
Association
.
findAll
({
where
:
whereConditions
},
function
(
result
)
{
Association
.
findAll
({
where
:
whereConditions
},
function
(
result
)
{
if
(
result
.
length
>
0
)
{
if
(
result
.
length
>
0
)
{
var
ids
=
[]
var
ids
=
Sequelize
.
Helper
.
Array
.
map
(
result
,
function
(
resultSet
)
{
result
.
forEach
(
function
(
resultSet
)
{
ids
.
push
(
resultSet
.
id
)
})
return
resultSet
[
Sequelize
.
Helper
.
SQL
.
asTableIdentifier
(
backAssocName
)]
})
Sequelize
.
Helper
.
log
(
ids
)
Sequelize
.
Helper
.
log
(
table2
.
tableName
)
table2
.
findAll
({
where
:
"id IN ("
+
ids
.
join
(
","
)
+
")"
},
callback
)
table2
.
findAll
({
where
:
"id IN ("
+
ids
.
join
(
","
)
+
")"
},
callback
)
}
else
{
}
else
{
if
(
callback
)
callback
([])
if
(
callback
)
callback
([])
...
@@ -23,38 +28,49 @@ module.exports.Factory = function(Sequelize, sequelize) {
...
@@ -23,38 +28,49 @@ module.exports.Factory = function(Sequelize, sequelize) {
objectIds
=
Sequelize
.
Helper
.
Array
.
map
(
objects
,
function
(
obj
)
{
return
obj
.
id
})
objectIds
=
Sequelize
.
Helper
.
Array
.
map
(
objects
,
function
(
obj
)
{
return
obj
.
id
})
var
getAssociatedObjects
=
function
(
callback
)
{
var
getAssociatedObjects
=
function
(
callback
)
{
self
[
assocName
](
function
(
associations
)
{
self
[
Sequelize
.
Helper
.
SQL
.
addPrefix
(
'get'
,
assocName
)](
function
(
associations
)
{
Sequelize
.
Helper
.
log
(
"current assocs:"
)
Sequelize
.
Helper
.
log
(
associations
)
currentAssociations
=
associations
currentAssociations
=
associations
callback
(
associations
)
if
(
callback
)
callback
(
associations
)
})
})
}
}
var
deleteObsoleteAssociations
=
function
(
callback
)
{
var
deleteObsoleteAssociations
=
function
(
callback
)
{
var
obsolete
=
[]
var
obsoleteAssociations
=
Sequelize
.
Helper
.
Array
.
select
(
currentAssociations
,
function
(
assoc
)
{
return
objectIds
.
indexOf
(
association
.
id
)
==
-
1
}),
currentAssociations
.
forEach
(
function
(
association
)
{
obsoleteIds
=
Sequelize
.
Helper
.
Array
.
map
(
obsoleteAssociations
,
function
(
assoc
)
{
return
assoc
.
id
})
if
(
objectIds
.
indexOf
(
association
.
id
)
==
-
1
)
obsolete
.
push
(
association
.
id
)
})
Sequelize
.
Helper
.
log
(
obsoleteIds
)
if
(
obsolete
.
length
==
0
)
if
(
obsoleteIds
.
length
==
0
)
callback
([])
callback
([])
else
else
sequelize
.
query
(
sequelize
.
query
(
Sequelize
.
sqlQueryFor
(
'delete'
,
{
table
:
Association
.
tableName
,
where
:
"id IN ("
+
obsolete
.
join
(
","
)
+
")"
,
limit
:
null
}),
Sequelize
.
sqlQueryFor
(
'delete'
,
{
table
:
Association
.
tableName
,
where
:
"id IN ("
+
obsolete
Ids
.
join
(
","
)
+
")"
,
limit
:
null
}),
function
(){
callback
(
obsolete
)
}
function
(){
callback
(
obsolete
Ids
)
}
)
)
}
}
var
createNewAssociations
=
function
(
obsolete
)
{
var
createNewAssociations
=
function
(
obsoleteIds
)
{
var
currentIds
=
Sequelize
.
Helper
.
Array
.
map
(
currentAssociations
,
function
(
assoc
)
{
return
assoc
.
id
})
var
currentIds
=
Sequelize
.
Helper
.
Array
.
map
(
currentAssociations
,
function
(
assoc
)
{
return
assoc
.
id
}),
var
withoutExisting
=
Sequelize
.
Helper
.
Array
.
reject
(
objects
,
function
(
o
)
{
withoutExisting
=
Sequelize
.
Helper
.
Array
.
select
(
objects
,
function
(
o
)
{
return
currentIds
.
indexOf
(
o
.
id
)
==
-
1
}),
currentIds
.
indexOf
(
o
.
id
)
>
-
1
savings
=
[]
})
var
savings
=
[]
Sequelize
.
Helper
.
log
(
"current"
)
Sequelize
.
Helper
.
log
(
obsoleteIds
)
Sequelize
.
Helper
.
log
(
currentIds
)
Sequelize
.
Helper
.
log
(
withoutExisting
)
Sequelize
.
Helper
.
log
(
"withoutExisiting"
)
withoutExisting
.
forEach
(
function
(
o
)
{
withoutExisting
.
forEach
(
function
(
o
)
{
Sequelize
.
Helper
.
log
(
o
)
if
((
o
instanceof
table2
)
&&
(
self
.
id
!=
null
)
&&
(
o
.
id
!=
null
))
{
if
((
o
instanceof
table2
)
&&
(
self
.
id
!=
null
)
&&
(
o
.
id
!=
null
))
{
var
attributes
=
{}
var
attributes
=
{}
attributes
[
self
.
table
.
identifier
]
=
self
.
id
attributes
[
Sequelize
.
Helper
.
SQL
.
asTableIdentifier
(
backAssocName
)
]
=
self
.
id
attributes
[
o
.
table
.
identifier
]
=
o
.
id
attributes
[
Sequelize
.
Helper
.
SQL
.
asTableIdentifier
(
assocName
)
]
=
o
.
id
savings
.
push
({
save
:
new
Association
(
attributes
)})
savings
.
push
({
save
:
new
Association
(
attributes
)})
}
}
})
})
Sequelize
.
chainQueries
(
savings
,
function
()
{
Sequelize
.
chainQueries
(
savings
,
function
()
{
getAssociatedObjects
(
callback
)
getAssociatedObjects
(
callback
)
})
})
...
@@ -67,9 +83,9 @@ module.exports.Factory = function(Sequelize, sequelize) {
...
@@ -67,9 +83,9 @@ module.exports.Factory = function(Sequelize, sequelize) {
})
})
}
}
},
},
createOneToManyGetter
:
function
(
table1
,
table2
)
{
createOneToManyGetter
:
function
(
table1
,
table2
,
assocName
)
{
return
function
(
callback
)
{
return
function
(
callback
)
{
var
whereConditions
=
[
table1
.
identifier
,
this
.
id
].
join
(
"="
)
var
whereConditions
=
[
Sequelize
.
Helper
.
SQL
.
asTableIdentifier
(
assocName
)
,
this
.
id
].
join
(
"="
)
table2
.
findAll
({
where
:
whereConditions
},
callback
)
table2
.
findAll
({
where
:
whereConditions
},
callback
)
}
}
},
},
...
@@ -79,18 +95,23 @@ module.exports.Factory = function(Sequelize, sequelize) {
...
@@ -79,18 +95,23 @@ module.exports.Factory = function(Sequelize, sequelize) {
objectIds
=
Sequelize
.
Helper
.
Array
.
map
(
objects
,
function
(
obj
)
{
return
obj
.
id
})
objectIds
=
Sequelize
.
Helper
.
Array
.
map
(
objects
,
function
(
obj
)
{
return
obj
.
id
})
this
[
assocName
](
function
(
currentAssociations
)
{
this
[
assocName
](
function
(
currentAssociations
)
{
var
currentIds
=
Sequelize
.
Helper
.
Array
.
map
(
currentAssociations
,
function
(
assoc
)
{
return
assoc
.
id
})
var
currentIds
=
Sequelize
.
Helper
.
Array
.
map
(
currentAssociations
,
function
(
assoc
)
{
return
assoc
.
id
}),
var
obsoleteAssociations
=
Sequelize
.
Helper
.
Array
.
select
(
currentAssociations
,
function
(
assoc
)
{
return
objectsIds
.
indexOf
(
assoc
.
id
)
==
-
1
})
obsoleteAssociations
=
Sequelize
.
Helper
.
Array
.
select
(
currentAssociations
,
function
(
assoc
)
{
return
objectsIds
.
indexOf
(
assoc
.
id
)
==
-
1
}),
var
queries
=
[]
queries
=
[]
obsoleteAssociations
.
forEach
(
function
(
assoc
)
{
obsoleteAssociations
.
forEach
(
function
(
assoc
)
{
var
attr
=
{};
attr
[
table1
.
identifier
]
=
null
var
attr
=
{}
attr
[
Sequelize
.
Helper
.
SQL
.
asTableIdentifier
(
assocName
)]
=
null
queries
.
push
({
updateAttributes
:
assoc
,
params
:
[
attr
]})
queries
.
push
({
updateAttributes
:
assoc
,
params
:
[
attr
]})
})
})
var
newAssociations
=
Sequelize
.
Helper
.
Array
.
select
(
objects
,
function
(
o
)
{
return
currentIds
.
indexOf
(
o
.
id
)
==
-
1
})
var
newAssociations
=
Sequelize
.
Helper
.
Array
.
select
(
objects
,
function
(
o
)
{
return
currentIds
.
indexOf
(
o
.
id
)
==
-
1
})
newAssociations
.
forEach
(
function
(
assoc
)
{
newAssociations
.
forEach
(
function
(
assoc
)
{
var
attr
=
{};
attr
[
table1
.
identifier
]
=
self
.
id
var
attr
=
{}
attr
[
Sequelize
.
Helper
.
SQL
.
asTableIdentifier
(
assocName
)]
=
self
.
id
queries
.
push
({
updateAttributes
:
assoc
,
params
:
[
attr
]})
queries
.
push
({
updateAttributes
:
assoc
,
params
:
[
attr
]})
})
})
Sequelize
.
chainQueries
(
queries
,
function
()
{
Sequelize
.
chainQueries
(
queries
,
function
()
{
self
[
assocName
](
callback
)
self
[
assocName
](
callback
)
})
})
...
...
lib/sequelize/SequelizeTable.js
View file @
9697633
...
@@ -144,13 +144,13 @@ exports.SequelizeTable = function(Sequelize, sequelize, tableName, attributes, o
...
@@ -144,13 +144,13 @@ exports.SequelizeTable = function(Sequelize, sequelize, tableName, attributes, o
// don't check inside of method to increase performance
// don't check inside of method to increase performance
if
(
backAssocName
)
{
if
(
backAssocName
)
{
table
.
prototype
[
assocName
]
=
Factory
.
createManyToManyGetter
(
table
,
_table
,
assocName
,
backAssocName
)
table
.
prototype
[
Sequelize
.
Helper
.
SQL
.
addPrefix
(
'get'
,
assocName
)]
=
Factory
.
createManyToManyGetter
(
table
,
_table
,
assocName
,
backAssocName
)
_table
.
prototype
[
backAssocName
]
=
Factory
.
createManyToManyGetter
(
_table
,
table
,
backAssocName
,
assocName
)
table
.
prototype
[
Sequelize
.
Helper
.
SQL
.
addPrefix
(
'set'
,
assocName
)]
=
Factory
.
createManyToManySetter
(
table
,
_table
,
assocName
,
backAssocName
)
table
.
prototype
[
Sequelize
.
Helper
.
SQL
.
addPrefix
(
'set'
,
assocName
)]
=
Factory
.
createManyToManySetter
(
table
,
_table
,
assocName
,
backAssocName
)
_table
.
prototype
[
Sequelize
.
Helper
.
SQL
.
addPrefix
(
'get'
,
backAssocName
)]
=
Factory
.
createManyToManyGetter
(
_table
,
table
,
backAssocName
,
assocName
)
_table
.
prototype
[
Sequelize
.
Helper
.
SQL
.
addPrefix
(
'set'
,
backAssocName
)]
=
Factory
.
createManyToManySetter
(
_table
,
table
,
backAssocName
,
assocName
)
_table
.
prototype
[
Sequelize
.
Helper
.
SQL
.
addPrefix
(
'set'
,
backAssocName
)]
=
Factory
.
createManyToManySetter
(
_table
,
table
,
backAssocName
,
assocName
)
}
else
{
}
else
{
table
.
prototype
[
assocName
]
=
Factory
.
createOneToManyGetter
(
table
,
_tabl
e
)
table
.
prototype
[
Sequelize
.
Helper
.
SQL
.
addPrefix
(
'get'
,
assocName
)]
=
Factory
.
createOneToManyGetter
(
table
,
_table
,
assocNam
e
)
table
.
prototype
[
Sequelize
.
Helper
.
SQL
.
addPrefix
(
'set'
,
assocName
)]
=
Factory
.
createOneToManySetter
(
table
)
table
.
prototype
[
Sequelize
.
Helper
.
SQL
.
addPrefix
(
'set'
,
assocName
)]
=
Factory
.
createOneToManySetter
(
table
,
assocName
)
}
}
return
table
return
table
...
...
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