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

Commit df5b611e by Sascha Depold

minor refactoring on many to many association setting

1 parent e60f8233
Showing with 15 additions and 15 deletions
...@@ -91,12 +91,7 @@ SequelizeTable = function(sequelize, tableName, attributes) { ...@@ -91,12 +91,7 @@ SequelizeTable = function(sequelize, tableName, attributes) {
sequelize.query( sequelize.query(
Sequelize.sqlQueryFor('select', queryOptions), Sequelize.sqlQueryFor('select', queryOptions),
function(result) { function(result) {
var objects = [] var objects = SequelizeHelper.Array.map(result, function(r) { return table.sqlResultToObject(r) })
result.forEach(function(resultSet) {
objects.push(table.sqlResultToObject(resultSet))
})
if(_callback) _callback(objects) if(_callback) _callback(objects)
} }
) )
...@@ -143,19 +138,21 @@ SequelizeTable = function(sequelize, tableName, attributes) { ...@@ -143,19 +138,21 @@ SequelizeTable = function(sequelize, tableName, attributes) {
result.forEach(function(resultSet) { ids.push(resultSet.id) }) result.forEach(function(resultSet) { ids.push(resultSet.id) })
_table.findAll({where: "id IN (" + ids.join(",") + ")"}, callback) _table.findAll({where: "id IN (" + ids.join(",") + ")"}, callback)
} else {
if(callback) callback([])
} }
}) })
} }
table.prototype[SequelizeHelper.SQL.addPrefix('set', assocName)] = function(objects, callback) { table.prototype[SequelizeHelper.SQL.addPrefix('set', assocName)] = function(objects, callback) {
var objectIds = SequelizeHelper.Array.map(objects, function(obj) { return obj.id })
var Association = sequelize.tables[SequelizeHelper.SQL.manyToManyTableName(_table, table)].klass
var self = this var self = this
var Association = sequelize.tables[SequelizeHelper.SQL.manyToManyTableName(_table, table)].klass
var currentAssociations = null var currentAssociations = null
var objectIds = SequelizeHelper.Array.map(objects, function(obj) { return obj.id })
var getAssociatedObjects = function(callback) { var getAssociatedObjects = function(callback) {
self[assocName](function(associations) { self[assocName](function(associations) {
currentAssociations = associations currentAssociations = associations
callback() callback(associations)
}) })
} }
var deleteObsoleteAssociations = function(callback) { var deleteObsoleteAssociations = function(callback) {
...@@ -163,10 +160,13 @@ SequelizeTable = function(sequelize, tableName, attributes) { ...@@ -163,10 +160,13 @@ SequelizeTable = function(sequelize, tableName, attributes) {
currentAssociations.forEach(function(association) { currentAssociations.forEach(function(association) {
if(objectIds.indexOf(association.id) == -1) obsolete.push(association.id) if(objectIds.indexOf(association.id) == -1) obsolete.push(association.id)
}) })
sequelize.query( if(obsolete.length == 0)
Sequelize.sqlQueryFor('delete', {table: Association.tableName, where: "id IN (" + obsolete.join(",") + ")"}), callback([])
function(){ callback(obsolete) } else
) sequelize.query(
Sequelize.sqlQueryFor('delete', {table: Association.tableName, where: "id IN (" + obsolete.join(",") + ")", limit: null}),
function(){ callback(obsolete) }
)
} }
var createNewAssociations = function(obsolete) { var createNewAssociations = function(obsolete) {
var currentIds = SequelizeHelper.Array.map(currentAssociations, function(assoc) { return assoc.id }) var currentIds = SequelizeHelper.Array.map(currentAssociations, function(assoc) { return assoc.id })
...@@ -178,8 +178,8 @@ SequelizeTable = function(sequelize, tableName, attributes) { ...@@ -178,8 +178,8 @@ SequelizeTable = function(sequelize, tableName, attributes) {
if((o instanceof _table) && (self.id != null) && (o.id != null)) { if((o instanceof _table) && (self.id != null) && (o.id != null)) {
var attributes = {} var attributes = {}
attributes[self.table.identifier] = self.id attributes[self.table.identifier] = self.id
attributes[object.table.identifier] = o.id attributes[o.table.identifier] = o.id
savings.push(new Association(attributes).save) savings.push({save: new Association(attributes)})
} }
}) })
Sequelize.chainQueries(savings, function() { Sequelize.chainQueries(savings, function() {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!