forked from sequelize/sequelize-auto
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request sequelize#179 from isac322/fix_typescript
Fix typescript, add missing option
- Loading branch information
Showing
3 changed files
with
103 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; |