recognision of OneTwoMany relations

This commit is contained in:
Kononnable 2017-06-05 13:39:27 +02:00
parent 06158a50ec
commit 9a2076b3f1
6 changed files with 48 additions and 36 deletions

3
.gitignore vendored
View File

@ -8,4 +8,5 @@ typings/
output/**/*.*
.nyc_output/
coverage/
.env
.env
package-lock.json

View File

@ -33,6 +33,9 @@ export class Engine {
this.createTsConfigFile(resultPath)
this.createTypeOrm(resultPath)
let entitesPath = path.resolve(resultPath, './entities')
Handlebars.registerHelper('toLowerCase', function (str) {
return str.toLowerCase();
});
if (!fs.existsSync(entitesPath))
fs.mkdirSync(entitesPath);
let compliedTemplate = Handlebars.compile(template,{noEscape:true})

View File

@ -273,10 +273,10 @@ order by
let ownColumn: ColumnInfo = ownerColumn;
let isOneToMany: boolean;
isOneToMany = false;
let index = ownerEntity.Indexes.find(
let index = referencedEntity.Indexes.find(
(index) => {
return index.isUnique && index.columns.some(col => {
return col.name == ownColumn.name
return col.name == relatedColumn!.name
})
}
)
@ -285,38 +285,44 @@ order by
} else {
isOneToMany = false;
}
let ownerRelation=new RelationInfo()
ownerRelation.actionOnDelete= relationTmp.actionOnDelete
ownerRelation.actionOnUpdate= relationTmp.actionOnUpdate
ownerRelation.isOwner= true
ownerRelation.relatedColumn= relatedColumn.name.toLowerCase()
ownerRelation.relatedTable= relationTmp.referencedTable
ownerRelation.relationType= isOneToMany ? "OneToMany" : "OneToOne"
let ownerRelation = new RelationInfo()
ownerRelation.actionOnDelete = relationTmp.actionOnDelete
ownerRelation.actionOnUpdate = relationTmp.actionOnUpdate
ownerRelation.isOwner = true
ownerRelation.relatedColumn = relatedColumn.name.toLowerCase()
ownerRelation.relatedTable = relationTmp.referencedTable
ownerRelation.ownerTable = relationTmp.ownerTable
ownerRelation.ownerColumn = ownerEntity.EntityName.toLowerCase()
ownerRelation.relationType = isOneToMany ? "OneToMany" : "OneToOne"
ownerColumn.relations.push(ownerRelation)
if (isOneToMany) {
let col = new ColumnInfo()
col.name = ownerEntity.EntityName.toLowerCase() //+ 's'
let referencedRelation = new RelationInfo();
col.relations.push(referencedRelation)
referencedRelation.actionOnDelete= relationTmp.actionOnDelete
referencedRelation.actionOnUpdate= relationTmp.actionOnUpdate
referencedRelation.isOwner= false
referencedRelation.relatedColumn= ownerColumn.name
referencedRelation.relatedTable= relationTmp.ownerTable
referencedRelation.relationType= "ManyToOne"
referencedRelation.actionOnDelete = relationTmp.actionOnDelete
referencedRelation.actionOnUpdate = relationTmp.actionOnUpdate
referencedRelation.isOwner = false
referencedRelation.relatedColumn = ownerColumn.name
referencedRelation.relatedTable = relationTmp.ownerTable
referencedRelation.ownerTable = relationTmp.referencedTable
referencedRelation.ownerColumn = relatedColumn.name.toLowerCase()
referencedRelation.relationType = "ManyToOne"
referencedEntity.Columns.push(col)
} else {
let col = new ColumnInfo()
col.name = ownerEntity.EntityName.toLowerCase()
let referencedRelation = new RelationInfo();
col.relations.push(referencedRelation)
referencedRelation.actionOnDelete= relationTmp.actionOnDelete
referencedRelation.actionOnUpdate= relationTmp.actionOnUpdate
referencedRelation.isOwner= false
referencedRelation.relatedColumn= ownerColumn.name
referencedRelation.relatedTable= relationTmp.ownerTable
referencedRelation.relationType= "OneToOne"
referencedRelation.actionOnDelete = relationTmp.actionOnDelete
referencedRelation.actionOnUpdate = relationTmp.actionOnUpdate
referencedRelation.isOwner = false
referencedRelation.relatedColumn = ownerColumn.name
referencedRelation.relatedTable = relationTmp.ownerTable
referencedRelation.ownerTable = relationTmp.referencedTable
referencedRelation.ownerColumn = relatedColumn.name.toLowerCase()
referencedRelation.relationType = "OneToOne"
referencedEntity.Columns.push(col)
}
})

