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

Commit 82120c5d by Sascha Depold

always force the refetch of associated data when setting them; one-to-many association storage works

1 parent 7eeccd0c
Showing with 51 additions and 26 deletions
...@@ -10,7 +10,7 @@ module.exports.Factory = function(Sequelize, sequelize) { ...@@ -10,7 +10,7 @@ module.exports.Factory = function(Sequelize, sequelize) {
getterName = Sequelize.Helper.SQL.addPrefix('get', methodName || assocName) getterName = Sequelize.Helper.SQL.addPrefix('get', methodName || assocName)
table1.prototype[setterName] = Factory.createOneToManySetter(table1, assocName, methodName) table1.prototype[setterName] = Factory.createOneToManySetter(table1, assocName, methodName)
table1.prototype[getterName] = Factory.createOneToManyGetter(table1, table2, assocName) table1.prototype[getterName] = Factory.createOneToManyGetter(table1, table2, assocName, methodName)
}, },
addOneToOneMethods: function(table1, table2, assocName, backAssocName) { addOneToOneMethods: function(table1, table2, assocName, backAssocName) {
...@@ -51,22 +51,33 @@ module.exports.Factory = function(Sequelize, sequelize) { ...@@ -51,22 +51,33 @@ module.exports.Factory = function(Sequelize, sequelize) {
}, },
createManyToManyGetter: function(table1, table2, assocName, backAssocName) { createManyToManyGetter: function(table1, table2, assocName, backAssocName) {
return function(callback) { return function(options, callback) {
var Association = sequelize.tables[Sequelize.Helper.SQL.manyToManyTableName(assocName, backAssocName)].klass, var _callback = ((typeof options == 'object') ? callback : options),
assocNameAsTableIdentifier = Sequelize.Helper.SQL.asTableIdentifier(backAssocName), _options = (typeof options == 'object') ? options : {},
whereConditions = [assocNameAsTableIdentifier, this.id].join("=") self = this
Association.findAll({ where: whereConditions }, function(result) { if(_options.force || !this.hasFetchedAssociationFor(assocName)) {
if(result.length > 0) { var Association = sequelize.tables[Sequelize.Helper.SQL.manyToManyTableName(assocName, backAssocName)].klass,
var ids = Sequelize.Helper.Array.map(result, function(resultSet) { assocNameAsTableIdentifier = Sequelize.Helper.SQL.asTableIdentifier(backAssocName),
return resultSet[Sequelize.Helper.SQL.asTableIdentifier(assocName)] whereConditions = [assocNameAsTableIdentifier, this.id].join("=")
})
Association.findAll({ where: whereConditions }, function(result) {
table2.findAll({where: "id IN (" + ids.join(",") + ")"}, callback) if(result.length > 0) {
} else { var ids = Sequelize.Helper.Array.map(result, function(resultSet) {
if(callback) callback([]) return resultSet[Sequelize.Helper.SQL.asTableIdentifier(assocName)]
} })
})
table2.findAll({where: "id IN (" + ids.join(",") + ")"}, function(objects) {
// self.fetchedAssociations[assocName] =
if(_callback) _callback(objects)
})
} else {
if(_callback) _callback([])
}
})
} else {
if(_callback) _callback(this.fetchedAssociations[assocName])
}
} }
}, },
createManyToManySetter: function(table1, table2, assocName, backAssocName) { createManyToManySetter: function(table1, table2, assocName, backAssocName) {
...@@ -123,20 +134,34 @@ module.exports.Factory = function(Sequelize, sequelize) { ...@@ -123,20 +134,34 @@ module.exports.Factory = function(Sequelize, sequelize) {
}) })
} }
}, },
createOneToManyGetter: function(table1, table2, assocName) { createOneToManyGetter: function(table1, table2, assocName, methodName) {
return function(callback) { return function(options, callback) {
var whereConditions = [Sequelize.Helper.SQL.asTableIdentifier(assocName), this.id].join("=") var _callback = ((typeof options == 'object') ? callback : options),
table2.findAll({where: whereConditions}, callback) _options = (typeof options == 'object') ? options : {},
accessKey = methodName || assocName,
self = this
if(_options.refetchAssociations || !this.hasFetchedAssociationFor(accessKey)) {
var whereConditions = [Sequelize.Helper.SQL.asTableIdentifier(assocName), this.id].join("=")
table2.findAll({where: whereConditions}, function(result) {
self.setAssociationDataFor(accessKey, result)
if(_callback) _callback(result)
})
} else {
var result = self.getAssociationDataFor(accessKey)
if(_callback) _callback(result)
}
} }
}, },
createOneToManySetter: function(table1, assocName, methodName) { createOneToManySetter: function(table1, assocName, methodName) {
return function(objects, callback) { return function(objects, callback) {
var self = this, var self = this,
objectIds = Sequelize.Helper.Array.map(objects, function(obj) { return obj.id }) objectIds = Sequelize.Helper.Array.map(objects, function(obj) { return obj.id }),
getterName = Sequelize.Helper.SQL.addPrefix('get', methodName)
this[Sequelize.Helper.SQL.addPrefix('get', methodName)](function(currentAssociations) { this[getterName]({refetchAssociations: true}, function(currentAssociations) {
var currentIds = Sequelize.Helper.Array.map(currentAssociations, function(assoc) { return assoc.id }), 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 }), obsoleteAssociations = Sequelize.Helper.Array.select(currentAssociations, function(assoc) { return objectIds.indexOf(assoc.id) == -1 }),
queries = [] queries = []
obsoleteAssociations.forEach(function(assoc) { obsoleteAssociations.forEach(function(assoc) {
...@@ -153,7 +178,7 @@ module.exports.Factory = function(Sequelize, sequelize) { ...@@ -153,7 +178,7 @@ module.exports.Factory = function(Sequelize, sequelize) {
}) })
Sequelize.chainQueries(queries, function() { Sequelize.chainQueries(queries, function() {
self[Sequelize.Helper.SQL.addPrefix('get', methodName)](callback) self[getterName]({refetchAssociations: true}, callback)
}) })
}) })
} }
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!