不要怂,就是干,撸起袖子干!

Commit 5dce3a5c by Sascha Depold

huge refactoring of assocs

1 parent 5203dfcf
Showing with 43 additions and 29 deletions
...@@ -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) {
var obsolete = []
currentAssociations.forEach(function(association) {
if(objectIds.indexOf(association.id) == -1) obsolete.push(association.id)
})
sequelize.query(
Sequelize.sqlQueryFor('delete', {table: Association.tableName, where: "id IN (" + obsolete.join(",") + ")"}),
function(){ callback(obsolete) }
)
} }
table.prototype[SequelizeHelper.SQL.addPrefix('remove', assocName)] = function(object, callback) { var createNewAssociations = function(obsolete) {
var Association = sequelize.tables[SequelizeHelper.SQL.manyToManyTableName(_table, table)].klass var currentIds = SequelizeHelper.Array.map(currentAssociations, function(assoc) { return assoc.id })
if(object instanceof _table) { var withoutExisting = SequelizeHelper.Array.without(objects, function(o) {
if((this.id != null) && (object.id != null)) { currentIds.indexOf(o.id) > -1
var attributes = {}
attributes[this.identifier] = this.id
attributes[object.identifier] = object.id
Association.find(attributes, function(result) {
if((result != null) && (typeof result != 'undefined'))
result.destroy(callback)
}) })
} else { var savings = []
throw new Error("Please save the objects before removing the association!") 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)
} }
} else { })
throw new Error("You tried to remove an object which is not associated with the class " + tableName) 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
) )
} }
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!