diff --git a/lib/mysql.js b/lib/mysql.js index 5e44138..5c8b2c4 100644 --- a/lib/mysql.js +++ b/lib/mysql.js @@ -6,6 +6,7 @@ var mysql = require('mysql'); var SqlConnector = require('loopback-connector').SqlConnector; var EnumFactory = require('./enumFactory').EnumFactory; +var async = require('async'); var debug = require('debug')('loopback:connector:mysql'); /** @@ -637,7 +638,7 @@ MySQL.prototype.destroyAll = function destroyAll(model, where, callback) { */ MySQL.prototype.autoupdate = function (models, cb) { var self = this; - var wait = 0; + if ((!cb) && ('function' === typeof models)) { cb = models; models = undefined; @@ -649,11 +650,11 @@ MySQL.prototype.autoupdate = function (models, cb) { models = models || Object.keys(this._models); - models.forEach(function (model) { + async.each(models, function (model, done) { if (model in self._models) { - wait++; - self.query('SHOW FIELDS FROM ' + self.tableEscaped(model), function (err, fields) { - self.query('SHOW INDEXES FROM ' + self.tableEscaped(model), function (err, indexes) { + var table = self.tableEscaped(model); + self.query('SHOW FIELDS FROM ' + table, function (err, fields) { + self.query('SHOW INDEXES FROM ' + table, function (err, indexes) { if (!err && fields && fields.length) { self.alterTable(model, fields, indexes, done); } else { @@ -662,16 +663,8 @@ MySQL.prototype.autoupdate = function (models, cb) { }); }); } - }); + }, cb); - function done(err) { - if (err) { - console.error('%j', err); - } - if (--wait === 0 && cb) { - cb(); - } - } }; /*! @@ -695,28 +688,29 @@ MySQL.prototype.createTable = function (model, cb) { * @param {String[]} [models] A model name or an array of model names. If not present, apply to all models * @param {Function} [cb] The callback function */ -MySQL.prototype.isActual = function (cb) { - var ok = false; +MySQL.prototype.isActual = function(cb) { var self = this; - var wait = 0; - Object.keys(this._models).forEach(function (model) { - wait += 1; - self.query('SHOW FIELDS FROM ' + model, function (err, fields) { - self.query('SHOW INDEXES FROM ' + model, function (err, indexes) { - self.alterTable(model, fields, indexes, done, true); + var ok = false; + async.each(Object.keys(this._models), function(model, done) { + var table = self.tableEscaped(model); + self.query('SHOW FIELDS FROM ' + table, function(err, fields) { + self.query('SHOW INDEXES FROM ' + table, function(err, indexes) { + self.alterTable(model, fields, indexes, function(err, needAlter) { + if (err) { + return done(err); + } else { + ok = ok || needAlter; + done(err); + } + }, true); }); }); - }); - - function done(err, needAlter) { + }, function(err) { if (err) { - debug(err); + return err; } - ok = ok || needAlter; - if (--wait === 0 && cb) { - cb(null, !ok); - } - } + cb(null, !ok); + }); }; MySQL.prototype.alterTable = function (model, actualFields, actualIndexes, done, checkOnly) { diff --git a/package.json b/package.json index 62a828e..1aaab2d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "loopback-connector-mysql", - "version": "1.4.5", + "version": "1.4.6", "description": "MySQL connector for loopback-datasource-juggler", "main": "index.js", "scripts": {