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

Commit e6c57ea9 by Sascha Depold

minor refactoring + finish of set association for 1:n assocs

1 parent 09b8f51f
Showing with 9 additions and 5 deletions
...@@ -203,18 +203,20 @@ SequelizeTable = function(sequelize, tableName, attributes) { ...@@ -203,18 +203,20 @@ SequelizeTable = function(sequelize, tableName, attributes) {
this[assocName](function(currentAssociations) { this[assocName](function(currentAssociations) {
var currentIds = SequelizeHelper.Array.map(currentAssociations, function(assoc) { return assoc.id }) var currentIds = SequelizeHelper.Array.map(currentAssociations, function(assoc) { return assoc.id })
var obsoleteAssociations = SequelizeHelper.Array.select(currentAssociations, function(assoc) { return objectsIds.indexOf(assoc.id) == -1 }) var obsoleteAssociations = SequelizeHelper.Array.select(currentAssociations, function(assoc) { return objectsIds.indexOf(assoc.id) == -1 })
var queries = []
obsoleteAssociations.forEach(function(assoc) { obsoleteAssociations.forEach(function(assoc) {
var attr = {}; attr[table.identifier] = null var attr = {}; attr[table.identifier] = null
assoc.updateAttributes(attr) queries.push({updateAttributes: assoc, params: [attr]})
}) })
var newAssociations = SequelizeHelper.Array.select(objects, function(o) { return currentIds.indexOf(o.id) == -1 }) var newAssociations = SequelizeHelper.Array.select(objects, function(o) { return currentIds.indexOf(o.id) == -1 })
newAssociations.forEach(function(assoc) { newAssociations.forEach(function(assoc) {
var attr = {}; attr[table.identifier] = self.id var attr = {}; attr[table.identifier] = self.id
assoc.updateAttributes(attr) queries.push({updateAttributes: assoc, params: [attr]})
})
Sequelize.chainQueries(queries, function() {
self[assocName](callback)
}) })
self[assocName](callback)
}) })
} }
} }
...@@ -276,7 +278,9 @@ SequelizeTable = function(sequelize, tableName, attributes) { ...@@ -276,7 +278,9 @@ SequelizeTable = function(sequelize, tableName, attributes) {
if(this.id == null) { if(this.id == null) {
this.createdAt = new Date() this.createdAt = new Date()
query = Sequelize.sqlQueryFor('insert', { query = Sequelize.sqlQueryFor('insert', {
table: table.tableName, fields: SequelizeHelper.SQL.fieldsForInsertQuery(this), values: SequelizeHelper.SQL.valuesForInsertQuery(this) table: table.tableName,
fields: SequelizeHelper.SQL.fieldsForInsertQuery(this),
values: SequelizeHelper.SQL.valuesForInsertQuery(this)
}) })
} else } else
query = Sequelize.sqlQueryFor('update', { table: table.tableName, values: SequelizeHelper.SQL.valuesForUpdate(this), id: this.id }) query = Sequelize.sqlQueryFor('update', { table: table.tableName, values: SequelizeHelper.SQL.valuesForUpdate(this), id: this.id })
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!