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,316 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const TypeormDriver = require("typeorm/driver/oracle/OracleDriver");
|
|
4
|
+
const TomgUtils = require("../Utils");
|
|
5
|
+
const AbstractDriver_1 = require("./AbstractDriver");
|
|
6
|
+
class OracleDriver extends AbstractDriver_1.default {
|
|
7
|
+
constructor() {
|
|
8
|
+
super();
|
|
9
|
+
this.defaultValues = new TypeormDriver.OracleDriver({
|
|
10
|
+
options: {},
|
|
11
|
+
}).dataTypeDefaults;
|
|
12
|
+
this.standardPort = 1521;
|
|
13
|
+
this.standardUser = "SYS";
|
|
14
|
+
this.standardSchema = "";
|
|
15
|
+
try {
|
|
16
|
+
// eslint-disable-next-line import/no-extraneous-dependencies, global-require, import/no-unresolved
|
|
17
|
+
this.Oracle = require("oracledb");
|
|
18
|
+
this.Oracle.outFormat = this.Oracle.OBJECT;
|
|
19
|
+
}
|
|
20
|
+
catch (error) {
|
|
21
|
+
TomgUtils.LogError("", false, error);
|
|
22
|
+
throw error;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
async GetAllTables(schemas, dbNames) {
|
|
26
|
+
const response = (await this.Connection.execute(`SELECT NULL AS TABLE_SCHEMA, TABLE_NAME, NULL AS DB_NAME FROM all_tables WHERE owner = (select user from dual)`)).rows;
|
|
27
|
+
const ret = [];
|
|
28
|
+
response.forEach((val) => {
|
|
29
|
+
ret.push({
|
|
30
|
+
columns: [],
|
|
31
|
+
indices: [],
|
|
32
|
+
relations: [],
|
|
33
|
+
relationIds: [],
|
|
34
|
+
sqlName: val.TABLE_NAME,
|
|
35
|
+
tscName: val.TABLE_NAME,
|
|
36
|
+
fileName: val.TABLE_NAME,
|
|
37
|
+
database: dbNames.length > 1 ? val.DB_NAME : "",
|
|
38
|
+
schema: val.TABLE_SCHEMA,
|
|
39
|
+
fileImports: [],
|
|
40
|
+
});
|
|
41
|
+
});
|
|
42
|
+
return ret;
|
|
43
|
+
}
|
|
44
|
+
async GetCoulmnsFromEntity(entities) {
|
|
45
|
+
const response = (await this.Connection.execute(`SELECT utc.*, (select count(*) from USER_CONS_COLUMNS ucc
|
|
46
|
+
JOIN USER_CONSTRAINTS uc ON uc.CONSTRAINT_NAME = ucc.CONSTRAINT_NAME and uc.CONSTRAINT_TYPE='U'
|
|
47
|
+
where ucc.column_name = utc.COLUMN_NAME AND ucc.table_name = utc.TABLE_NAME) IS_UNIQUE
|
|
48
|
+
FROM USER_TAB_COLUMNS utc`)).rows;
|
|
49
|
+
entities.forEach((ent) => {
|
|
50
|
+
response
|
|
51
|
+
.filter((filterVal) => filterVal.TABLE_NAME === ent.tscName)
|
|
52
|
+
.forEach((resp) => {
|
|
53
|
+
const tscName = resp.COLUMN_NAME;
|
|
54
|
+
const options = {
|
|
55
|
+
name: resp.COLUMN_NAME,
|
|
56
|
+
};
|
|
57
|
+
if (resp.NULLABLE === "Y")
|
|
58
|
+
options.nullable = true;
|
|
59
|
+
if (resp.IS_UNIQUE > 0)
|
|
60
|
+
options.unique = true;
|
|
61
|
+
const generated = resp.IDENTITY_COLUMN === "YES" ? true : undefined;
|
|
62
|
+
const defaultValue = !resp.DATA_DEFAULT || resp.DATA_DEFAULT.includes('"')
|
|
63
|
+
? undefined
|
|
64
|
+
: OracleDriver.ReturnDefaultValueFunction(resp.DATA_DEFAULT);
|
|
65
|
+
const DATA_TYPE = resp.DATA_TYPE.replace(/\([0-9]+\)/g, "");
|
|
66
|
+
const columnType = DATA_TYPE.toLowerCase();
|
|
67
|
+
let tscType = "";
|
|
68
|
+
switch (DATA_TYPE.toLowerCase()) {
|
|
69
|
+
case "char":
|
|
70
|
+
tscType = "string";
|
|
71
|
+
break;
|
|
72
|
+
case "nchar":
|
|
73
|
+
tscType = "string";
|
|
74
|
+
break;
|
|
75
|
+
case "nvarchar2":
|
|
76
|
+
tscType = "string";
|
|
77
|
+
break;
|
|
78
|
+
case "varchar2":
|
|
79
|
+
tscType = "string";
|
|
80
|
+
break;
|
|
81
|
+
case "long":
|
|
82
|
+
tscType = "string";
|
|
83
|
+
break;
|
|
84
|
+
case "raw":
|
|
85
|
+
tscType = "Buffer";
|
|
86
|
+
break;
|
|
87
|
+
case "long raw":
|
|
88
|
+
tscType = "Buffer";
|
|
89
|
+
break;
|
|
90
|
+
case "number":
|
|
91
|
+
tscType = "number";
|
|
92
|
+
break;
|
|
93
|
+
case "numeric":
|
|
94
|
+
tscType = "number";
|
|
95
|
+
break;
|
|
96
|
+
case "float":
|
|
97
|
+
tscType = "number";
|
|
98
|
+
break;
|
|
99
|
+
case "dec":
|
|
100
|
+
tscType = "number";
|
|
101
|
+
break;
|
|
102
|
+
case "decimal":
|
|
103
|
+
tscType = "number";
|
|
104
|
+
break;
|
|
105
|
+
case "integer":
|
|
106
|
+
tscType = "number";
|
|
107
|
+
break;
|
|
108
|
+
case "int":
|
|
109
|
+
tscType = "number";
|
|
110
|
+
break;
|
|
111
|
+
case "smallint":
|
|
112
|
+
tscType = "number";
|
|
113
|
+
break;
|
|
114
|
+
case "real":
|
|
115
|
+
tscType = "number";
|
|
116
|
+
break;
|
|
117
|
+
case "double precision":
|
|
118
|
+
tscType = "number";
|
|
119
|
+
break;
|
|
120
|
+
case "date":
|
|
121
|
+
tscType = "Date";
|
|
122
|
+
break;
|
|
123
|
+
case "timestamp":
|
|
124
|
+
tscType = "Date";
|
|
125
|
+
break;
|
|
126
|
+
case "timestamp with time zone":
|
|
127
|
+
tscType = "Date";
|
|
128
|
+
break;
|
|
129
|
+
case "timestamp with local time zone":
|
|
130
|
+
tscType = "Date";
|
|
131
|
+
break;
|
|
132
|
+
case "interval year to month":
|
|
133
|
+
tscType = "string";
|
|
134
|
+
break;
|
|
135
|
+
case "interval day to second":
|
|
136
|
+
tscType = "string";
|
|
137
|
+
break;
|
|
138
|
+
case "bfile":
|
|
139
|
+
tscType = "Buffer";
|
|
140
|
+
break;
|
|
141
|
+
case "blob":
|
|
142
|
+
tscType = "Buffer";
|
|
143
|
+
break;
|
|
144
|
+
case "clob":
|
|
145
|
+
tscType = "string";
|
|
146
|
+
break;
|
|
147
|
+
case "nclob":
|
|
148
|
+
tscType = "string";
|
|
149
|
+
break;
|
|
150
|
+
case "rowid":
|
|
151
|
+
tscType = "number";
|
|
152
|
+
break;
|
|
153
|
+
case "urowid":
|
|
154
|
+
tscType = "number";
|
|
155
|
+
break;
|
|
156
|
+
default:
|
|
157
|
+
tscType = "NonNullable<unknown>";
|
|
158
|
+
TomgUtils.LogError(`Unknown column type:${DATA_TYPE}`);
|
|
159
|
+
break;
|
|
160
|
+
}
|
|
161
|
+
if (this.ColumnTypesWithPrecision.some((v) => v === columnType)) {
|
|
162
|
+
if (resp.DATA_PRECISION !== null) {
|
|
163
|
+
options.precision = resp.DATA_PRECISION;
|
|
164
|
+
}
|
|
165
|
+
if (resp.DATA_SCALE !== null) {
|
|
166
|
+
options.scale = resp.DATA_SCALE;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
if (this.ColumnTypesWithLength.some((v) => v === columnType)) {
|
|
170
|
+
options.length =
|
|
171
|
+
resp.DATA_LENGTH > 0 ? resp.DATA_LENGTH : undefined;
|
|
172
|
+
}
|
|
173
|
+
ent.columns.push({
|
|
174
|
+
generated,
|
|
175
|
+
type: columnType,
|
|
176
|
+
default: defaultValue,
|
|
177
|
+
options,
|
|
178
|
+
tscName,
|
|
179
|
+
tscType,
|
|
180
|
+
});
|
|
181
|
+
});
|
|
182
|
+
});
|
|
183
|
+
return entities;
|
|
184
|
+
}
|
|
185
|
+
async GetIndexesFromEntity(entities) {
|
|
186
|
+
const response = (await this.Connection.execute(`SELECT ind.TABLE_NAME, ind.INDEX_NAME, col.COLUMN_NAME,ind.UNIQUENESS, CASE WHEN uc.CONSTRAINT_NAME IS NULL THEN 0 ELSE 1 END ISPRIMARYKEY
|
|
187
|
+
FROM USER_INDEXES ind
|
|
188
|
+
JOIN USER_IND_COLUMNS col ON ind.INDEX_NAME=col.INDEX_NAME
|
|
189
|
+
LEFT JOIN USER_CONSTRAINTS uc ON uc.INDEX_NAME = ind.INDEX_NAME
|
|
190
|
+
ORDER BY col.INDEX_NAME ASC ,col.COLUMN_POSITION ASC`)).rows;
|
|
191
|
+
entities.forEach((ent) => {
|
|
192
|
+
const entityIndices = response.filter((filterVal) => filterVal.TABLE_NAME === ent.tscName);
|
|
193
|
+
const indexNames = new Set(entityIndices.map((v) => v.INDEX_NAME));
|
|
194
|
+
indexNames.forEach((indexName) => {
|
|
195
|
+
const records = entityIndices.filter((v) => v.INDEX_NAME === indexName);
|
|
196
|
+
const indexInfo = {
|
|
197
|
+
columns: [],
|
|
198
|
+
options: {},
|
|
199
|
+
name: records[0].INDEX_NAME,
|
|
200
|
+
};
|
|
201
|
+
if (records[0].ISPRIMARYKEY === 1)
|
|
202
|
+
indexInfo.primary = true;
|
|
203
|
+
if (records[0].UNIQUENESS === "UNIQUE")
|
|
204
|
+
indexInfo.options.unique = true;
|
|
205
|
+
records.forEach((record) => {
|
|
206
|
+
indexInfo.columns.push(record.COLUMN_NAME);
|
|
207
|
+
});
|
|
208
|
+
ent.indices.push(indexInfo);
|
|
209
|
+
});
|
|
210
|
+
});
|
|
211
|
+
return entities;
|
|
212
|
+
}
|
|
213
|
+
async GetRelations(entities, schemas, dbNames, generationOptions) {
|
|
214
|
+
const response = (await this.Connection.execute(`select owner.TABLE_NAME OWNER_TABLE_NAME,ownCol.POSITION OWNER_POSITION,ownCol.COLUMN_NAME OWNER_COLUMN_NAME,
|
|
215
|
+
child.TABLE_NAME CHILD_TABLE_NAME ,childCol.COLUMN_NAME CHILD_COLUMN_NAME,
|
|
216
|
+
owner.DELETE_RULE,
|
|
217
|
+
owner.CONSTRAINT_NAME
|
|
218
|
+
from user_constraints owner
|
|
219
|
+
join user_constraints child on owner.r_constraint_name=child.CONSTRAINT_NAME and child.constraint_type in ('P','U')
|
|
220
|
+
JOIN USER_CONS_COLUMNS ownCol ON owner.CONSTRAINT_NAME = ownCol.CONSTRAINT_NAME
|
|
221
|
+
JOIN USER_CONS_COLUMNS childCol ON child.CONSTRAINT_NAME = childCol.CONSTRAINT_NAME AND ownCol.POSITION=childCol.POSITION
|
|
222
|
+
ORDER BY OWNER_TABLE_NAME ASC, owner.CONSTRAINT_NAME ASC, OWNER_POSITION ASC`)).rows;
|
|
223
|
+
const relationsTemp = [];
|
|
224
|
+
const relationKeys = new Set(response.map((v) => v.CONSTRAINT_NAME));
|
|
225
|
+
relationKeys.forEach((relationId) => {
|
|
226
|
+
const rows = response.filter((v) => v.CONSTRAINT_NAME === relationId);
|
|
227
|
+
const ownerTable = entities.find((v) => v.sqlName === rows[0].OWNER_TABLE_NAME);
|
|
228
|
+
const relatedTable = entities.find((v) => v.sqlName === rows[0].CHILD_TABLE_NAME);
|
|
229
|
+
if (!ownerTable || !relatedTable) {
|
|
230
|
+
TomgUtils.LogError(`Relation between tables ${rows[0].OWNER_TABLE_NAME} and ${rows[0].CHILD_TABLE_NAME} wasn't found in entity model.`, true);
|
|
231
|
+
return;
|
|
232
|
+
}
|
|
233
|
+
const internal = {
|
|
234
|
+
ownerColumns: [],
|
|
235
|
+
relatedColumns: [],
|
|
236
|
+
ownerTable,
|
|
237
|
+
relatedTable,
|
|
238
|
+
};
|
|
239
|
+
if (rows[0].DELETE_RULE !== "NO ACTION") {
|
|
240
|
+
internal.onDelete = rows[0].DELETE_RULE;
|
|
241
|
+
}
|
|
242
|
+
rows.forEach((row) => {
|
|
243
|
+
internal.ownerColumns.push(row.OWNER_COLUMN_NAME);
|
|
244
|
+
internal.relatedColumns.push(row.CHILD_COLUMN_NAME);
|
|
245
|
+
});
|
|
246
|
+
relationsTemp.push(internal);
|
|
247
|
+
});
|
|
248
|
+
const retVal = OracleDriver.GetRelationsFromRelationTempInfo(relationsTemp, entities, generationOptions);
|
|
249
|
+
return retVal;
|
|
250
|
+
}
|
|
251
|
+
async DisconnectFromServer() {
|
|
252
|
+
if (this.Connection) {
|
|
253
|
+
await this.Connection.close();
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
async ConnectToServer(connectionOptions) {
|
|
257
|
+
let config;
|
|
258
|
+
if (connectionOptions.user === String(process.env.ORACLE_UsernameSys)) {
|
|
259
|
+
config = {
|
|
260
|
+
connectString: `${connectionOptions.host}:${connectionOptions.port}/${connectionOptions.databaseNames[0]}`,
|
|
261
|
+
externalAuth: connectionOptions.ssl,
|
|
262
|
+
password: connectionOptions.password,
|
|
263
|
+
privilege: this.Oracle.SYSDBA,
|
|
264
|
+
user: connectionOptions.user,
|
|
265
|
+
};
|
|
266
|
+
}
|
|
267
|
+
else {
|
|
268
|
+
config = {
|
|
269
|
+
connectString: `${connectionOptions.host}:${connectionOptions.port}/${connectionOptions.databaseNames[0]}`,
|
|
270
|
+
externalAuth: connectionOptions.ssl,
|
|
271
|
+
password: connectionOptions.password,
|
|
272
|
+
user: connectionOptions.user,
|
|
273
|
+
};
|
|
274
|
+
}
|
|
275
|
+
const promise = new Promise((resolve, reject) => {
|
|
276
|
+
this.Oracle.getConnection(config, (err, connection) => {
|
|
277
|
+
if (!err) {
|
|
278
|
+
this.Connection = connection;
|
|
279
|
+
resolve(true);
|
|
280
|
+
}
|
|
281
|
+
else {
|
|
282
|
+
TomgUtils.LogError("Error connecting to Oracle Server.", false, err.message);
|
|
283
|
+
reject(err);
|
|
284
|
+
}
|
|
285
|
+
});
|
|
286
|
+
});
|
|
287
|
+
await promise;
|
|
288
|
+
}
|
|
289
|
+
async CreateDB(dbName) {
|
|
290
|
+
await this.Connection.execute(`CREATE USER ${dbName} IDENTIFIED BY ${String(process.env.ORACLE_Password)}`);
|
|
291
|
+
await this.Connection.execute(`GRANT CONNECT TO ${dbName}`);
|
|
292
|
+
}
|
|
293
|
+
// eslint-disable-next-line class-methods-use-this
|
|
294
|
+
async UseDB() {
|
|
295
|
+
// not supported
|
|
296
|
+
}
|
|
297
|
+
async DropDB(dbName) {
|
|
298
|
+
await this.Connection.execute(`DROP USER ${dbName} CASCADE`);
|
|
299
|
+
}
|
|
300
|
+
async CheckIfDBExists(dbName) {
|
|
301
|
+
const { rows } = await this.Connection.execute(`select count(*) as CNT from dba_users where username='${dbName.toUpperCase()}'`);
|
|
302
|
+
return rows[0][0] > 0 || rows[0].CNT;
|
|
303
|
+
}
|
|
304
|
+
static ReturnDefaultValueFunction(defVal) {
|
|
305
|
+
let defaultVal = defVal === null || defVal === void 0 ? void 0 : defVal.trim();
|
|
306
|
+
if (!defaultVal) {
|
|
307
|
+
return undefined;
|
|
308
|
+
}
|
|
309
|
+
if (defaultVal.endsWith(" ")) {
|
|
310
|
+
defaultVal = defaultVal.slice(0, -1);
|
|
311
|
+
}
|
|
312
|
+
return `() => "${defaultVal}"`;
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
exports.default = OracleDriver;
|
|
316
|
+
//# sourceMappingURL=OracleDriver.js.map
|
|
@@ -0,0 +1,31 @@
|
|
|
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 { Column } from "../models/Column";
|
|
6
|
+
import IGenerationOptions from "../IGenerationOptions";
|
|
7
|
+
export default class PostgresDriver extends AbstractDriver {
|
|
8
|
+
defaultValues: DataTypeDefaults;
|
|
9
|
+
readonly standardPort = 5432;
|
|
10
|
+
readonly standardUser = "postgres";
|
|
11
|
+
readonly standardSchema = "public";
|
|
12
|
+
private PG;
|
|
13
|
+
private Connection;
|
|
14
|
+
constructor();
|
|
15
|
+
GetAllTables(schemas: string[], dbNames: string[]): Promise<Entity[]>;
|
|
16
|
+
GetCoulmnsFromEntity(entities: Entity[], schemas: string[]): Promise<Entity[]>;
|
|
17
|
+
MatchColumnTypes(dataType: string, udtName: string, enumValues: string | null): {
|
|
18
|
+
tsType: Column["tscType"];
|
|
19
|
+
sqlType: string;
|
|
20
|
+
isArray: boolean;
|
|
21
|
+
enumValues: string[];
|
|
22
|
+
};
|
|
23
|
+
GetIndexesFromEntity(entities: Entity[], schemas: string[]): Promise<Entity[]>;
|
|
24
|
+
GetRelations(entities: Entity[], schemas: string[], dbNames: string[], generationOptions: IGenerationOptions): Promise<Entity[]>;
|
|
25
|
+
DisconnectFromServer(): Promise<void>;
|
|
26
|
+
ConnectToServer(connectionOptons: IConnectionOptions): Promise<void>;
|
|
27
|
+
CreateDB(dbName: string): Promise<void>;
|
|
28
|
+
DropDB(dbName: string): Promise<void>;
|
|
29
|
+
CheckIfDBExists(dbName: string): Promise<boolean>;
|
|
30
|
+
private static ReturnDefaultValueFunction;
|
|
31
|
+
}
|