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

Commit 8c573e76 by Sascha Depold

renamed constructor to klass; added method for chaining queries and waiting for them to end

1 parent d4e06e44
Showing with 36 additions and 27 deletions
...@@ -49,6 +49,17 @@ var classMethods = { ...@@ -49,6 +49,17 @@ var classMethods = {
} }
return SequelizeHelper.evaluateTemplate(query, values) return SequelizeHelper.evaluateTemplate(query, values)
},
chainQueries: function(queries, callback) {
var executeQuery = function(index) {
queries[index](function() {
if(queries.length > (index + 1))
executeQuery(index + 1)
else
if (callback) callback()
})
}
executeQuery(0)
} }
} }
...@@ -66,29 +77,36 @@ Sequelize.prototype = { ...@@ -66,29 +77,36 @@ Sequelize.prototype = {
var tables = this.tables var tables = this.tables
SequelizeHelper.Hash.forEach(tables, function(table) { SequelizeHelper.Hash.forEach(tables, function(table) {
table.constructor.prepareAssociations() table.klass.prepareAssociations()
}) })
SequelizeHelper.Hash.forEach(tables, function(table) { if(SequelizeHelper.Hash.keys(tables).length == 0)
table.constructor.sync(function() { callback()
finished.push(true) else
if(finished.length == SequelizeHelper.Hash.keys(tables).length) SequelizeHelper.Hash.forEach(tables, function(table) {
callback() table.klass.sync(function() {
finished.push(true)
if(finished.length == SequelizeHelper.Hash.keys(tables).length)
callback()
})
}) })
})
}, },
drop: function(callback) { drop: function(callback) {
var finished = [] var finished = []
var tables = this.tables var tables = this.tables
SequelizeHelper.Hash.forEach(tables, function(table, tableName) { if(SequelizeHelper.Hash.keys(tables).length == 0)
table.constructor.drop(function() { callback()
finished.push(true) else
if(finished.length == SequelizeHelper.Hash.keys(tables).length) SequelizeHelper.Hash.forEach(tables, function(table, tableName) {
callback() SequelizeHelper.log(table)
table.klass.drop(function() {
finished.push(true)
if(finished.length == SequelizeHelper.Hash.keys(tables).length)
callback()
})
}) })
})
}, },
define: function(name, attributes) { define: function(name, attributes) {
...@@ -98,7 +116,7 @@ Sequelize.prototype = { ...@@ -98,7 +116,7 @@ Sequelize.prototype = {
var table = new SequelizeTable(this, SequelizeHelper.SQL.asTableName(name), attributes) var table = new SequelizeTable(this, SequelizeHelper.SQL.asTableName(name), attributes)
table.attributes = attributes table.attributes = attributes
this.tables[name] = {constructor: table, attributes: attributes} this.tables[name] = {klass: table, attributes: attributes}
table.sequelize = this table.sequelize = this
return table return table
...@@ -135,16 +153,6 @@ Sequelize.prototype = { ...@@ -135,16 +153,6 @@ Sequelize.prototype = {
}) })
connection.close() connection.close()
}) })
},
waitForQueries: function(queries, callback) {
var finishedQueries = 0
queries.forEach(function(query) {
query(function() {
finishedQueries++
if(finishedQueries == queries.length)
callback()
})
})
} }
} }
......
...@@ -134,7 +134,7 @@ SequelizeTable = function(sequelize, tableName, attributes) { ...@@ -134,7 +134,7 @@ SequelizeTable = function(sequelize, tableName, attributes) {
// don't check inside of method to increase performance // don't check inside of method to increase performance
if(_table.isCrossAssociatedWith(table)) { if(_table.isCrossAssociatedWith(table)) {
table.prototype[assocName] = function(callback) { table.prototype[assocName] = function(callback) {
var Association = sequelize.tables[SequelizeHelper.SQL.manyToManyTableName(_table, table)].constructor var Association = sequelize.tables[SequelizeHelper.SQL.manyToManyTableName(_table, table)].klass
var whereConditions = [table.identifier, this.id].join("=") var whereConditions = [table.identifier, this.id].join("=")
Association.findAll({ where: whereConditions }, function(result) { Association.findAll({ where: whereConditions }, function(result) {
if(result.length > 0) { if(result.length > 0) {
...@@ -146,7 +146,8 @@ SequelizeTable = function(sequelize, tableName, attributes) { ...@@ -146,7 +146,8 @@ SequelizeTable = function(sequelize, tableName, attributes) {
}) })
} }
table.prototype[SequelizeHelper.SQL.addPrefix('add', assocName)] = function(object, callback) { table.prototype[SequelizeHelper.SQL.addPrefix('add', assocName)] = function(object, callback) {
var Association = sequelize.tables[SequelizeHelper.SQL.manyToManyTableName(_table, table)].constructor SequelizeHelper.log(SequelizeHelper.Hash.keys(sequelize.tables))
var Association = sequelize.tables[SequelizeHelper.SQL.manyToManyTableName(_table, table)].klass
if(object instanceof _table) { if(object instanceof _table) {
if((this.id != null) && (object.id != null)) { if((this.id != null) && (object.id != null)) {
var attributes = {} var attributes = {}
...@@ -161,7 +162,7 @@ SequelizeTable = function(sequelize, tableName, attributes) { ...@@ -161,7 +162,7 @@ SequelizeTable = function(sequelize, tableName, attributes) {
} }
} }
table.prototype[SequelizeHelper.SQL.addPrefix('remove', assocName)] = function(object, callback) { table.prototype[SequelizeHelper.SQL.addPrefix('remove', assocName)] = function(object, callback) {
var Association = sequelize.tables[SequelizeHelper.SQL.manyToManyTableName(_table, table)].constructor var Association = sequelize.tables[SequelizeHelper.SQL.manyToManyTableName(_table, table)].klass
if(object instanceof _table) { if(object instanceof _table) {
if((this.id != null) && (object.id != null)) { if((this.id != null) && (object.id != null)) {
var attributes = {} var attributes = {}
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!