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 5dce3a5c
authored
Aug 04, 2010
by
Sascha Depold
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
huge refactoring of assocs
1 parent
5203dfcf
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
46 additions
and
32 deletions
src/SequelizeTable.js
src/SequelizeTable.js
View file @
5dce3a5
...
@@ -33,7 +33,7 @@ SequelizeTable = function(sequelize, tableName, attributes) {
...
@@ -33,7 +33,7 @@ SequelizeTable = function(sequelize, tableName, attributes) {
return
result
return
result
},
},
prepareAssociations
:
function
()
{
prepareAssociations
:
function
(
callback
)
{
table
.
associations
.
forEach
(
function
(
association
)
{
table
.
associations
.
forEach
(
function
(
association
)
{
switch
(
association
.
type
)
{
switch
(
association
.
type
)
{
case
'hasMany'
:
case
'hasMany'
:
...
@@ -58,6 +58,7 @@ SequelizeTable = function(sequelize, tableName, attributes) {
...
@@ -58,6 +58,7 @@ SequelizeTable = function(sequelize, tableName, attributes) {
break
break
}
}
})
})
if
(
callback
)
callback
()
},
},
sync
:
function
(
callback
)
{
sync
:
function
(
callback
)
{
...
@@ -145,39 +146,52 @@ SequelizeTable = function(sequelize, tableName, attributes) {
...
@@ -145,39 +146,52 @@ SequelizeTable = function(sequelize, tableName, attributes) {
}
}
})
})
}
}
table
.
prototype
[
SequelizeHelper
.
SQL
.
addPrefix
(
'
add'
,
assocName
)]
=
function
(
object
,
callback
)
{
table
.
prototype
[
SequelizeHelper
.
SQL
.
addPrefix
(
'
set'
,
assocName
)]
=
function
(
objects
,
callback
)
{
SequelizeHelper
.
log
(
SequelizeHelper
.
Hash
.
keys
(
sequelize
.
tables
)
)
var
objectIds
=
SequelizeHelper
.
Array
.
map
(
objects
,
function
(
obj
)
{
return
obj
.
id
}
)
var
Association
=
sequelize
.
tables
[
SequelizeHelper
.
SQL
.
manyToManyTableName
(
_table
,
table
)].
klass
var
Association
=
sequelize
.
tables
[
SequelizeHelper
.
SQL
.
manyToManyTableName
(
_table
,
table
)].
klass
if
(
object
instanceof
_table
)
{
var
self
=
this
if
((
this
.
id
!=
null
)
&&
(
object
.
id
!=
null
))
{
var
currentAssociations
=
null
var
attributes
=
{}
attributes
[
this
.
table
.
identifier
]
=
this
.
id
var
getAssociatedObjects
=
function
(
callback
)
{
attributes
[
object
.
table
.
identifier
]
=
object
.
id
self
[
assocName
](
function
(
associations
)
{
new
Association
(
attributes
).
save
(
callback
)
currentAssociations
=
associations
}
else
{
callback
()
throw
new
Error
(
"Please save the objects before setting the association!"
)
})
}
}
else
{
throw
new
Error
(
"You tried to add an object which is not associated with the class "
+
tableName
)
}
}
}
var
deleteObsoleteAssociations
=
function
(
callback
)
{
table
.
prototype
[
SequelizeHelper
.
SQL
.
addPrefix
(
'remove'
,
assocName
)]
=
function
(
object
,
callback
)
{
var
obsolete
=
[]
var
Association
=
sequelize
.
tables
[
SequelizeHelper
.
SQL
.
manyToManyTableName
(
_table
,
table
)].
klass
currentAssociations
.
forEach
(
function
(
association
)
{
if
(
object
instanceof
_table
)
{
if
(
objectIds
.
indexOf
(
association
.
id
)
==
-
1
)
obsolete
.
push
(
association
.
id
)
if
((
this
.
id
!=
null
)
&&
(
object
.
id
!=
null
))
{
})
var
attributes
=
{}
sequelize
.
query
(
attributes
[
this
.
identifier
]
=
this
.
id
Sequelize
.
sqlQueryFor
(
'delete'
,
{
table
:
Association
.
tableName
,
where
:
"id IN ("
+
obsolete
.
join
(
","
)
+
")"
}),
attributes
[
object
.
identifier
]
=
object
.
id
function
(){
callback
(
obsolete
)
}
Association
.
find
(
attributes
,
function
(
result
)
{
)
if
((
result
!=
null
)
&&
(
typeof
result
!=
'undefined'
))
result
.
destroy
(
callback
)
})
}
else
{
throw
new
Error
(
"Please save the objects before removing the association!"
)
}
}
else
{
throw
new
Error
(
"You tried to remove an object which is not associated with the class "
+
tableName
)
}
}
var
createNewAssociations
=
function
(
obsolete
)
{
var
currentIds
=
SequelizeHelper
.
Array
.
map
(
currentAssociations
,
function
(
assoc
)
{
return
assoc
.
id
})
var
withoutExisting
=
SequelizeHelper
.
Array
.
without
(
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
[
object
.
table
.
identifier
]
=
o
.
id
savings
.
push
(
new
Association
(
attributes
).
save
)
}
})
Sequelize
.
chainQueries
(
savings
,
function
()
{
getAssociatedObjects
(
callback
)
})
}
getAssociatedObjects
(
function
()
{
deleteObsoleteAssociations
(
function
(
obsolete
)
{
createNewAssociations
(
obsolete
)
})
})
}
}
}
else
{
}
else
{
table
.
prototype
[
assocName
]
=
function
(
callback
)
{
table
.
prototype
[
assocName
]
=
function
(
callback
)
{
...
@@ -272,7 +286,7 @@ SequelizeTable = function(sequelize, tableName, attributes) {
...
@@ -272,7 +286,7 @@ SequelizeTable = function(sequelize, tableName, attributes) {
destroy
:
function
(
callback
)
{
destroy
:
function
(
callback
)
{
sequelize
.
query
(
sequelize
.
query
(
Sequelize
.
sqlQueryFor
(
'delete'
,
{
table
:
table
.
tableName
,
id
:
this
.
id
}),
Sequelize
.
sqlQueryFor
(
'delete'
,
{
table
:
table
.
tableName
,
where
:
[
'id'
,
this
.
id
].
join
(
"="
)
}),
callback
callback
)
)
}
}
...
...
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