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 155381bd
authored
Sep 03, 2010
by
Sascha Depold
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactored hasMany to get getter and setter from factory
1 parent
2d21c8c0
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
108 additions
and
89 deletions
lib/sequelize/Factory.js
lib/sequelize/SequelizeTable.js
lib/sequelize/Factory.js
0 → 100644
View file @
155381b
module
.
exports
.
Factory
=
function
(
Sequelize
,
sequelize
)
{
return
{
createManyToManyGetter
:
function
(
table1
,
table2
,
assocName
,
backAssocName
)
{
return
function
(
callback
)
{
var
Association
=
sequelize
.
tables
[
Sequelize
.
Helper
.
SQL
.
manyToManyTableName
(
assocName
,
table1
.
tableName
)].
klass
var
whereConditions
=
[
table1
.
identifier
,
this
.
id
].
join
(
"="
)
Association
.
findAll
({
where
:
whereConditions
},
function
(
result
)
{
if
(
result
.
length
>
0
)
{
var
ids
=
[]
result
.
forEach
(
function
(
resultSet
)
{
ids
.
push
(
resultSet
.
id
)
})
table2
.
findAll
({
where
:
"id IN ("
+
ids
.
join
(
","
)
+
")"
},
callback
)
}
else
{
if
(
callback
)
callback
([])
}
})
}
},
createManyToManySetter
:
function
(
table1
,
table2
,
assocName
,
backAssocName
)
{
return
function
(
objects
,
callback
)
{
var
self
=
this
,
Association
=
sequelize
.
tables
[
Sequelize
.
Helper
.
SQL
.
manyToManyTableName
(
assocName
,
table1
.
tableName
)].
klass
,
currentAssociations
=
null
,
objectIds
=
Sequelize
.
Helper
.
Array
.
map
(
objects
,
function
(
obj
)
{
return
obj
.
id
})
var
getAssociatedObjects
=
function
(
callback
)
{
self
[
assocName
](
function
(
associations
)
{
currentAssociations
=
associations
callback
(
associations
)
})
}
var
deleteObsoleteAssociations
=
function
(
callback
)
{
var
obsolete
=
[]
currentAssociations
.
forEach
(
function
(
association
)
{
if
(
objectIds
.
indexOf
(
association
.
id
)
==
-
1
)
obsolete
.
push
(
association
.
id
)
})
if
(
obsolete
.
length
==
0
)
callback
([])
else
sequelize
.
query
(
Sequelize
.
sqlQueryFor
(
'delete'
,
{
table
:
Association
.
tableName
,
where
:
"id IN ("
+
obsolete
.
join
(
","
)
+
")"
,
limit
:
null
}),
function
(){
callback
(
obsolete
)
}
)
}
var
createNewAssociations
=
function
(
obsolete
)
{
var
currentIds
=
Sequelize
.
Helper
.
Array
.
map
(
currentAssociations
,
function
(
assoc
)
{
return
assoc
.
id
})
var
withoutExisting
=
Sequelize
.
Helper
.
Array
.
reject
(
objects
,
function
(
o
)
{
currentIds
.
indexOf
(
o
.
id
)
>
-
1
})
var
savings
=
[]
withoutExisting
.
forEach
(
function
(
o
)
{
if
((
o
instanceof
table2
)
&&
(
self
.
id
!=
null
)
&&
(
o
.
id
!=
null
))
{
var
attributes
=
{}
attributes
[
self
.
table
.
identifier
]
=
self
.
id
attributes
[
o
.
table
.
identifier
]
=
o
.
id
savings
.
push
({
save
:
new
Association
(
attributes
)})
}
})
Sequelize
.
chainQueries
(
savings
,
function
()
{
getAssociatedObjects
(
callback
)
})
}
getAssociatedObjects
(
function
()
{
deleteObsoleteAssociations
(
function
(
obsolete
)
{
createNewAssociations
(
obsolete
)
})
})
}
},
createOneToManyGetter
:
function
(
table1
,
table2
)
{
return
function
(
callback
)
{
var
whereConditions
=
[
table1
.
identifier
,
this
.
id
].
join
(
"="
)
table2
.
findAll
({
where
:
whereConditions
},
callback
)
}
},
createOneToManySetter
:
function
(
table1
)
{
return
function
(
objects
,
callback
)
{
var
self
=
this
,
objectIds
=
Sequelize
.
Helper
.
Array
.
map
(
objects
,
function
(
obj
)
{
return
obj
.
id
})
this
[
assocName
](
function
(
currentAssociations
)
{
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
})
var
queries
=
[]
obsoleteAssociations
.
forEach
(
function
(
assoc
)
{
var
attr
=
{};
attr
[
table1
.
identifier
]
=
null
queries
.
push
({
updateAttributes
:
assoc
,
params
:
[
attr
]})
})
var
newAssociations
=
Sequelize
.
Helper
.
Array
.
select
(
objects
,
function
(
o
)
{
return
currentIds
.
indexOf
(
o
.
id
)
==
-
1
})
newAssociations
.
forEach
(
function
(
assoc
)
{
var
attr
=
{};
attr
[
table1
.
identifier
]
=
self
.
id
queries
.
push
({
updateAttributes
:
assoc
,
params
:
[
attr
]})
})
Sequelize
.
chainQueries
(
queries
,
function
()
{
self
[
assocName
](
callback
)
})
})
}
}
}
}
\ No newline at end of file
lib/sequelize/SequelizeTable.js
View file @
155381b
...
@@ -150,6 +150,8 @@ exports.SequelizeTable = function(Sequelize, sequelize, tableName, attributes, o
...
@@ -150,6 +150,8 @@ exports.SequelizeTable = function(Sequelize, sequelize, tableName, attributes, o
},
},
hasMany
:
function
(
assocName
,
_table
)
{
hasMany
:
function
(
assocName
,
_table
)
{
var
Factory
=
new
require
(
"./Factory"
).
Factory
(
Sequelize
,
sequelize
)
table
.
associations
.
push
({
table
.
associations
.
push
({
name
:
assocName
,
name
:
assocName
,
table
:
_table
,
table
:
_table
,
...
@@ -158,96 +160,11 @@ exports.SequelizeTable = function(Sequelize, sequelize, tableName, attributes, o
...
@@ -158,96 +160,11 @@ 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
(
_table
.
isCrossAssociatedWith
(
table
))
{
if
(
_table
.
isCrossAssociatedWith
(
table
))
{
table
.
prototype
[
assocName
]
=
function
(
callback
)
{
table
.
prototype
[
assocName
]
=
Factory
.
createManyToManyGetter
(
table
,
_table
,
assocName
)
var
Association
=
sequelize
.
tables
[
Sequelize
.
Helper
.
SQL
.
manyToManyTableName
(
assocName
,
table
.
tableName
)].
klass
table
.
prototype
[
Sequelize
.
Helper
.
SQL
.
addPrefix
(
'set'
,
assocName
)]
=
Factory
.
createManyToManySetter
(
table
,
_table
,
assocName
)
var
whereConditions
=
[
table
.
identifier
,
this
.
id
].
join
(
"="
)
Association
.
findAll
({
where
:
whereConditions
},
function
(
result
)
{
if
(
result
.
length
>
0
)
{
var
ids
=
[]
result
.
forEach
(
function
(
resultSet
)
{
ids
.
push
(
resultSet
.
id
)
})
_table
.
findAll
({
where
:
"id IN ("
+
ids
.
join
(
","
)
+
")"
},
callback
)
}
else
{
if
(
callback
)
callback
([])
}
})
}
table
.
prototype
[
Sequelize
.
Helper
.
SQL
.
addPrefix
(
'set'
,
assocName
)]
=
function
(
objects
,
callback
)
{
var
self
=
this
var
Association
=
sequelize
.
tables
[
Sequelize
.
Helper
.
SQL
.
manyToManyTableName
(
assocName
,
table
.
tableName
)].
klass
var
currentAssociations
=
null
var
objectIds
=
Sequelize
.
Helper
.
Array
.
map
(
objects
,
function
(
obj
)
{
return
obj
.
id
})
var
getAssociatedObjects
=
function
(
callback
)
{
self
[
assocName
](
function
(
associations
)
{
currentAssociations
=
associations
callback
(
associations
)
})
}
var
deleteObsoleteAssociations
=
function
(
callback
)
{
var
obsolete
=
[]
currentAssociations
.
forEach
(
function
(
association
)
{
if
(
objectIds
.
indexOf
(
association
.
id
)
==
-
1
)
obsolete
.
push
(
association
.
id
)
})
if
(
obsolete
.
length
==
0
)
callback
([])
else
sequelize
.
query
(
Sequelize
.
sqlQueryFor
(
'delete'
,
{
table
:
Association
.
tableName
,
where
:
"id IN ("
+
obsolete
.
join
(
","
)
+
")"
,
limit
:
null
}),
function
(){
callback
(
obsolete
)
}
)
}
var
createNewAssociations
=
function
(
obsolete
)
{
var
currentIds
=
Sequelize
.
Helper
.
Array
.
map
(
currentAssociations
,
function
(
assoc
)
{
return
assoc
.
id
})
var
withoutExisting
=
Sequelize
.
Helper
.
Array
.
reject
(
objects
,
function
(
o
)
{
currentIds
.
indexOf
(
o
.
id
)
>
-
1
})
var
savings
=
[]
withoutExisting
.
forEach
(
function
(
o
)
{
if
((
o
instanceof
_table
)
&&
(
self
.
id
!=
null
)
&&
(
o
.
id
!=
null
))
{
var
attributes
=
{}
attributes
[
self
.
table
.
identifier
]
=
self
.
id
attributes
[
o
.
table
.
identifier
]
=
o
.
id
savings
.
push
({
save
:
new
Association
(
attributes
)})
}
})
Sequelize
.
chainQueries
(
savings
,
function
()
{
getAssociatedObjects
(
callback
)
})
}
getAssociatedObjects
(
function
()
{
deleteObsoleteAssociations
(
function
(
obsolete
)
{
createNewAssociations
(
obsolete
)
})
})
}
}
else
{
}
else
{
table
.
prototype
[
assocName
]
=
function
(
callback
)
{
table
.
prototype
[
assocName
]
=
Factory
.
createOneToManyGetter
(
table
,
_table
)
var
whereConditions
=
[
table
.
identifier
,
this
.
id
].
join
(
"="
)
table
.
prototype
[
Sequelize
.
Helper
.
SQL
.
addPrefix
(
'set'
,
assocName
)]
=
Factory
.
createOneToManySetter
(
table
)
_table
.
findAll
({
where
:
whereConditions
},
callback
)
}
table
.
prototype
[
Sequelize
.
Helper
.
SQL
.
addPrefix
(
'set'
,
assocName
)]
=
function
(
objects
,
callback
)
{
var
self
=
this
var
objectIds
=
Sequelize
.
Helper
.
Array
.
map
(
objects
,
function
(
obj
)
{
return
obj
.
id
})
this
[
assocName
](
function
(
currentAssociations
)
{
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
})
var
queries
=
[]
obsoleteAssociations
.
forEach
(
function
(
assoc
)
{
var
attr
=
{};
attr
[
table
.
identifier
]
=
null
queries
.
push
({
updateAttributes
:
assoc
,
params
:
[
attr
]})
})
var
newAssociations
=
Sequelize
.
Helper
.
Array
.
select
(
objects
,
function
(
o
)
{
return
currentIds
.
indexOf
(
o
.
id
)
==
-
1
})
newAssociations
.
forEach
(
function
(
assoc
)
{
var
attr
=
{};
attr
[
table
.
identifier
]
=
self
.
id
queries
.
push
({
updateAttributes
:
assoc
,
params
:
[
attr
]})
})
Sequelize
.
chainQueries
(
queries
,
function
()
{
self
[
assocName
](
callback
)
})
})
}
}
}
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