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 8e7c1ad commit 1e60353
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 12 deletions.
4 changes: 2 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ AutoSequelize.prototype.run = function(callback) {
text[table] += "\n";

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

text[table] += spaces + "}";
Expand All @@ -381,7 +381,7 @@ AutoSequelize.prototype.run = function(callback) {
text[table] += "\n" + spaces + "}";

// typescript end table in definitions file
if(self.options.typescript) typescriptDefFile += tsHelper.def.getDefinitionFileTable(tsTableDef, tableName);
if(self.options.typescript) typescriptDefFile += tsHelper.def.getTableDefinition(tsTableDef, tableName);

function addAdditionalOption(value, key) {
if (key === 'name') {
Expand Down
17 changes: 7 additions & 10 deletions lib/ts-helper.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,31 @@
// 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) {
function getTableDefinition(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) {
function getMemberDefinition(spaces, fieldName, val, allowNull) {
var m = '\n' + spaces + fieldName + (allowNull === true ? '?:' : ':');

if(val.indexOf('DataTypes.BOOLEAN') > 0){
Expand Down Expand Up @@ -63,7 +61,6 @@ function getDefinitionFileMember(spaces, fieldName, val, allowNull) {

exports.def = {
getDefinitionFileStart,
getDefinitionFileTable,
getDefinitionFileMember
}

getTableDefinition,
getMemberDefinition
}

0 comments on commit 1e60353

Please sign in to comment.