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

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) {
getterName = Sequelize.Helper.SQL.addPrefix('get', methodName || assocName)
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) {
......@@ -51,22 +51,33 @@ module.exports.Factory = function(Sequelize, sequelize) {
},
createManyToManyGetter: function(table1, table2, assocName, backAssocName) {
return function(callback) {
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 = Sequelize.Helper.Array.map(result, function(resultSet) {
return resultSet[Sequelize.Helper.SQL.asTableIdentifier(assocName)]
})
table2.findAll({where: "id IN (" + ids.join(",") + ")"}, callback)
} else {
if(callback) callback([])
}
})
return function(options, callback) {
var _callback = ((typeof options == 'object') ? callback : options),
_options = (typeof options == 'object') ? options : {},
self = this
if(_options.force || !this.hasFetchedAssociationFor(assocName)) {
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 = Sequelize.Helper.Array.map(result, function(resultSet) {
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) {
......@@ -123,20 +134,34 @@ module.exports.Factory = function(Sequelize, sequelize) {
})
}
},
createOneToManyGetter: function(table1, table2, assocName) {
return function(callback) {
var whereConditions = [Sequelize.Helper.SQL.asTableIdentifier(assocName), this.id].join("=")
table2.findAll({where: whereConditions}, callback)
createOneToManyGetter: function(table1, table2, assocName, methodName) {
return function(options, callback) {
var _callback = ((typeof options == 'object') ? callback : options),
_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) {
return function(objects, callback) {
var self = this,
objectIds = Sequelize.Helper.Array.map(objects, function(obj) { return obj.id })
var self = this,
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 }),
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 = []
obsoleteAssociations.forEach(function(assoc) {
......@@ -153,7 +178,7 @@ module.exports.Factory = function(Sequelize, sequelize) {
})
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!