1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 | 1× 1× 1× 1× 1× 1× 2× 1× 3× 3× 1× 8× 8× 8× 12× 8× 1× 12× 1× 12× 12× 12× 43× 10× 33× 12× 1× 33× 21× 11× 1× 1× 1× 12× 43× 10× 4× 1× 3× 2× 1× 1× 2× 1× 1× 2× 1× 1× | "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var utils_1 = require("../../../commons/utils/utils"); var typeUtil_1 = require("../../../commons/utils/typeUtil"); /** * Class which implements the logic for generating valid sequelize files. * * @export * @class SequelizeModelGenerator */ var SequelizeModelGenerator = (function () { function SequelizeModelGenerator() { } /** * Returns the string version of the code which will manage the sequelzie models. * * @returns {string} - sequelize models registry * @memberof SequelizeModelGenerator */ SequelizeModelGenerator.prototype.getModelsRegistry = function () { return "'use strict';\nconst fs = require('fs');\nconst path = require('path');\nconst Sequelize = require('sequelize');\nconst basename = path.basename(module.filename);\nconst env = process.env.NODE_ENV || 'development';\nconst config = require(__dirname + '/../configs/database')[env];\nconst db = {};\nlet sequelize;\n\nif (config.use_env_variable) {\n sequelize = new Sequelize(process.env[config.use_env_variable]);\n} else {\n sequelize = new Sequelize(config.database, config.username, config.password, config);\n}\nfs\n .readdirSync(__dirname)\n .filter(function(file) {\n return (file.indexOf('.') !== 0) && (file !== basename) && (file.slice(-3) === '.js');\n })\n .forEach(function(file) {\n var model = sequelize['import'](path.join(__dirname, file));\n db[model.name] = model;\n });\nObject.keys(db).forEach(function(modelName) {\n if (db[modelName].associate) {\n db[modelName].associate(db);\n }\n});\ndb.sequelize = sequelize;\ndb.Sequelize = Sequelize;\nmodule.exports = db;\n"; }; /** * Returns the sequelize config * * @param {ConnectionData} connection - connection data * @returns {string} - connection string * @memberof SequelizeModelGenerator */ SequelizeModelGenerator.prototype.getConfig = function (connection) { var config = { development: { uri: connection.dialect.toLowerCase() + "://" + connection.user + ":" + connection.password + "@" + connection.host + ":" + connection.port + "/" + connection.database, username: connection.user, password: connection.password, database: connection.database, host: connection.host, dialect: connection.dialect.toLowerCase(), port: connection.port, }, production: { uri: 'mysql://user:password@host:port/database', username: 'user', password: 'password', database: 'database', host: 'host', dialect: 'mysql', port: 'port', }, }; return "module.exports = " + JSON.stringify(config, undefined, 2); }; /** * Generate models for a given schema. * * @param {Schema} schema - api schema * @return {{name: string, content: string}[]} */ SequelizeModelGenerator.prototype.getModels = function (schema) { var _this = this; var models = []; schema.forEach(function (table) { models.push({ name: utils_1.default.toTitleCase(table.name) + ".js", content: _this.getModelForTable(table), }); }); return models; }; /** * Generate contents for each model based on the table schema. * * @param {Table} table - source * @return {string} content of sequelize model as string. */ SequelizeModelGenerator.prototype.getModelForTable = function (table) { return "module.exports = (sequelize, DataTypes) => {\n const " + utils_1.default.toTitleCase(table.name) + " = sequelize.define('" + utils_1.default.singular(table.name).toLowerCase() + "', {\n" + this.getBasicDataTypes(table) + "\n }, {\n tableName: '" + table.name + "',\n timestamps: false,\n underscored: true,\n });\n " + utils_1.default.toTitleCase(table.name) + ".associate = (models) => {\n" + this.getRelations(table) + "\n }\n return " + utils_1.default.toTitleCase(table.name) + ";\n};"; }; /** * Generate contents for the basic data types. * @param {Table} table - source * @return {string} generated content. */ SequelizeModelGenerator.prototype.getBasicDataTypes = function (table) { var _this = this; var dataTypes = ''; table.columns.forEach(function (column) { if (!typeUtil_1.default.isDefaultType(column.dataType.type)) { return; } dataTypes += " " + column.name + ": {\n type: " + _this.getType(column.dataType) + ",\n allowNull: " + (column.allowNull ? 'true' : 'false') + ",\n unique: " + (column.unique ? 'true' : 'false') + ",\n primaryKey: " + (column.primary ? 'true' : 'false') + ",\n " + (column.primary && column.dataType.type === 'number' ? 'autoIncrement: true,' : '') + "\n },\n"; }); return dataTypes; }; /** * Returns the string version of the sequelize type. * @param {DataType} type source * @return {string} sequelize type */ SequelizeModelGenerator.prototype.getType = function (type) { switch (type.type) { case 'string': return 'DataTypes.STRING()'; case 'number': return 'DataTypes.INTEGER()'; case 'enum': return "DataTypes.ENUM(" + typeUtil_1.default.getEnumValuesAsString(type) + ")"; /* istanbul ignore next */ default: return ''; } }; /** * Generate association methods for types. * @param {Table} table source * @return {string} - generated content */ SequelizeModelGenerator.prototype.getRelations = function (table) { var modelName = utils_1.default.toTitleCase(table.name); return table.columns.filter(function (column) { return !typeUtil_1.default.isDefaultType(column.dataType.type); }).map(function (column) { switch (column.dataType.relationType) { case '1-1': { if (column.dataType.isRelationHolder) { return " " + modelName + ".hasOne(models." + column.dataType.type.toLowerCase() + ", {as: '" + column.name + "'});"; } else { return " " + modelName + ".belongsTo(models." + column.dataType.type.toLowerCase() + ", {as: '" + column.name + "'});"; } } case '1-n': { if (column.dataType.isRelationHolder) { return " " + modelName + ".hasMany(models." + column.dataType.type.toLowerCase() + ", {as: '" + column.name + "'});"; } else { return " " + modelName + ".belongsTo(models." + column.dataType.type.toLowerCase() + ", {as: '" + column.name + "'});"; } } case 'n-n': { if (modelName.localeCompare(column.dataType.type) > 0) { return " " + modelName + ".belongsToMany(models." + column.dataType.type.toLowerCase() + ", {through: '" + utils_1.default.pluralize(modelName).toLowerCase() + "_" + utils_1.default.pluralize(column.dataType.type).toLowerCase() + "', as: '" + column.name + "'});"; } else { return " " + modelName + ".belongsToMany(models." + column.dataType.type.toLowerCase() + ", {through: '" + utils_1.default.pluralize(column.dataType.type).toLowerCase() + "_" + utils_1.default.pluralize(modelName).toLowerCase() + "', as: '" + column.name + "'});"; } } default: return ''; } }).join('\n'); }; return SequelizeModelGenerator; }()); exports.default = SequelizeModelGenerator; |