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

You need to sign in or sign up before continuing.
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 26 additions and 17 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,11 +77,14 @@ Sequelize.prototype = { ...@@ -66,11 +77,14 @@ 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()
}) })
if(SequelizeHelper.Hash.keys(tables).length == 0)
callback()
else
SequelizeHelper.Hash.forEach(tables, function(table) { SequelizeHelper.Hash.forEach(tables, function(table) {
table.constructor.sync(function() { table.klass.sync(function() {
finished.push(true) finished.push(true)
if(finished.length == SequelizeHelper.Hash.keys(tables).length) if(finished.length == SequelizeHelper.Hash.keys(tables).length)
callback() callback()
...@@ -82,8 +96,12 @@ Sequelize.prototype = { ...@@ -82,8 +96,12 @@ Sequelize.prototype = {
var finished = [] var finished = []
var tables = this.tables var tables = this.tables
if(SequelizeHelper.Hash.keys(tables).length == 0)
callback()
else
SequelizeHelper.Hash.forEach(tables, function(table, tableName) { SequelizeHelper.Hash.forEach(tables, function(table, tableName) {
table.constructor.drop(function() { SequelizeHelper.log(table)
table.klass.drop(function() {
finished.push(true) finished.push(true)
if(finished.length == SequelizeHelper.Hash.keys(tables).length) if(finished.length == SequelizeHelper.Hash.keys(tables).length)
callback() callback()
...@@ -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!