Allow custom engine and column types
This commit is contained in:
parent
869348df09
commit
84b38b75cc
37
lib/mysql.js
37
lib/mysql.js
|
@ -535,6 +535,22 @@ MySQL.prototype.autoupdate = function (models, cb) {
|
|||
}
|
||||
};
|
||||
|
||||
/*!
|
||||
* Create table
|
||||
* @param model
|
||||
* @param cb
|
||||
*/
|
||||
MySQL.prototype.createTable = function (model, cb) {
|
||||
var metadata = this._models[model].settings[this.name];
|
||||
var engine = metadata && metadata.engine;
|
||||
var sql = 'CREATE TABLE ' + this.tableEscaped(model) +
|
||||
' (\n ' + this.propertiesSQL(model) + '\n)';
|
||||
if(engine) {
|
||||
sql += 'ENGINE=' + engine + '\n';
|
||||
}
|
||||
this.query(sql, cb);
|
||||
};
|
||||
|
||||
/**
|
||||
* Check if the models exist
|
||||
* @param {String[]} [models] A model name or an array of model names. If not present, apply to all models
|
||||
|
@ -786,11 +802,28 @@ MySQL.prototype.indexSettingsSQL = function (model, prop) {
|
|||
|
||||
MySQL.prototype.propertySettingsSQL = function (model, prop) {
|
||||
var p = this._models[model].properties[prop];
|
||||
var line = datatype(p) + ' ' +
|
||||
(p.allowNull === false || p['null'] === false ? 'NOT NULL' : 'NULL');
|
||||
var line = this.columnDataType(model, prop) + ' ' +
|
||||
(p.nullable === false || p.allowNull === false || p['null'] === false ? 'NOT NULL' : 'NULL');
|
||||
return line;
|
||||
};
|
||||
|
||||
MySQL.prototype.columnDataType = function (model, property) {
|
||||
var columnMetadata = this.columnMetadata(model, property);
|
||||
var colType = columnMetadata && columnMetadata.dataType;
|
||||
if(colType) {
|
||||
colType = colType.toUpperCase();
|
||||
}
|
||||
var prop = this._models[model].properties[property];
|
||||
if(!prop) {
|
||||
return null;
|
||||
}
|
||||
var colLength = columnMetadata && columnMetadata.dataLength || prop.length;
|
||||
if (colType) {
|
||||
return colType + (colLength ? '(' + colLength + ')' : '');
|
||||
}
|
||||
return datatype(prop);
|
||||
};
|
||||
|
||||
function datatype(p) {
|
||||
var dt = '';
|
||||
switch (p.type.name) {
|
||||
|
|
Loading…
Reference in New Issue