Skip to content

Commit

Permalink
match code format of ts-help.js with other
Browse files Browse the repository at this point in the history
  • Loading branch information
isac322 committed Jul 31, 2017
1 parent 48a3706 commit 0743798
Showing 1 changed file with 91 additions and 91 deletions.
182 changes: 91 additions & 91 deletions lib/ts-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,116 +5,116 @@ 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, isCamelCase, isCamelCaseForFile) {
var spaces = '';
for (var i = 0; i < indentation; ++ i) {
spaces += (isSpaces === true ? ' ' : "\t");
var spaces = '';
for (var i = 0; i < indentation; ++i) {
spaces += (isSpaces === true ? ' ' : "\t");
}

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 generateInterface() {
var fileTextOutput = 'export interface ITables {\n';
for (var i = 0; i < tableNames.length; i++) {
var table = isCamelCase ? _.camelCase(tableNames[i]) : tableNames[i];

fileTextOutput += spaces + table + ':def.' + table + 'Model;\n';
}

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 generateInterface() {
var fileTextOutput = 'export interface ITables {\n';
for (var i = 0; i < tableNames.length; i++) {
var table = isCamelCase ? _.camelCase(tableNames[i]) : tableNames[i];

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++) {
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;
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++) {
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';
}

return m + ';';
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 + ';';
}

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

0 comments on commit 0743798

Please sign in to comment.