Add optional models argument to autoupdate
This commit is contained in:
parent
9daf9ef91c
commit
90a3450488
37
lib/mysql.js
37
lib/mysql.js
|
@ -394,25 +394,38 @@ MySQL.prototype.all = function all(model, filter, callback) {
|
|||
|
||||
};
|
||||
|
||||
MySQL.prototype.autoupdate = function (cb) {
|
||||
MySQL.prototype.autoupdate = function (models, cb) {
|
||||
var self = this;
|
||||
var wait = 0;
|
||||
Object.keys(this._models).forEach(function (model) {
|
||||
wait += 1;
|
||||
self.query('SHOW FIELDS FROM ' + self.tableEscaped(model), function (err, fields) {
|
||||
self.query('SHOW INDEXES FROM ' + self.tableEscaped(model), function (err, indexes) {
|
||||
if (!err && fields.length) {
|
||||
self.alterTable(model, fields, indexes, done);
|
||||
} else {
|
||||
self.createTable(model, done);
|
||||
}
|
||||
if ((!cb) && ('function' === typeof models)) {
|
||||
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 INDEXES FROM ' + self.tableEscaped(model), function (err, indexes) {
|
||||
if (!err && fields.length) {
|
||||
self.alterTable(model, fields, indexes, done);
|
||||
} else {
|
||||
self.createTable(model, done);
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
function done(err) {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
console.error(err);
|
||||
}
|
||||
if (--wait === 0 && cb) {
|
||||
cb();
|
||||
|
|
Loading…
Reference in New Issue