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 261b8a8 commit 619df08
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 14 deletions.
6 changes: 3 additions & 3 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,13 +152,13 @@ AutoSequelize.prototype.run = function(callback) {

var tableName = self.options.camelCase ? _.camelCase(table) : table;
var tsTableDef = self.options.typescript ? 'export interface ' + tableName + 'Attribute {' : '';
text[table] = "/* jshint indent: " + self.options.indentation + " */\n\n";


if(!self.options.typescript){
text[table] = "/* jshint indent: " + self.options.indentation + " */\n\n";
text[table] += "module.exports = function(sequelize, DataTypes) {\n";
text[table] += spaces + "return sequelize.define('" + tableName + "', {\n";
} else {
text[table] += tsHelper.model.getModelFileStart(spaces, tableName);
text[table] = tsHelper.model.getModelFileStart(self.options.indentation, spaces, tableName);
}

_.each(fields, function(field, i){
Expand Down
25 changes: 14 additions & 11 deletions lib/ts-helper.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
// 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;
function getModelFileStart(indentation, spaces, tableName) {
var fileStart = "/* jshint indent: " + indentation + " */\n";
fileStart += '// tslint:disable \n'
fileStart += 'import sequelize, { DataTypes } from \'sequelize\';\n';
fileStart += 'import { ' + tableName + 'Instance, ' + tableName + 'Attribute } from \'./db.d\';\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;
}

exports.model = {
Expand All @@ -18,13 +20,14 @@ function getDefinitionFileStart() {
}

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;
var tableDef = '// 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) {
var m = '\n' + spaces + fieldName + (allowNull === true ? '?:' : ':');

Expand Down

0 comments on commit 619df08

Please sign in to comment.