Skip to content

Commit

Permalink
Merge pull request sequelize#179 from isac322/fix_typescript
Browse files Browse the repository at this point in the history
Fix typescript, add missing option
  • Loading branch information
durango authored Oct 23, 2017
2 parents ef44041 + 0743798 commit 8c1e755
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 93 deletions.
3 changes: 3 additions & 0 deletions bin/sequelize-auto
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ var argv = require('yargs')
.alias('n', 'no-write')
.alias('s', 'schema')
.alias('z', 'typescript')
.alias('f', 'camel-file-name')
.describe('h', 'IP/Hostname for the database.')
.describe('d', 'Database name.')
.describe('u', 'Username for database.')
Expand All @@ -38,6 +39,7 @@ var argv = require('yargs')
.describe('n', 'Prevent writing the models to disk.')
.describe('s', 'Database schema from which to retrieve tables')
.describe('z', 'Output models as typescript with a definitions file')
.describe('f', 'Use camel case for file name')
.argv;

var dir = !argv.n && (argv.o || path.resolve(process.cwd() + '/models'));
Expand Down Expand Up @@ -72,6 +74,7 @@ configFile.skipTables = configFile.skipTables || (argv.T && argv.T.split(',')) |
configFile.camelCase = !!argv.C;
configFile.schema = argv.s || configFile.schema;
configFile.typescript = argv.z || configFile.typescript || false;
configFile.camelCaseForFileName = argv.f || configFile.camelCaseForFileName || false;

function getDefaultPort(dialect) {
switch (dialect.toLowerCase()) {
Expand Down
21 changes: 10 additions & 11 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ function AutoSequelize(database, username, password, options) {
directory: './models',
additional: {},
freezeTableName: true,
typescript: false
typescript: false,
camelCaseForFileName: false
}, options || {});
}

Expand Down Expand Up @@ -136,7 +137,6 @@ AutoSequelize.prototype.run = function(callback) {
var text = {};
var tables = [];
var typescriptFiles = [self.options.typescript ? tsHelper.def.getDefinitionFileStart() : '', ''];
var tsTableNames = [];

this.build(generateText);

Expand All @@ -160,12 +160,11 @@ AutoSequelize.prototype.run = function(callback) {
text[table] += "module.exports = function(sequelize, DataTypes) {\n";
text[table] += spaces + "return sequelize.define('" + tableName + "', {\n";
} else {
tsTableNames.push(tableName);
text[table] = tsHelper.model.getModelFileStart(self.options.indentation, spaces, tableName);
}

_.each(fields, function(field, i){
var additional = self.options.additional
var additional = self.options.additional;
if( additional && additional.timestamps !== undefined && additional.timestamps){
if((additional.createdAt && field === 'createdAt' || additional.createdAt === field )
||(additional.updatedAt && field === 'updatedAt' || additional.updatedAt === field )
Expand Down Expand Up @@ -382,7 +381,7 @@ AutoSequelize.prototype.run = function(callback) {
_.each(self.options.additional, addAdditionalOption)
}

text[table] = text[table].trim()
text[table] = text[table].trim();
text[table] = text[table].substring(0, text[table].length - 1);
text[table] += "\n" + spaces + "}";

Expand Down Expand Up @@ -410,15 +409,15 @@ AutoSequelize.prototype.run = function(callback) {
self.sequelize.close();

// typescript generate tables
if(self.options.typescript) typescriptFiles[1] = tsHelper.model.generateTableModels(tsTableNames, self.options.spaces, self.options.indentation);
if(self.options.typescript) typescriptFiles[1] = tsHelper.model.generateTableModels(_.keys(text), self.options.spaces, self.options.indentation, self.options.camelCase, self.options.camelCaseForFileName);

if (self.options.directory) {
return self.write(text, typescriptFiles, callback);
}
return callback(false, text);
});
}
}
};

AutoSequelize.prototype.write = function(attributes, typescriptFiles, callback) {
var tables = _.keys(attributes);
Expand All @@ -433,8 +432,8 @@ AutoSequelize.prototype.write = function(attributes, typescriptFiles, callback)
callback();
});

if(self.options.typescript){
if(typescriptFiles != null && typescriptFiles.length > 1){
if (self.options.typescript) {
if (typescriptFiles !== null && typescriptFiles.length > 1) {
fs.writeFileSync(path.join(self.options.directory, 'db.d.ts'), typescriptFiles[0], 'utf8');
fs.writeFileSync(path.join(self.options.directory, 'db.tables.ts'), typescriptFiles[1], 'utf8');
}
Expand All @@ -444,6 +443,6 @@ AutoSequelize.prototype.write = function(attributes, typescriptFiles, callback)
var fileName = self.options.camelCaseForFileName ? _.camelCase(table) : table;
fs.writeFile(path.resolve(path.join(self.options.directory, fileName + (self.options.typescript ? '.ts' : '.js'))), attributes[table], _callback);
}
}
};

