Allow custom table name in mysql
This commit is contained in:
parent
028375b16e
commit
d3231e7484
|
@ -29,6 +29,7 @@ function MySQL(client) {
|
|||
}
|
||||
|
||||
MySQL.prototype.define = function (descr) {
|
||||
if (!descr.settings) descr.settings = {};
|
||||
this._models[descr.model.modelName] = descr;
|
||||
};
|
||||
|
||||
|
@ -46,8 +47,12 @@ MySQL.prototype.query = function (sql, callback) {
|
|||
});
|
||||
};
|
||||
|
||||
MySQL.prototype.table = function (model) {
|
||||
return this._models[model].settings.table || model;
|
||||
};
|
||||
|
||||
MySQL.prototype.save = function (model, data, callback) {
|
||||
var sql = 'UPDATE ' + model + ' SET ' + this.toFields(model, data) +
|
||||
var sql = 'UPDATE ' + this.table(model) + ' SET ' + this.toFields(model, data) +
|
||||
' WHERE id = ' + data.id;
|
||||
|
||||
this.query(sql, function (err) {
|
||||
|
@ -60,7 +65,7 @@ MySQL.prototype.save = function (model, data, callback) {
|
|||
*/
|
||||
MySQL.prototype.create = function (model, data, callback) {
|
||||
var fields = this.toFields(model, data);
|
||||
var sql = 'INSERT ' + model;
|
||||
var sql = 'INSERT ' + this.table(model);
|
||||
if (fields) {
|
||||
sql += ' SET ' + fields;
|
||||
} else {
|
||||
|
@ -140,7 +145,7 @@ MySQL.prototype.fromDatabase = function (model, data) {
|
|||
};
|
||||
|
||||
MySQL.prototype.exists = function (model, id, callback) {
|
||||
var sql = 'SELECT 1 FROM ' + model + ' WHERE id = ' + id + ' LIMIT 1';
|
||||
var sql = 'SELECT 1 FROM ' + this.table(model) + ' WHERE id = ' + id + ' LIMIT 1';
|
||||
this.query(sql, function (err, data) {
|
||||
if (err) return callback(err);
|
||||
callback(null, data.length === 1);
|
||||
|
@ -148,7 +153,7 @@ MySQL.prototype.exists = function (model, id, callback) {
|
|||
};
|
||||
|
||||
MySQL.prototype.find = function find(model, id, callback) {
|
||||
var sql = 'SELECT * FROM ' + model + ' WHERE id = ' + id + ' LIMIT 1';
|
||||
var sql = 'SELECT * FROM ' + this.table(model) + ' WHERE id = ' + id + ' LIMIT 1';
|
||||
this.query(sql, function (err, data) {
|
||||
if (data && data.length === 1) {
|
||||
data[0].id = id;
|
||||
|
@ -160,7 +165,7 @@ MySQL.prototype.find = function find(model, id, callback) {
|
|||
};
|
||||
|
||||
MySQL.prototype.destroy = function destroy(model, id, callback) {
|
||||
var sql = 'DELETE FROM ' + model + ' WHERE id = ' + id + ' LIMIT 1';
|
||||
var sql = 'DELETE FROM ' + this.table(model) + ' WHERE id = ' + id + ' LIMIT 1';
|
||||
this.query(sql, function (err) {
|
||||
callback(err);
|
||||
});
|
||||
|
@ -168,7 +173,7 @@ MySQL.prototype.destroy = function destroy(model, id, callback) {
|
|||
|
||||
MySQL.prototype.all = function all(model, filter, callback) {
|
||||
|
||||
var sql = 'SELECT * FROM ' + model;
|
||||
var sql = 'SELECT * FROM ' + this.table(model);
|
||||
var self = this;
|
||||
var props = this._models[model].properties;
|
||||
|
||||
|
@ -259,7 +264,7 @@ MySQL.prototype.all = function all(model, filter, callback) {
|
|||
};
|
||||
|
||||
MySQL.prototype.destroyAll = function destroyAll(model, callback) {
|
||||
this.query('DELETE FROM ' + model, function (err) {
|
||||
this.query('DELETE FROM ' + this.table(model), function (err) {
|
||||
if (err) {
|
||||
return callback(err, []);
|
||||
}
|
||||
|
@ -271,7 +276,7 @@ MySQL.prototype.count = function count(model, callback, where) {
|
|||
var self = this;
|
||||
var props = this._models[model].properties;
|
||||
|
||||
this.query('SELECT count(*) as cnt FROM ' + model + buildWhere(where), function (err, res) {
|
||||
this.query('SELECT count(*) as cnt FROM ' + this.table(model) + buildWhere(where), function (err, res) {
|
||||
callback(err, err ? null : res[0].cnt);
|
||||
});
|
||||
|
||||
|
@ -323,7 +328,7 @@ MySQL.prototype.autoupdate = function (cb) {
|
|||
var wait = 0;
|
||||
Object.keys(this._models).forEach(function (model) {
|
||||
wait += 1;
|
||||
self.query('SHOW FIELDS FROM ' + model, function (err, fields) {
|
||||
self.query('SHOW FIELDS FROM ' + self.table(model), function (err, fields) {
|
||||
if (!err && fields.length) {
|
||||
self.alterTable(model, fields, done);
|
||||
} else {
|
||||
|
@ -423,11 +428,11 @@ MySQL.prototype.alterTable = function (model, actualFields, done, checkOnly) {
|
|||
};
|
||||
|
||||
MySQL.prototype.dropTable = function (model, cb) {
|
||||
this.query('DROP TABLE IF EXISTS ' + model, cb);
|
||||
this.query('DROP TABLE IF EXISTS ' + this.table(model), cb);
|
||||
};
|
||||
|
||||
MySQL.prototype.createTable = function (model, cb) {
|
||||
this.query('CREATE TABLE ' + model +
|
||||
this.query('CREATE TABLE ' + this.table(model) +
|
||||
' (\n ' + this.propertiesSQL(model) + '\n)', cb);
|
||||
};
|
||||
|
||||
|
@ -468,3 +473,4 @@ function datatype(p) {
|
|||
}
|
||||
return dt;
|
||||
}
|
||||
|
||||
|
|
|
@ -66,7 +66,7 @@ function testOrm(schema) {
|
|||
content: { type: Text },
|
||||
date: { type: Date, default: Date.now, index: true },
|
||||
published: { type: Boolean, default: false }
|
||||
});
|
||||
}, {table: 'posts'});
|
||||
|
||||
Post.validateAsync('title', function (err, done) {
|
||||
process.nextTick(done);
|
||||
|
|
Loading…
Reference in New Issue