View File

@ -19,7 +19,7 @@ import {Index,Entity, PrimaryColumn, Column, OneToOne, OneToMany, ManyToOne, Joi
})
{{name}}:{{ts_type}};
{{/relations}}{{#relations}}
@{{relationType}}(type=>{{relatedTable}},x=>x.{{relatedColumn}}){{#isOwner}}
@{{relationType}}(type=>{{relatedTable}}, {{../name}}=>{{../name}}.{{#if isOwner}}{{ownerColumn}}{{else}}{{relatedColumn}}{{/if}}){{#isOwner}}
@JoinColumn(){{/isOwner}}
{{#if isOneToMany}}{{../name}}:{{relatedTable}}[];
{{else}}{{../name}}:{{relatedTable}};

View File

@ -3,6 +3,8 @@ export class RelationInfo {
relationType: "OneToOne" | "OneToMany" | "ManyToOne"
relatedTable: string
relatedColumn: string
ownerTable: string
ownerColumn: string
actionOnDelete: "RESTRICT" | "CASCADE" | "SET NULL"
actionOnUpdate: "RESTRICT" | "CASCADE" | "SET NULL"

View File

@ -5,9 +5,9 @@ export class EntityFileToJson {
if (decoratorParameters.length > 0) {
if (decoratorParameters.search(',') > 0) {
col.columnTypes = decoratorParameters.substring(0, decoratorParameters.indexOf(',')).trim().split('|').map(function (x) {
if (!x.endsWith('[]')) {
x = x + '[]'// can't distinguish OneTwoMany from OneToOne without indexes
}
// if (!x.endsWith('[]')) {
// x = x + '[]'// can't distinguish OneTwoMany from OneToOne without indexes
// }
return x;
});
let badJSON = decoratorParameters.substring(decoratorParameters.indexOf(',') + 1).trim()
@ -18,9 +18,9 @@ export class EntityFileToJson {
} else {
if (decoratorParameters[0] == '"' && decoratorParameters.endsWith('"')) {
col.columnTypes = decoratorParameters.split('|').map(function (x) {
if (!x.endsWith('[]')) {
x = x + '[]'// can't distinguish OneTwoMany from OneToOne without indexes
}
// if (!x.endsWith('[]')) {
// x = x + '[]'// can't distinguish OneTwoMany from OneToOne without indexes
// }
return x;
});
} else {
@ -112,7 +112,7 @@ export class EntityFileToJson {
isMultilineStatement = false;
let column = new EntityColumn()
retVal.columns.push(column)
column.relationType = "OneToMany"//"ManyToOne" - can't distinguish OneTwoMany from OneToOne without indexes
column.relationType = "ManyToOne"
continue;
}
} else if (trimmedLine.startsWith('@OneToMany')) {
@ -136,7 +136,7 @@ export class EntityFileToJson {
isMultilineStatement = false;
let column = new EntityColumn()
retVal.columns.push(column)
column.relationType = "OneToMany"//"OneToOne" - can't distinguish OneTwoMany from OneToOne without indexes
column.relationType = "OneToOne"
continue;
}
} else if (trimmedLine.startsWith('@JoinColumn')) {
@ -153,15 +153,15 @@ export class EntityFileToJson {
retVal.columns[retVal.columns.length - 1].columnName = trimmedLine.split(':')[0].trim();
//TODO:Should check if null only column is nullable?
retVal.columns[retVal.columns.length - 1].columnTypes = trimmedLine.split(':')[1].split(';')[0].trim().split('|').map(function (x) {
if (!x.endsWith('[]')) {
x = x + '[]'// can't distinguish OneTwoMany from OneToOne without indexes
}
// if (!x.endsWith('[]')) {
// x = x + '[]'// can't distinguish OneTwoMany from OneToOne without indexes
// }
return x;
});
if (!retVal.columns[retVal.columns.length - 1].columnTypes.some(function (this, val, ind, arr) {
return val == "null" ? true : false;
})) retVal.columns[retVal.columns.length - 1].columnTypes.push('null[]')
})) retVal.columns[retVal.columns.length - 1].columnTypes.push('null')
continue
} else if (trimmedLine = '}') {