module.exports = AutoSequelize
module.exports = AutoSequelize;
172 changes: 90 additions & 82 deletions lib/ts-helper.js
Original file line number Diff line number Diff line change
@@ -1,112 +1,120 @@
// put in seperate file to keep main sequelize-auto clean
'use strict';

var Sequelize = require('sequelize');
var _ = Sequelize.Utils._;

function getModelFileStart(indentation, spaces, tableName) {
var fileStart = "/* jshint indent: " + indentation + " */\n";
fileStart += '// tslint:disable\n';
fileStart += 'import * as sequelize from \'sequelize\';\n';
fileStart += 'import {DataTypes} from \'sequelize\';\n';
fileStart += 'import {' + tableName + 'Instance, ' + tableName + 'Attribute} from \'./db\';\n\n';
fileStart += "module.exports = function(sequelize: sequelize.Sequelize, DataTypes: DataTypes) {\n";
fileStart += spaces + 'return sequelize.define<' + tableName + 'Instance, ' + tableName + 'Attribute>(\'' + tableName + '\', {\n';
return fileStart;
var fileStart = "/* jshint indent: " + indentation + " */\n";
fileStart += '// tslint:disable\n';
fileStart += 'import * as sequelize from \'sequelize\';\n';
fileStart += 'import {DataTypes} from \'sequelize\';\n';
fileStart += 'import {' + tableName + 'Instance, ' + tableName + 'Attribute} from \'./db\';\n\n';
fileStart += "module.exports = function(sequelize: sequelize.Sequelize, DataTypes: DataTypes) {\n";
fileStart += spaces + 'return sequelize.define<' + tableName + 'Instance, ' + tableName + 'Attribute>(\'' + tableName + '\', {\n';
return fileStart;
}

