midway-model-gen 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.idea/codeStyles/Project.xml +57 -0
- package/.idea/codeStyles/codeStyleConfig.xml +5 -0
- package/.idea/inspectionProfiles/Project_Default.xml +6 -0
- package/.idea/modules.xml +8 -0
- package/.idea/prettier.xml +6 -0
- package/.idea/test.iml +8 -0
- package/.idea/vcs.xml +6 -0
- package/CHANGELOG.md +161 -0
- package/CONTRIBUTING.md +51 -0
- package/DEVELOPER.md +30 -0
- package/LICENSE +21 -0
- package/README.md +94 -0
- package/USECASES.md +53 -0
- package/bin/typeorm-model-generator +6 -0
- package/dist/package.json +106 -0
- package/dist/src/Engine.d.ts +7 -0
- package/dist/src/Engine.js +47 -0
- package/dist/src/IConnectionOptions.d.ts +14 -0
- package/dist/src/IConnectionOptions.js +22 -0
- package/dist/src/IGenerationOptions.d.ts +24 -0
- package/dist/src/IGenerationOptions.js +33 -0
- package/dist/src/ModelCustomization.d.ts +4 -0
- package/dist/src/ModelCustomization.js +256 -0
- package/dist/src/ModelGeneration.d.ts +4 -0
- package/dist/src/ModelGeneration.js +247 -0
- package/dist/src/NamingStrategy.d.ts +10 -0
- package/dist/src/NamingStrategy.js +61 -0
- package/dist/src/Utils.d.ts +6 -0
- package/dist/src/Utils.js +68 -0
- package/dist/src/drivers/AbstractDriver.d.ts +30 -0
- package/dist/src/drivers/AbstractDriver.js +258 -0
- package/dist/src/drivers/MariaDbDriver.d.ts +4 -0
- package/dist/src/drivers/MariaDbDriver.js +11 -0
- package/dist/src/drivers/MssqlDriver.d.ts +25 -0
- package/dist/src/drivers/MssqlDriver.js +408 -0
- package/dist/src/drivers/MysqlDriver.d.ts +27 -0
- package/dist/src/drivers/MysqlDriver.js +439 -0
- package/dist/src/drivers/OracleDriver.d.ts +25 -0
- package/dist/src/drivers/OracleDriver.js +316 -0
- package/dist/src/drivers/PostgresDriver.d.ts +31 -0
- package/dist/src/drivers/PostgresDriver.js +542 -0
- package/dist/src/drivers/SqliteDriver.d.ts +28 -0
- package/dist/src/drivers/SqliteDriver.js +308 -0
- package/dist/src/index.d.ts +1 -0
- package/dist/src/index.js +667 -0
- package/dist/src/library.d.ts +11 -0
- package/dist/src/library.js +14 -0
- package/dist/src/models/Column.d.ts +24 -0
- package/dist/src/models/Column.js +3 -0
- package/dist/src/models/Entity.d.ts +21 -0
- package/dist/src/models/Entity.js +3 -0
- package/dist/src/models/Index.d.ts +9 -0
- package/dist/src/models/Index.js +3 -0
- package/dist/src/models/Relation.d.ts +11 -0
- package/dist/src/models/Relation.js +3 -0
- package/dist/src/models/RelationId.d.ts +5 -0
- package/dist/src/models/RelationId.js +3 -0
- package/dist/src/models/RelationInternal.d.ts +11 -0
- package/dist/src/models/RelationInternal.js +3 -0
- package/dist/src/templates/entity.mst +47 -0
- package/dist/src/templates/index.mst +5 -0
- package/dist/src/templates/ormconfig.mst +17 -0
- package/dist/src/templates/tsconfig.mst +10 -0
- package/npminstall-debug.log +175 -0
- package/package.json +106 -0
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.fileName = exports.columnName = exports.entityName = exports.relationName = exports.relationIdName = exports.enablePluralization = void 0;
|
|
4
|
+
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
5
|
+
const pluralize_1 = require("pluralize");
|
|
6
|
+
let pluralize;
|
|
7
|
+
function enablePluralization(value) {
|
|
8
|
+
pluralize = value;
|
|
9
|
+
}
|
|
10
|
+
exports.enablePluralization = enablePluralization;
|
|
11
|
+
function relationIdName(relationId, relation, owner) {
|
|
12
|
+
const columnOldName = relationId.fieldName;
|
|
13
|
+
const isRelationToMany = relation.relationType === "OneToMany" ||
|
|
14
|
+
relation.relationType === "ManyToMany";
|
|
15
|
+
let newColumnName = columnOldName.replace(/[0-9]$/, "");
|
|
16
|
+
if (!Number.isNaN(parseInt(newColumnName[newColumnName.length - 1], 10))) {
|
|
17
|
+
newColumnName = newColumnName.substring(0, newColumnName.length - 1);
|
|
18
|
+
}
|
|
19
|
+
if (!Number.isNaN(parseInt(newColumnName[newColumnName.length - 1], 10))) {
|
|
20
|
+
newColumnName = newColumnName.substring(0, newColumnName.length - 1);
|
|
21
|
+
}
|
|
22
|
+
if (isRelationToMany && pluralize) {
|
|
23
|
+
newColumnName = (0, pluralize_1.plural)(newColumnName);
|
|
24
|
+
}
|
|
25
|
+
return newColumnName;
|
|
26
|
+
}
|
|
27
|
+
exports.relationIdName = relationIdName;
|
|
28
|
+
function relationName(relation, owner) {
|
|
29
|
+
const columnOldName = relation.fieldName;
|
|
30
|
+
const isRelationToMany = relation.relationType === "OneToMany" ||
|
|
31
|
+
relation.relationType === "ManyToMany";
|
|
32
|
+
let newColumnName = columnOldName.replace(/[0-9]$/, "");
|
|
33
|
+
if (newColumnName.toLowerCase().endsWith("id") &&
|
|
34
|
+
!newColumnName.toLowerCase().endsWith("guid")) {
|
|
35
|
+
newColumnName = newColumnName.substring(0, newColumnName.toLowerCase().lastIndexOf("id"));
|
|
36
|
+
}
|
|
37
|
+
if (!Number.isNaN(parseInt(newColumnName[newColumnName.length - 1], 10))) {
|
|
38
|
+
newColumnName = newColumnName.substring(0, newColumnName.length - 1);
|
|
39
|
+
}
|
|
40
|
+
if (!Number.isNaN(parseInt(newColumnName[newColumnName.length - 1], 10))) {
|
|
41
|
+
newColumnName = newColumnName.substring(0, newColumnName.length - 1);
|
|
42
|
+
}
|
|
43
|
+
if (isRelationToMany && pluralize) {
|
|
44
|
+
newColumnName = (0, pluralize_1.plural)(newColumnName);
|
|
45
|
+
}
|
|
46
|
+
return newColumnName;
|
|
47
|
+
}
|
|
48
|
+
exports.relationName = relationName;
|
|
49
|
+
function entityName(oldEntityName, entity) {
|
|
50
|
+
return oldEntityName;
|
|
51
|
+
}
|
|
52
|
+
exports.entityName = entityName;
|
|
53
|
+
function columnName(oldColumnName, column) {
|
|
54
|
+
return oldColumnName;
|
|
55
|
+
}
|
|
56
|
+
exports.columnName = columnName;
|
|
57
|
+
function fileName(oldFileName) {
|
|
58
|
+
return oldFileName;
|
|
59
|
+
}
|
|
60
|
+
exports.fileName = fileName;
|
|
61
|
+
//# sourceMappingURL=NamingStrategy.js.map
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { Entity } from "./models/Entity";
|
|
2
|
+
export declare function LogError(errText: string, isABug?: boolean, passedError?: string | ErrorConstructor): void;
|
|
3
|
+
export declare function packageVersion(): string;
|
|
4
|
+
export declare function findNameForNewField(_fieldName: string, entity: Entity, columnOldName?: string): string;
|
|
5
|
+
export declare function requireLocalFile(fileName: string): any;
|
|
6
|
+
export declare function assertUnreachable(x: never): never;
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.assertUnreachable = exports.requireLocalFile = exports.findNameForNewField = exports.packageVersion = exports.LogError = void 0;
|
|
4
|
+
const changeCase = require("change-case");
|
|
5
|
+
const path = require("path");
|
|
6
|
+
const packagejson = require("../package.json");
|
|
7
|
+
function LogError(errText, isABug = true, passedError) {
|
|
8
|
+
let errObject = passedError;
|
|
9
|
+
console.error(errText);
|
|
10
|
+
console.error(`Error occurred in typeorm-model-generator.`);
|
|
11
|
+
console.error(`${packageVersion()} node@${process.version}`);
|
|
12
|
+
console.error(`If you think this is a bug please open an issue including this log on ${packagejson.bugs.url}`);
|
|
13
|
+
if (isABug && !passedError) {
|
|
14
|
+
errObject = new Error().stack;
|
|
15
|
+
}
|
|
16
|
+
if (errObject) {
|
|
17
|
+
console.error(errObject);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
exports.LogError = LogError;
|
|
21
|
+
function packageVersion() {
|
|
22
|
+
return `${packagejson.name}@${packagejson.version}`;
|
|
23
|
+
}
|
|
24
|
+
exports.packageVersion = packageVersion;
|
|
25
|
+
function findNameForNewField(_fieldName, entity, columnOldName = "") {
|
|
26
|
+
let fieldName = _fieldName;
|
|
27
|
+
const validNameCondition = () => (entity.columns.every((v) => changeCase.camelCase(v.tscName) !==
|
|
28
|
+
changeCase.camelCase(fieldName)) &&
|
|
29
|
+
entity.relations.every((v) => changeCase.camelCase(v.fieldName) !==
|
|
30
|
+
changeCase.camelCase(fieldName)) &&
|
|
31
|
+
entity.relationIds.every((v) => changeCase.camelCase(v.fieldName) !==
|
|
32
|
+
changeCase.camelCase(fieldName))) ||
|
|
33
|
+
(columnOldName &&
|
|
34
|
+
changeCase.camelCase(columnOldName) ===
|
|
35
|
+
changeCase.camelCase(fieldName));
|
|
36
|
+
if (!validNameCondition()) {
|
|
37
|
+
fieldName += "_";
|
|
38
|
+
for (let i = 2; i <= entity.columns.length + entity.relations.length; i++) {
|
|
39
|
+
fieldName =
|
|
40
|
+
fieldName.substring(0, fieldName.length - i.toString().length) +
|
|
41
|
+
i.toString();
|
|
42
|
+
if (validNameCondition()) {
|
|
43
|
+
break;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
return fieldName;
|
|
48
|
+
}
|
|
49
|
+
exports.findNameForNewField = findNameForNewField;
|
|
50
|
+
function requireLocalFile(fileName) {
|
|
51
|
+
try {
|
|
52
|
+
// eslint-disable-next-line global-require, import/no-dynamic-require, @typescript-eslint/no-var-requires
|
|
53
|
+
return require(fileName);
|
|
54
|
+
}
|
|
55
|
+
catch (err) {
|
|
56
|
+
if (!path.isAbsolute(fileName)) {
|
|
57
|
+
// eslint-disable-next-line global-require, import/no-dynamic-require, @typescript-eslint/no-var-requires
|
|
58
|
+
return require(path.resolve(process.cwd(), fileName));
|
|
59
|
+
}
|
|
60
|
+
throw err;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
exports.requireLocalFile = requireLocalFile;
|
|
64
|
+
function assertUnreachable(x) {
|
|
65
|
+
throw new Error("Didn't expect to get here");
|
|
66
|
+
}
|
|
67
|
+
exports.assertUnreachable = assertUnreachable;
|
|
68
|
+
//# sourceMappingURL=Utils.js.map
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { WithLengthColumnType, WithPrecisionColumnType, WithWidthColumnType } from "typeorm/driver/types/ColumnTypes";
|
|
2
|
+
import { DataTypeDefaults } from "typeorm/driver/types/DataTypeDefaults";
|
|
3
|
+
import IConnectionOptions from "../IConnectionOptions";
|
|
4
|
+
import { Entity } from "../models/Entity";
|
|
5
|
+
import { RelationInternal } from "../models/RelationInternal";
|
|
6
|
+
import IGenerationOptions from "../IGenerationOptions";
|
|
7
|
+
export default abstract class AbstractDriver {
|
|
8
|
+
abstract standardPort: number;
|
|
9
|
+
abstract standardSchema: string;
|
|
10
|
+
abstract standardUser: string;
|
|
11
|
+
abstract defaultValues: DataTypeDefaults;
|
|
12
|
+
ColumnTypesWithWidth: WithWidthColumnType[];
|
|
13
|
+
ColumnTypesWithPrecision: WithPrecisionColumnType[];
|
|
14
|
+
ColumnTypesWithLength: WithLengthColumnType[];
|
|
15
|
+
static FindManyToManyRelations(dbModel: Entity[]): Entity[];
|
|
16
|
+
GetDataFromServer(connectionOptions: IConnectionOptions, generationOptions: IGenerationOptions): Promise<Entity[]>;
|
|
17
|
+
static FilterGeneratedTables(dbModel: Entity[], skipTables: string[], onlyTables: string[]): Entity[];
|
|
18
|
+
abstract ConnectToServer(connectionOptons: IConnectionOptions): Promise<void>;
|
|
19
|
+
abstract GetAllTables(schemas: string[], dbNames: string[]): Promise<Entity[]>;
|
|
20
|
+
static GetRelationsFromRelationTempInfo(relationsTemp: RelationInternal[], entities: Entity[], generationOptions: IGenerationOptions): Entity[];
|
|
21
|
+
abstract GetCoulmnsFromEntity(entities: Entity[], schemas: string[], dbNames: string[]): Promise<Entity[]>;
|
|
22
|
+
abstract GetIndexesFromEntity(entities: Entity[], schemas: string[], dbNames: string[]): Promise<Entity[]>;
|
|
23
|
+
abstract GetRelations(entities: Entity[], schemas: string[], dbNames: string[], generationOptions: IGenerationOptions): Promise<Entity[]>;
|
|
24
|
+
static FindPrimaryColumnsFromIndexes(dbModel: Entity[]): void;
|
|
25
|
+
abstract DisconnectFromServer(): Promise<void>;
|
|
26
|
+
abstract CreateDB(dbName: string): Promise<void>;
|
|
27
|
+
abstract DropDB(dbName: string): Promise<void>;
|
|
28
|
+
abstract CheckIfDBExists(dbName: string): Promise<boolean>;
|
|
29
|
+
protected static buildEscapedObjectList(dbNames: string[]): string;
|
|
30
|
+
}
|
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const TomgUtils = require("../Utils");
|
|
4
|
+
class AbstractDriver {
|
|
5
|
+
constructor() {
|
|
6
|
+
this.ColumnTypesWithWidth = [
|
|
7
|
+
"tinyint",
|
|
8
|
+
"smallint",
|
|
9
|
+
"mediumint",
|
|
10
|
+
"int",
|
|
11
|
+
"bigint",
|
|
12
|
+
];
|
|
13
|
+
this.ColumnTypesWithPrecision = [
|
|
14
|
+
"float",
|
|
15
|
+
"double",
|
|
16
|
+
"dec",
|
|
17
|
+
"decimal",
|
|
18
|
+
"numeric",
|
|
19
|
+
"real",
|
|
20
|
+
"double precision",
|
|
21
|
+
"number",
|
|
22
|
+
"datetime",
|
|
23
|
+
"datetime2",
|
|
24
|
+
"datetimeoffset",
|
|
25
|
+
"time",
|
|
26
|
+
"time with time zone",
|
|
27
|
+
"time without time zone",
|
|
28
|
+
"timestamp",
|
|
29
|
+
"timestamp without time zone",
|
|
30
|
+
"timestamp with time zone",
|
|
31
|
+
"timestamp with local time zone",
|
|
32
|
+
];
|
|
33
|
+
this.ColumnTypesWithLength = [
|
|
34
|
+
"character varying",
|
|
35
|
+
"varying character",
|
|
36
|
+
"nvarchar",
|
|
37
|
+
"character",
|
|
38
|
+
"native character",
|
|
39
|
+
"varchar",
|
|
40
|
+
"char",
|
|
41
|
+
"nchar",
|
|
42
|
+
"varchar2",
|
|
43
|
+
"nvarchar2",
|
|
44
|
+
"raw",
|
|
45
|
+
"binary",
|
|
46
|
+
"varbinary",
|
|
47
|
+
];
|
|
48
|
+
}
|
|
49
|
+
static FindManyToManyRelations(dbModel) {
|
|
50
|
+
let retVal = dbModel;
|
|
51
|
+
const manyToManyEntities = retVal.filter((entity) => entity.relations.length === 2 &&
|
|
52
|
+
entity.relations.every((v) => v.joinColumnOptions && v.relationType !== "ManyToMany") &&
|
|
53
|
+
entity.relations[0].relatedTable !==
|
|
54
|
+
entity.relations[1].relatedTable &&
|
|
55
|
+
entity.relations[0].joinColumnOptions.length ===
|
|
56
|
+
entity.relations[1].joinColumnOptions.length &&
|
|
57
|
+
entity.columns.length ===
|
|
58
|
+
entity.columns.filter((c) => c.primary).length &&
|
|
59
|
+
entity.columns
|
|
60
|
+
.map((v) => v.tscName)
|
|
61
|
+
.filter((v) => !entity.relations[0]
|
|
62
|
+
.joinColumnOptions.map((x) => x.name)
|
|
63
|
+
.some((jc) => jc === v) &&
|
|
64
|
+
!entity.relations[1]
|
|
65
|
+
.joinColumnOptions.map((x) => x.name)
|
|
66
|
+
.some((jc) => jc === v)).length === 0);
|
|
67
|
+
manyToManyEntities.forEach((junctionEntity) => {
|
|
68
|
+
const firstEntity = dbModel.find((v) => v.tscName === junctionEntity.relations[0].relatedTable);
|
|
69
|
+
const secondEntity = dbModel.find((v) => v.tscName === junctionEntity.relations[1].relatedTable);
|
|
70
|
+
const firstRelation = firstEntity.relations.find((v) => v.relatedTable === junctionEntity.tscName);
|
|
71
|
+
const secondRelation = secondEntity.relations.find((v) => v.relatedTable === junctionEntity.tscName);
|
|
72
|
+
firstRelation.relationType = "ManyToMany";
|
|
73
|
+
secondRelation.relationType = "ManyToMany";
|
|
74
|
+
firstRelation.relatedTable = secondEntity.tscName;
|
|
75
|
+
secondRelation.relatedTable = firstEntity.tscName;
|
|
76
|
+
firstRelation.fieldName = TomgUtils.findNameForNewField(secondEntity.tscName, firstEntity);
|
|
77
|
+
secondRelation.fieldName = TomgUtils.findNameForNewField(firstEntity.tscName, secondEntity);
|
|
78
|
+
firstRelation.relatedField = secondRelation.fieldName;
|
|
79
|
+
secondRelation.relatedField = firstRelation.fieldName;
|
|
80
|
+
firstRelation.joinTableOptions = {
|
|
81
|
+
name: junctionEntity.sqlName,
|
|
82
|
+
joinColumns: junctionEntity.relations[0].joinColumnOptions.map((v, i) => {
|
|
83
|
+
return {
|
|
84
|
+
referencedColumnName: v.referencedColumnName,
|
|
85
|
+
name: junctionEntity.relations[0]
|
|
86
|
+
.joinColumnOptions[i].name,
|
|
87
|
+
};
|
|
88
|
+
}),
|
|
89
|
+
inverseJoinColumns: junctionEntity.relations[1].joinColumnOptions.map((v, i) => {
|
|
90
|
+
return {
|
|
91
|
+
referencedColumnName: v.referencedColumnName,
|
|
92
|
+
name: junctionEntity.relations[1]
|
|
93
|
+
.joinColumnOptions[i].name,
|
|
94
|
+
};
|
|
95
|
+
}),
|
|
96
|
+
};
|
|
97
|
+
if (junctionEntity.database) {
|
|
98
|
+
firstRelation.joinTableOptions.database =
|
|
99
|
+
junctionEntity.database;
|
|
100
|
+
}
|
|
101
|
+
if (junctionEntity.schema) {
|
|
102
|
+
firstRelation.joinTableOptions.schema = junctionEntity.schema;
|
|
103
|
+
}
|
|
104
|
+
firstRelation.relationOptions = undefined;
|
|
105
|
+
secondRelation.relationOptions = undefined;
|
|
106
|
+
firstRelation.joinColumnOptions = undefined;
|
|
107
|
+
secondRelation.joinColumnOptions = undefined;
|
|
108
|
+
retVal = retVal.filter((ent) => {
|
|
109
|
+
return ent.tscName !== junctionEntity.tscName;
|
|
110
|
+
});
|
|
111
|
+
});
|
|
112
|
+
return retVal;
|
|
113
|
+
}
|
|
114
|
+
async GetDataFromServer(connectionOptions, generationOptions) {
|
|
115
|
+
let dbModel = [];
|
|
116
|
+
await this.ConnectToServer(connectionOptions);
|
|
117
|
+
dbModel = await this.GetAllTables(connectionOptions.schemaNames, connectionOptions.databaseNames);
|
|
118
|
+
await this.GetCoulmnsFromEntity(dbModel, connectionOptions.schemaNames, connectionOptions.databaseNames);
|
|
119
|
+
await this.GetIndexesFromEntity(dbModel, connectionOptions.schemaNames, connectionOptions.databaseNames);
|
|
120
|
+
AbstractDriver.FindPrimaryColumnsFromIndexes(dbModel);
|
|
121
|
+
dbModel = await this.GetRelations(dbModel, connectionOptions.schemaNames, connectionOptions.databaseNames, generationOptions);
|
|
122
|
+
await this.DisconnectFromServer();
|
|
123
|
+
dbModel = AbstractDriver.FindManyToManyRelations(dbModel);
|
|
124
|
+
dbModel = AbstractDriver.FilterGeneratedTables(dbModel, connectionOptions.skipTables, connectionOptions.onlyTables);
|
|
125
|
+
return dbModel;
|
|
126
|
+
}
|
|
127
|
+
static FilterGeneratedTables(dbModel, skipTables, onlyTables) {
|
|
128
|
+
return dbModel
|
|
129
|
+
.filter((table) => !skipTables.includes(table.sqlName))
|
|
130
|
+
.filter((table) => onlyTables.length === 0 ||
|
|
131
|
+
onlyTables.includes(table.sqlName));
|
|
132
|
+
}
|
|
133
|
+
static GetRelationsFromRelationTempInfo(relationsTemp, entities, generationOptions) {
|
|
134
|
+
relationsTemp.forEach((relationTmp) => {
|
|
135
|
+
const ownerEntity = entities.find((entity) => entity.tscName === relationTmp.ownerTable.tscName);
|
|
136
|
+
if (!ownerEntity) {
|
|
137
|
+
TomgUtils.LogError(`Relation between tables ${relationTmp.ownerTable.sqlName} and ${relationTmp.relatedTable.sqlName} didn't found entity model ${relationTmp.ownerTable.sqlName}.`);
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
const referencedEntity = entities.find((entity) => entity.tscName === relationTmp.relatedTable.tscName);
|
|
141
|
+
if (!referencedEntity) {
|
|
142
|
+
TomgUtils.LogError(`Relation between tables ${relationTmp.ownerTable.sqlName} and ${relationTmp.relatedTable.sqlName} didn't found entity model ${relationTmp.relatedTable.sqlName}.`);
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
const ownerColumns = [];
|
|
146
|
+
const relatedColumns = [];
|
|
147
|
+
for (let relationColumnIndex = 0; relationColumnIndex < relationTmp.ownerColumns.length; relationColumnIndex++) {
|
|
148
|
+
const ownerColumn = ownerEntity.columns.find((column) => column.tscName ===
|
|
149
|
+
relationTmp.ownerColumns[relationColumnIndex]);
|
|
150
|
+
if (!ownerColumn) {
|
|
151
|
+
TomgUtils.LogError(`Relation between tables ${relationTmp.ownerTable.sqlName} and ${relationTmp.relatedTable.sqlName} didn't found entity column ${relationTmp.ownerTable.sqlName}.${ownerColumn}.`);
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
const relatedColumn = referencedEntity.columns.find((column) => column.tscName ===
|
|
155
|
+
relationTmp.relatedColumns[relationColumnIndex]);
|
|
156
|
+
if (!relatedColumn) {
|
|
157
|
+
TomgUtils.LogError(`Relation between tables ${relationTmp.ownerTable.sqlName} and ${relationTmp.relatedTable.sqlName} didn't found entity column ${relationTmp.relatedTable.sqlName}.${relatedColumn}.`);
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
ownerColumns.push(ownerColumn);
|
|
161
|
+
relatedColumns.push(relatedColumn);
|
|
162
|
+
}
|
|
163
|
+
let isOneToMany;
|
|
164
|
+
isOneToMany = false;
|
|
165
|
+
const index = ownerEntity.indices.find((ind) => ind.options.unique &&
|
|
166
|
+
ind.columns.length === ownerColumns.length &&
|
|
167
|
+
ownerColumns.every((ownerColumn) => ind.columns.some((col) => col === ownerColumn.tscName)));
|
|
168
|
+
isOneToMany = !index;
|
|
169
|
+
ownerColumns.forEach((column) => {
|
|
170
|
+
column.isUsedInRelationAsOwner = true;
|
|
171
|
+
});
|
|
172
|
+
relatedColumns.forEach((column) => {
|
|
173
|
+
column.isUsedInRelationAsReferenced = true;
|
|
174
|
+
});
|
|
175
|
+
let fieldName = "";
|
|
176
|
+
if (ownerColumns.length === 1) {
|
|
177
|
+
fieldName = TomgUtils.findNameForNewField(ownerColumns[0].tscName, ownerEntity);
|
|
178
|
+
}
|
|
179
|
+
else {
|
|
180
|
+
fieldName = TomgUtils.findNameForNewField(relationTmp.relatedTable.tscName, ownerEntity);
|
|
181
|
+
}
|
|
182
|
+
const relationOptions = {
|
|
183
|
+
onDelete: relationTmp.onDelete,
|
|
184
|
+
onUpdate: relationTmp.onUpdate,
|
|
185
|
+
};
|
|
186
|
+
const ownerRelation = {
|
|
187
|
+
fieldName,
|
|
188
|
+
relatedField: TomgUtils.findNameForNewField(relationTmp.ownerTable.tscName, relationTmp.relatedTable),
|
|
189
|
+
joinColumnOptions: relationTmp.ownerColumns.map((v, idx) => {
|
|
190
|
+
const retVal = {
|
|
191
|
+
name: v,
|
|
192
|
+
referencedColumnName: relationTmp.relatedColumns[idx],
|
|
193
|
+
};
|
|
194
|
+
return retVal;
|
|
195
|
+
}),
|
|
196
|
+
relatedTable: relationTmp.relatedTable.tscName,
|
|
197
|
+
relationType: isOneToMany ? "ManyToOne" : "OneToOne",
|
|
198
|
+
};
|
|
199
|
+
if (JSON.stringify(relationOptions) !== "{}") {
|
|
200
|
+
ownerRelation.relationOptions = relationOptions;
|
|
201
|
+
}
|
|
202
|
+
const relatedRelation = {
|
|
203
|
+
fieldName: ownerRelation.relatedField,
|
|
204
|
+
relatedField: ownerRelation.fieldName,
|
|
205
|
+
relatedTable: relationTmp.ownerTable.tscName,
|
|
206
|
+
relationType: isOneToMany ? "OneToMany" : "OneToOne",
|
|
207
|
+
};
|
|
208
|
+
ownerEntity.relations.push(ownerRelation);
|
|
209
|
+
relationTmp.relatedTable.relations.push(relatedRelation);
|
|
210
|
+
if (generationOptions.relationIds && ownerColumns.length === 1) {
|
|
211
|
+
let relationIdFieldName = "";
|
|
212
|
+
relationIdFieldName = TomgUtils.findNameForNewField(ownerColumns[0].tscName, ownerEntity);
|
|
213
|
+
let fieldType = "";
|
|
214
|
+
if (ownerRelation.relationType === "OneToMany") {
|
|
215
|
+
fieldType = `${ownerColumns[0].tscType}[]`;
|
|
216
|
+
}
|
|
217
|
+
else {
|
|
218
|
+
fieldType = ownerColumns[0].tscType;
|
|
219
|
+
if (ownerColumns[0].options.nullable) {
|
|
220
|
+
fieldType += " | null";
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
ownerEntity.relationIds.push({
|
|
224
|
+
fieldName: relationIdFieldName,
|
|
225
|
+
fieldType,
|
|
226
|
+
relationField: ownerRelation.fieldName,
|
|
227
|
+
});
|
|
228
|
+
// TODO: RelationId on ManyToMany
|
|
229
|
+
}
|
|
230
|
+
});
|
|
231
|
+
return entities;
|
|
232
|
+
}
|
|
233
|
+
static FindPrimaryColumnsFromIndexes(dbModel) {
|
|
234
|
+
dbModel.forEach((entity) => {
|
|
235
|
+
const primaryIndex = entity.indices.find((v) => v.primary);
|
|
236
|
+
entity.columns
|
|
237
|
+
.filter((col) => primaryIndex &&
|
|
238
|
+
primaryIndex.columns.some((cIndex) => cIndex === col.tscName))
|
|
239
|
+
.forEach((col) => {
|
|
240
|
+
// eslint-disable-next-line no-param-reassign
|
|
241
|
+
col.primary = true;
|
|
242
|
+
if (col.options.unique) {
|
|
243
|
+
delete col.options.unique;
|
|
244
|
+
}
|
|
245
|
+
});
|
|
246
|
+
if (!entity.columns.some((v) => {
|
|
247
|
+
return !!v.primary;
|
|
248
|
+
})) {
|
|
249
|
+
TomgUtils.LogError(`Table ${entity.tscName} has no PK.`, false);
|
|
250
|
+
}
|
|
251
|
+
});
|
|
252
|
+
}
|
|
253
|
+
static buildEscapedObjectList(dbNames) {
|
|
254
|
+
return `'${dbNames.join("','")}'`;
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
exports.default = AbstractDriver;
|
|
258
|
+
//# sourceMappingURL=AbstractDriver.js.map
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const MysqlDriver_1 = require("./MysqlDriver");
|
|
4
|
+
class MariaDbDriver extends MysqlDriver_1.default {
|
|
5
|
+
constructor() {
|
|
6
|
+
super(...arguments);
|
|
7
|
+
this.EngineName = "MariaDb";
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
exports.default = MariaDbDriver;
|
|
11
|
+
//# sourceMappingURL=MariaDbDriver.js.map
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { DataTypeDefaults } from "typeorm/driver/types/DataTypeDefaults";
|
|
2
|
+
import AbstractDriver from "./AbstractDriver";
|
|
3
|
+
import IConnectionOptions from "../IConnectionOptions";
|
|
4
|
+
import { Entity } from "../models/Entity";
|
|
5
|
+
import IGenerationOptions from "../IGenerationOptions";
|
|
6
|
+
export default class MssqlDriver extends AbstractDriver {
|
|
7
|
+
defaultValues: DataTypeDefaults;
|
|
8
|
+
readonly standardPort = 1433;
|
|
9
|
+
readonly standardSchema = "dbo";
|
|
10
|
+
readonly standardUser = "sa";
|
|
11
|
+
private MSSQL;
|
|
12
|
+
private Connection;
|
|
13
|
+
constructor();
|
|
14
|
+
GetAllTables(schemas: string[], dbNames: string[]): Promise<Entity[]>;
|
|
15
|
+
GetCoulmnsFromEntity(entities: Entity[], schemas: string[], dbNames: string[]): Promise<Entity[]>;
|
|
16
|
+
GetIndexesFromEntity(entities: Entity[], schemas: string[], dbNames: string[]): Promise<Entity[]>;
|
|
17
|
+
GetRelations(entities: Entity[], schemas: string[], dbNames: string[], generationOptions: IGenerationOptions): Promise<Entity[]>;
|
|
18
|
+
DisconnectFromServer(): Promise<void>;
|
|
19
|
+
ConnectToServer(connectionOptons: IConnectionOptions): Promise<void>;
|
|
20
|
+
CreateDB(dbName: string): Promise<void>;
|
|
21
|
+
UseDB(dbName: string): Promise<void>;
|
|
22
|
+
DropDB(dbName: string): Promise<void>;
|
|
23
|
+
CheckIfDBExists(dbName: string): Promise<boolean>;
|
|
24
|
+
private static ReturnDefaultValueFunction;
|
|
25
|
+
}
|