-
Notifications
You must be signed in to change notification settings - Fork 7
/
sequelize.ts
206 lines (192 loc) · 6.68 KB
/
sequelize.ts
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import utils from '../../../commons/utils/utils';
import typeUtil from '../../../commons/utils/typeUtil';
import { ConnectionData, SequleizeConfig, Schema, Table, Column, DataType } from '../../../commons/types';
/**
* Class which implements the logic for generating valid sequelize files.
*
* @export
* @class SequelizeModelGenerator
*/
export default class SequelizeModelGenerator {
/**
* Returns the string version of the code which will manage the sequelzie models.
*
* @returns {string} - sequelize models registry
* @memberof SequelizeModelGenerator
*/
public getModelsRegistry(): string {
return `'use strict';
const fs = require('fs');
const path = require('path');
const Sequelize = require('sequelize');
const basename = path.basename(module.filename);
const env = process.env.NODE_ENV || 'development';
const config = require(__dirname + '/../configs/database')[env];
const db = {};
let sequelize;
if (config.use_env_variable) {
sequelize = new Sequelize(process.env[config.use_env_variable]);
} else {
sequelize = new Sequelize(config.database, config.username, config.password, config);
}
fs
.readdirSync(__dirname)
.filter(function(file) {
return (file.indexOf('.') !== 0) && (file !== basename) && (file.slice(-3) === '.js');
})
.forEach(function(file) {
var model = sequelize['import'](path.join(__dirname, file));
db[model.name] = model;
});
Object.keys(db).forEach(function(modelName) {
if (db[modelName].associate) {
db[modelName].associate(db);
}
});
db.sequelize = sequelize;
db.Sequelize = Sequelize;
module.exports = db;
`;
}
/**
* Returns the sequelize config
*
* @param {ConnectionData} connection - connection data
* @returns {string} - connection string
* @memberof SequelizeModelGenerator
*/
public getConfig(connection: ConnectionData): string {
const config: SequleizeConfig = {
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}[]}
*/
public getModels(schema: Schema): {name: string; content: string}[] {
const models: {name: string; content: string}[] = [];
schema.forEach((table: Table) => {
models.push({
name: `${utils.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.
*/
private getModelForTable(table: Table): string {
return `module.exports = (sequelize, DataTypes) => {
const ${utils.toTitleCase(table.name)} = sequelize.define('${utils.singular(table.name).toLowerCase()}', {
${this.getBasicDataTypes(table)}
}, {
tableName: '${table.name}',
timestamps: false,
underscored: true,
});
${utils.toTitleCase(table.name)}.associate = (models) => {
${this.getRelations(table)}
}
return ${utils.toTitleCase(table.name)};
};`;
}
/**
* Generate contents for the basic data types.
* @param {Table} table - source
* @return {string} generated content.
*/
private getBasicDataTypes(table: Table): string {
let dataTypes: string = '';
table.columns.forEach((column: Column) => {
if (!typeUtil.isDefaultType(column.dataType.type)) {
return;
}
dataTypes += ` ${column.name}: {
type: ${this.getType(column.dataType)},
allowNull: ${column.allowNull ? 'true' : 'false'},
unique: ${column.unique ? 'true' : 'false'},
primaryKey: ${column.primary ? 'true' : 'false'},
${column.primary && column.dataType.type === 'number' ? 'autoIncrement: true,' : ''}
},\n`;
});
return dataTypes;
}
/**
* Returns the string version of the sequelize type.
* @param {DataType} type source
* @return {string} sequelize type
*/
private getType(type: DataType): string {
switch (type.type) {
case 'string':
return 'DataTypes.STRING()';
case 'number':
return 'DataTypes.INTEGER()';
case 'enum':
return `DataTypes.ENUM(${typeUtil.getEnumValuesAsString(type)})`;
/* istanbul ignore next */
default:
return '';
}
}
/**
* Generate association methods for types.
* @param {Table} table source
* @return {string} - generated content
*/
private getRelations(table: Table): string {
const modelName: string = utils.toTitleCase(table.name);
return table.columns.filter((column: Column) => !typeUtil.isDefaultType(column.dataType.type)).map((column: 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.pluralize(modelName).toLowerCase()}_${utils.pluralize(column.dataType.type).toLowerCase()}', as: '${column.name}'});`;
} else {
return ` ${modelName}.belongsToMany(models.${column.dataType.type.toLowerCase()}, {through: '${utils.pluralize(column.dataType.type).toLowerCase()}_${utils.pluralize(modelName).toLowerCase()}', as: '${column.name}'});`;
}
}
default:
return '';
}
}).join('\n');
}
}