function generateTableModels(tableNames, isSpaces, indentation) {
var spaces = '';
for (var i = 0; i < indentation; ++ i) {
spaces += (isSpaces === true ? ' ' : "\t");
}
function generateTableModels(tableNames, isSpaces, indentation, isCamelCase, isCamelCaseForFile) {
var spaces = '';
for (var i = 0; i < indentation; ++i) {
spaces += (isSpaces === true ? ' ' : "\t");
}

return generateImports() + generateInterface() + generateTableMapper();
return generateImports() + generateInterface() + generateTableMapper();

function generateImports() {
var fileTextOutput = '// tslint:disable\n';
fileTextOutput += 'import * as path from \'path\';\n';
fileTextOutput += 'import * as sequelize from \'sequelize\';\n';
fileTextOutput += 'import * as def from \'./db\';\n\n';
return fileTextOutput;
}
function generateImports() {
var fileTextOutput = '// tslint:disable\n';
fileTextOutput += 'import * as path from \'path\';\n';
fileTextOutput += 'import * as sequelize from \'sequelize\';\n';
fileTextOutput += 'import * as def from \'./db\';\n\n';
return fileTextOutput;
}

function generateInterface() {
var fileTextOutput = 'export interface ITables {\n';
for (var i = 0; i < tableNames.length; i++) {
var table = isCamelCase ? _.camelCase(tableNames[i]) : tableNames[i];

function generateInterface() {
var fileTextOutput = 'export interface ITables {\n';
for (var i = 0; i < tableNames.length; i++) {
fileTextOutput += spaces + tableNames[i] + ':def.' + tableNames[i] + 'Model;\n';
}
fileTextOutput += '}\n\n';
return fileTextOutput;
fileTextOutput += spaces + table + ':def.' + table + 'Model;\n';
}
fileTextOutput += '}\n\n';
return fileTextOutput;
}

function generateTableMapper() {
var fileTextOutput = 'export const getModels = function(seq:sequelize.Sequelize):ITables {\n';
fileTextOutput += spaces + 'const tables:ITables = {\n';
for (var i = 0; i < tableNames.length; i++) {
fileTextOutput += spaces + spaces + tableNames[i] + ': seq.import(path.join(__dirname, \'./' + tableNames[i] + '\')),\n';
}
fileTextOutput += spaces + '};\n';
fileTextOutput += spaces + 'return tables;\n';
fileTextOutput += '};\n';
return fileTextOutput;
function generateTableMapper() {
var fileTextOutput = 'export const getModels = function(seq:sequelize.Sequelize):ITables {\n';
fileTextOutput += spaces + 'const tables:ITables = {\n';
for (var i = 0; i < tableNames.length; i++) {
var tableForClass = isCamelCase ? _.camelCase(tableNames[i]) : tableNames[i];
var tableForFile = isCamelCaseForFile ? _.camelCase(tableNames[i]) : tableNames[i];

fileTextOutput += spaces + spaces + tableForClass + ': seq.import(path.join(__dirname, \'./' + tableForFile + '\')),\n';
}
fileTextOutput += spaces + '};\n';
fileTextOutput += spaces + 'return tables;\n';
fileTextOutput += '};\n';
return fileTextOutput;
}
}

exports.model = {
getModelFileStart,
generateTableModels
getModelFileStart,
generateTableModels
};

function getDefinitionFileStart() {
return '// tslint:disable\nimport * as Sequelize from \'sequelize\';\n\n';
return '// tslint:disable\nimport * as Sequelize from \'sequelize\';\n\n';
}

function getTableDefinition(tsTableDefAttr, tableName) {
var tableDef = '\n// table: ' + tableName + '\n';
tableDef += tsTableDefAttr + '\n}\n';
tableDef += 'export interface ' + tableName + 'Instance extends Sequelize.Instance<' + tableName + 'Attribute>, ' + tableName + 'Attribute { }\n';
tableDef += 'export interface ' + tableName + 'Model extends Sequelize.Model<' + tableName + 'Instance, ' + tableName + 'Attribute> { }\n';
return tableDef;
var tableDef = '\n// table: ' + tableName + '\n';
tableDef += tsTableDefAttr + '\n}\n';
tableDef += 'export interface ' + tableName + 'Instance extends Sequelize.Instance<' + tableName + 'Attribute>, ' + tableName + 'Attribute { }\n';
tableDef += 'export interface ' + tableName + 'Model extends Sequelize.Model<' + tableName + 'Instance, ' + tableName + 'Attribute> { }\n';
return tableDef;
}

// doing this in ts helper to not clutter up main index if statement
function getMemberDefinition(spaces, fieldName, val, allowNull) {
if (fieldName === undefined) return '';
var m = '\n' + spaces + fieldName + (allowNull === true ? '?:' : ':');

if (val === undefined) {
m += 'any';
} else if (val.indexOf('DataTypes.BOOLEAN') > -1) {
m += 'boolean';
} else if (val.indexOf('DataTypes.INTEGER') > -1) {
m += 'number';
} else if (val.indexOf('DataTypes.BIGINT') > -1) {
m += 'number';
} else if (val.indexOf('DataTypes.STRING') > -1) {
m += 'string';
} else if (val.indexOf('DataTypes.CHAR') > -1) {
m += 'string';
} else if (val.indexOf('DataTypes.REAL') > -1) {
m += 'number';
} else if (val.indexOf('DataTypes.TEXT') > -1) {
m += 'string';
} else if (val.indexOf('DataTypes.DATE') > -1) {
m += 'Date';
} else if (val.indexOf('DataTypes.FLOAT') > -1) {
m += 'number';
} else if (val.indexOf('DataTypes.DECIMAL') > -1) {
m += 'number';
} else if (val.indexOf('DataTypes.DOUBLE') > -1) {
m += 'number';
} else if (val.indexOf('DataTypes.UUIDV4') > -1) {
m += 'string';
} else {
m += 'any';
}
if (fieldName === undefined) return '';
var m = '\n' + spaces + fieldName + (allowNull === true ? '?:' : ':');

if (val === undefined) {
m += 'any';
} else if (val.indexOf('DataTypes.BOOLEAN') > -1) {
m += 'boolean';
} else if (val.indexOf('DataTypes.INTEGER') > -1) {
m += 'number';
} else if (val.indexOf('DataTypes.BIGINT') > -1) {
m += 'number';
} else if (val.indexOf('DataTypes.STRING') > -1) {
m += 'string';
} else if (val.indexOf('DataTypes.CHAR') > -1) {
m += 'string';
} else if (val.indexOf('DataTypes.REAL') > -1) {
m += 'number';
} else if (val.indexOf('DataTypes.TEXT') > -1) {
m += 'string';
} else if (val.indexOf('DataTypes.DATE') > -1) {
m += 'Date';
} else if (val.indexOf('DataTypes.FLOAT') > -1) {
m += 'number';
} else if (val.indexOf('DataTypes.DECIMAL') > -1) {
m += 'number';
} else if (val.indexOf('DataTypes.DOUBLE') > -1) {
m += 'number';
} else if (val.indexOf('DataTypes.UUIDV4') > -1) {
m += 'string';
} else {
m += 'any';
}

return m + ';';
return m + ';';
}

exports.def = {
getDefinitionFileStart,
getTableDefinition,
getMemberDefinition
getDefinitionFileStart,
getTableDefinition,
getMemberDefinition
};

0 comments on commit 8c1e755

Please sign in to comment.