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

Commit 155381bd by Sascha Depold

refactored hasMany to get getter and setter from factory

1 parent 2d21c8c0
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
......@@ -150,6 +150,8 @@ exports.SequelizeTable = function(Sequelize, sequelize, tableName, attributes, o
},
hasMany: function(assocName, _table) {
var Factory = new require("./Factory").Factory(Sequelize, sequelize)
table.associations.push({
name: assocName,
table: _table,
......@@ -158,96 +160,11 @@ exports.SequelizeTable = function(Sequelize, sequelize, tableName, attributes, o
// don't check inside of method to increase performance
if(_table.isCrossAssociatedWith(table)) {
table.prototype[assocName] = function(callback) {
var Association = sequelize.tables[Sequelize.Helper.SQL.manyToManyTableName(assocName, table.tableName)].klass
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)
})
})
}
table.prototype[assocName] = Factory.createManyToManyGetter(table, _table, assocName)
table.prototype[Sequelize.Helper.SQL.addPrefix('set', assocName)] = Factory.createManyToManySetter(table, _table, assocName)
} else {
table.prototype[assocName] = function(callback) {
var whereConditions = [table.identifier, this.id].join("=")
_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)
})
})
}
table.prototype[assocName] = Factory.createOneToManyGetter(table, _table)
table.prototype[Sequelize.Helper.SQL.addPrefix('set', assocName)] = Factory.createOneToManySetter(table)
}
return table
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!