From 84b38b75ccb324fb553fd56c6708d83c2d7709bd Mon Sep 17 00:00:00 2001 From: Raymond Feng Date: Tue, 8 Oct 2013 13:44:58 -0700 Subject: [PATCH] Allow custom engine and column types --- lib/mysql.js | 37 +++++++++++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/lib/mysql.js b/lib/mysql.js index 8e45adc..281d4f7 100644 --- a/lib/mysql.js +++ b/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) {