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

Commit 96976330 by Sascha Depold

a lot of stuff in order to get the new assoc system run

1 parent 3a6b4f55
......@@ -2,12 +2,17 @@ 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, backAssocName)].klass
var whereConditions = [table1.identifier, this.id].join("=")
var Association = sequelize.tables[Sequelize.Helper.SQL.manyToManyTableName(assocName, backAssocName)].klass,
assocNameAsTableIdentifier = Sequelize.Helper.SQL.asTableIdentifier(backAssocName),
whereConditions = [assocNameAsTableIdentifier, this.id].join("=")
Association.findAll({ where: whereConditions }, function(result) {
if(result.length > 0) {
var ids = []
result.forEach(function(resultSet) { ids.push(resultSet.id) })
var ids = Sequelize.Helper.Array.map(result, function(resultSet) {
return resultSet[Sequelize.Helper.SQL.asTableIdentifier(backAssocName)]
})
Sequelize.Helper.log(ids)
Sequelize.Helper.log(table2.tableName)
table2.findAll({where: "id IN (" + ids.join(",") + ")"}, callback)
} else {
if(callback) callback([])
......@@ -23,38 +28,49 @@ module.exports.Factory = function(Sequelize, sequelize) {
objectIds = Sequelize.Helper.Array.map(objects, function(obj) { return obj.id })
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
callback(associations)
if(callback) 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)
var obsoleteAssociations = Sequelize.Helper.Array.select(currentAssociations, function(assoc) { return objectIds.indexOf(association.id) == -1 }),
obsoleteIds = Sequelize.Helper.Array.map(obsoleteAssociations, function(assoc) { return assoc.id })
Sequelize.Helper.log(obsoleteIds)
if(obsoleteIds.length == 0)
callback([])
else
sequelize.query(
Sequelize.sqlQueryFor('delete', {table: Association.tableName, where: "id IN (" + obsolete.join(",") + ")", limit: null}),
function(){ callback(obsolete) }
Sequelize.sqlQueryFor('delete', {table: Association.tableName, where: "id IN (" + obsoleteIds.join(",") + ")", limit: null}),
function(){ callback(obsoleteIds) }
)
}
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 = []
var createNewAssociations = function(obsoleteIds) {
var currentIds = Sequelize.Helper.Array.map(currentAssociations, function(assoc) { return assoc.id }),
withoutExisting = Sequelize.Helper.Array.select(objects, function(o) { return currentIds.indexOf(o.id) == -1 }),
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) {
Sequelize.Helper.log(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
attributes[Sequelize.Helper.SQL.asTableIdentifier(backAssocName)] = self.id
attributes[Sequelize.Helper.SQL.asTableIdentifier(assocName)] = o.id
savings.push({save: new Association(attributes)})
}
})
Sequelize.chainQueries(savings, function() {
getAssociatedObjects(callback)
})
......@@ -67,9 +83,9 @@ module.exports.Factory = function(Sequelize, sequelize) {
})
}
},
createOneToManyGetter: function(table1, table2) {
createOneToManyGetter: function(table1, table2, assocName) {
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)
}
},
......@@ -79,18 +95,23 @@ module.exports.Factory = function(Sequelize, sequelize) {
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 = []
var currentIds = Sequelize.Helper.Array.map(currentAssociations, function(assoc) { return assoc.id }),
obsoleteAssociations = Sequelize.Helper.Array.select(currentAssociations, function(assoc) { return objectsIds.indexOf(assoc.id) == -1 }),
queries = []
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]})
})
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
var attr = {}
attr[Sequelize.Helper.SQL.asTableIdentifier(assocName)] = self.id
queries.push({updateAttributes: assoc, params: [attr]})
})
Sequelize.chainQueries(queries, function() {
self[assocName](callback)
})
......
......@@ -144,13 +144,13 @@ exports.SequelizeTable = function(Sequelize, sequelize, tableName, attributes, o
// don't check inside of method to increase performance
if(backAssocName) {
table.prototype[assocName] = Factory.createManyToManyGetter(table, _table, assocName, backAssocName)
_table.prototype[backAssocName] = Factory.createManyToManyGetter(_table, table, backAssocName, assocName)
table.prototype[Sequelize.Helper.SQL.addPrefix('get', assocName)] = Factory.createManyToManyGetter(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)
} else {
table.prototype[assocName] = Factory.createOneToManyGetter(table, _table)
table.prototype[Sequelize.Helper.SQL.addPrefix('set', assocName)] = Factory.createOneToManySetter(table)
table.prototype[Sequelize.Helper.SQL.addPrefix('get', assocName)] = Factory.createOneToManyGetter(table, _table, assocName)
table.prototype[Sequelize.Helper.SQL.addPrefix('set', assocName)] = Factory.createOneToManySetter(table, assocName)
}
return table
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!