Add optional models argument to autoupdate

This commit is contained in:
Raymond Feng 2013-08-08 08:35:29 -07:00
parent 9daf9ef91c
commit 90a3450488
1 changed files with 25 additions and 12 deletions

View File

@ -394,11 +394,23 @@ MySQL.prototype.all = function all(model, filter, callback) {
}; };
MySQL.prototype.autoupdate = function (cb) { MySQL.prototype.autoupdate = function (models, cb) {
var self = this; var self = this;
var wait = 0; var wait = 0;
Object.keys(this._models).forEach(function (model) { if ((!cb) && ('function' === typeof models)) {
wait += 1; cb = models;
models = undefined;
}
// First argument is a model name
if ('string' === typeof models) {
models = [models];
}
models = models || Object.keys(this._models);
models.forEach(function (model) {
if (model in self._models) {
wait++;
self.query('SHOW FIELDS FROM ' + self.tableEscaped(model), function (err, fields) { self.query('SHOW FIELDS FROM ' + self.tableEscaped(model), function (err, fields) {
self.query('SHOW INDEXES FROM ' + self.tableEscaped(model), function (err, indexes) { self.query('SHOW INDEXES FROM ' + self.tableEscaped(model), function (err, indexes) {
if (!err && fields.length) { if (!err && fields.length) {
@ -408,11 +420,12 @@ MySQL.prototype.autoupdate = function (cb) {
} }
}); });
}); });
}
}); });
function done(err) { function done(err) {
if (err) { if (err) {
console.log(err); console.error(err);
} }
if (--wait === 0 && cb) { if (--wait === 0 && cb) {
cb(); cb();