Skip to content

Commit

Permalink
Added typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
gsdnano committed Jul 18, 2017
1 parent d5eec88 commit fbadaa4
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 34 deletions.
54 changes: 20 additions & 34 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var mkdirp = require('mkdirp');
var dialects = require('./dialects');
var _ = Sequelize.Utils._;
var SqlString = require('./sql-string');
var tsHelper = require('./ts-helper');

function AutoSequelize(database, username, password, options) {
if (options && options.dialect === 'sqlite' && ! options.storage)
Expand Down Expand Up @@ -133,7 +134,7 @@ AutoSequelize.prototype.run = function(callback) {
var self = this;
var text = {};
var tables = [];
var typescriptDef = self.options.typescript ? `import Sequelize from 'sequelize';\n\n` : '';
var typescriptDefFile = self.options.typescript ? tsHelper.def.getDefinitionFileStart() : '';

this.build(generateText);

Expand All @@ -153,14 +154,11 @@ AutoSequelize.prototype.run = function(callback) {
var tsTableDefAttr = self.options.typescript ? 'export interface ' + tableName + 'Attribute {' : '';
text[table] = "/* jshint indent: " + self.options.indentation + " */\n\n";

if(self.options.typescript){
text[table] += 'import sequelize, { DataTypes } from \'sequelize\';\n';
text[table] += 'import { ' + tableName + 'Instance, ' + tableName + 'Attribute } from \'./db.d\';\n\n';
text[table] += "module.exports = function(sequelize:sequelize.Sequelize, DataTypes:DataTypes) {\n";
text[table] += spaces + 'return sequelize.define<' + tableName + 'Instance, ' + tableName + 'Attribute>(\'' + tableName + '\', {\n';
} else {
if(!self.options.typescript){
text[table] += "module.exports = function(sequelize, DataTypes) {\n";
text[table] += spaces + "return sequelize.define('" + tableName + "', {\n";
} else {
text[table] += tsHelper.model.getModelFileStart(spaces, tableName);
}

_.each(fields, function(field, i){
Expand Down Expand Up @@ -192,6 +190,10 @@ AutoSequelize.prototype.run = function(callback) {
self.tables[table][field].type = "ENUM(" + self.tables[table][field].special.map(function(f){ return quoteWrapper + f + quoteWrapper; }).join(',') + ")";
}

// typescript
var tsAllowNull = '';
var tsVal = '';

var isUnique = self.tables[table][field].foreignKey && self.tables[table][field].foreignKey.isUnique

_.each(fieldAttr, function(attr, x){
Expand Down Expand Up @@ -220,6 +222,7 @@ AutoSequelize.prototype.run = function(callback) {
}
else if (attr === "allowNull") {
text[table] += spaces + spaces + spaces + attr + ": " + self.tables[table][field][attr];
if(self.options.typescript) tsAllowNull = self.tables[table][field][attr];
}
else if (attr === "defaultValue") {
if (self.sequelize.options.dialect === "mssql" && defaultVal && defaultVal.toLowerCase() === '(newid())') {
Expand Down Expand Up @@ -273,7 +276,6 @@ AutoSequelize.prototype.run = function(callback) {

if (_attr === "boolean" || _attr === "bit(1)" || _attr === "bit") {
val = 'DataTypes.BOOLEAN';
if(self.options.typescript) tsTableDefAttr += `${spaces}${fieldName}:boolean;\n`;
}
else if (_attr.match(/^(smallint|mediumint|tinyint|int)/)) {
var length = _attr.match(/\(\d+\)/);
Expand All @@ -284,71 +286,56 @@ AutoSequelize.prototype.run = function(callback) {

var zero = _attr.match(/zerofill/i);
if (zero) val += '.ZEROFILL'
if(self.options.typescript) tsTableDefAttr += `${spaces}${fieldName}:number;\n`;
}
else if (_attr.match(/^bigint/)) {
val = 'DataTypes.BIGINT';
if(self.options.typescript) tsTableDefAttr += `${spaces}${fieldName}:number;\n`;
}
else if (_attr.match(/^varchar/)) {
var length = _attr.match(/\(\d+\)/);
val = 'DataTypes.STRING' + (! _.isNull(length) ? length : '');
if(self.options.typescript) tsTableDefAttr += `${spaces}${fieldName}:string;\n`;
}
else if (_attr.match(/^string|varying|nvarchar/)) {
val = 'DataTypes.STRING';
if(self.options.typescript) tsTableDefAttr += `${spaces}${fieldName}:string;\n`;
}
else if (_attr.match(/^char/)) {
var length = _attr.match(/\(\d+\)/);
val = 'DataTypes.CHAR' + (! _.isNull(length) ? length : '');
if(self.options.typescript) tsTableDefAttr += `${spaces}${fieldName}:string;\n`;
}
else if (_attr.match(/^real/)) {
val = 'DataTypes.REAL';
if(self.options.typescript) tsTableDefAttr += `${spaces}${fieldName}:number;\n`;
}
else if (_attr.match(/text|ntext$/)) {
val = 'DataTypes.TEXT';
if(self.options.typescript) tsTableDefAttr += `${spaces}${fieldName}:string;\n`;
}
else if (_attr.match(/^(date|timestamp)/)) {
val = 'DataTypes.DATE';
if(self.options.typescript) tsTableDefAttr += `${spaces}${fieldName}:Date;\n`;
}
else if (_attr.match(/^(time)/)) {
val = 'DataTypes.TIME';
if(self.options.typescript) tsTableDefAttr += `${spaces}${fieldName}:any;\n`;
}
else if (_attr.match(/^(float|float4)/)) {
val = 'DataTypes.FLOAT';
if(self.options.typescript) tsTableDefAttr += `${spaces}${fieldName}:number;\n`;
}
else if (_attr.match(/^decimal/)) {
val = 'DataTypes.DECIMAL';
if(self.options.typescript) tsTableDefAttr += `${spaces}${fieldName}:number;\n`;
}
else if (_attr.match(/^(float8|double precision|numeric)/)) {
val = 'DataTypes.DOUBLE';
if(self.options.typescript) tsTableDefAttr += `${spaces}${fieldName}:number;\n`;
}
else if (_attr.match(/^uuid|uniqueidentifier/)) {
val = 'DataTypes.UUIDV4';
if(self.options.typescript) tsTableDefAttr += `${spaces}${fieldName}:string;\n`;
}
else if (_attr.match(/^jsonb/)) {
val = 'DataTypes.JSONB';
if(self.options.typescript) tsTableDefAttr += `${spaces}${fieldName}:any;\n`;
}
else if (_attr.match(/^json/)) {
val = 'DataTypes.JSON';
if(self.options.typescript) tsTableDefAttr += `${spaces}${fieldName}:any;\n`;
}
else if (_attr.match(/^geometry/)) {
val = 'DataTypes.GEOMETRY';
if(self.options.typescript) tsTableDefAttr += `${spaces}${fieldName}:any;\n`;
}
text[table] += spaces + spaces + spaces + attr + ": " + val;
if(self.options.typescript) tsVal = val;
}

text[table] += ",";
Expand All @@ -371,6 +358,9 @@ AutoSequelize.prototype.run = function(callback) {
text[table] += ",";
}
text[table] += "\n";

// typescript, get definition for this field
if(self.options.typescript) tsTableDefAttr += tsHelper.def.getDefinitionFileMember(spaces, fieldName, tsVal, tsAllowNull);
});

text[table] += spaces + "}";
Expand All @@ -390,12 +380,8 @@ AutoSequelize.prototype.run = function(callback) {
text[table] = text[table].substring(0, text[table].length - 1);
text[table] += "\n" + spaces + "}";

if(self.options.typescript){
typescriptDef += '// table: ' + tableName + '\n';
typescriptDef += tsTableDefAttr + '}\n';
typescriptDef += 'export interface ' + tableName + 'Instance extends Sequelize.Instance<' + tableName + 'Attribute>, ' + tableName + 'Attribute { }\n';
typescriptDef += 'export interface ' + tableName + 'Model extends Sequelize.Model<' + tableName + 'Instance, ' + tableName + 'Attribute> { }\n';
}
// typescript end table in definitions file
if(self.options.typescript) typescriptDefFile += tsHelper.def.getDefinitionFileTable(tsTableDefAttr, tableName);

function addAdditionalOption(value, key) {
if (key === 'name') {
Expand All @@ -418,23 +404,23 @@ AutoSequelize.prototype.run = function(callback) {
self.sequelize.close();

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

AutoSequelize.prototype.write = function(attributes, typescriptDef, callback) {
AutoSequelize.prototype.write = function(attributes, typescriptDefFile, callback) {
var tables = _.keys(attributes);
var self = this;

mkdirp.sync(path.resolve(self.options.directory));

async.each(tables, createFile, callback);

if(self.options.typescript && typescriptDef != null){
fs.writeFile(path.resolve(path.join(self.options.directory, 'db.d.ts')), typescriptDef, callback);
if(self.options.typescript && typescriptDefFile != null){
fs.writeFile(path.resolve(path.join(self.options.directory, 'db.d.ts')), typescriptDefFile, callback);
}

function createFile(table, _callback) {
Expand Down
69 changes: 69 additions & 0 deletions lib/ts-helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// put in seperate file to keep main sequelize-auto clean
'use strict';


function getModelFileStart(spaces, tableName) {
var s = 'import sequelize, { DataTypes } from \'sequelize\';\n';
s += 'import { ' + tableName + 'Instance, ' + tableName + 'Attribute } from \'./db.d\';\n\n';
s += "module.exports = function(sequelize:sequelize.Sequelize, DataTypes:DataTypes) {\n";
s += spaces + 'return sequelize.define<' + tableName + 'Instance, ' + tableName + 'Attribute>(\'' + tableName + '\', {\n';
return s;
}
exports.model = {
getModelFileStart
}



function getDefinitionFileStart() {
return 'import Sequelize from \'sequelize\';\n\n';
}

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

function getDefinitionFileMember(spaces, fieldName, val, allowNull) {
var m = '\n' + spaces + fieldName + (allowNull === true ? '?:' : ':');

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

return m + ';';
}

exports.def = {
getDefinitionFileStart,
getDefinitionFileTable,
getDefinitionFileMember
}

0 comments on commit fbadaa4

Please sign in to comment.