gg-mysql-connector 1.0.43 → 1.0.46
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/package.json +1 -1
- package/dist/GGMySQLConnector/GGMySQLConnector.d.ts +0 -72
- package/dist/GGMySQLConnector/GGMySQLConnector.js +0 -296
- package/dist/GGMySQLConnector.d.ts +0 -72
- package/dist/GGMySQLConnector.js +0 -296
- package/dist/ModelGenerator/ModelGenerator.d.ts +0 -38
- package/dist/ModelGenerator/ModelGenerator.js +0 -299
- package/dist/ModelGenerator/SeedGenerator.d.ts +0 -1
- package/dist/ModelGenerator/SeedGenerator.js +0 -3
- package/dist/ModelGenerator.d.ts +0 -35
- package/dist/ModelGenerator.js +0 -288
- package/dist/MyDBMigration.d.ts +0 -22
- package/dist/MyDBMigration.js +0 -157
- package/dist/MyDBMigrator/MyDBMigrator.d.ts +0 -22
- package/dist/MyDBMigrator/MyDBMigrator.js +0 -157
- package/dist/index.d.ts +0 -4
- package/dist/index.js +0 -10
- package/dist/myModel.d.ts +0 -16
- package/dist/myModel.js +0 -6
package/dist/ModelGenerator.js
DELETED
|
@@ -1,288 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
const chalk_1 = __importDefault(require("chalk"));
|
|
7
|
-
const crypto_1 = require("crypto");
|
|
8
|
-
const fs_1 = __importDefault(require("fs"));
|
|
9
|
-
const promise_1 = __importDefault(require("mysql2/promise"));
|
|
10
|
-
const path_1 = __importDefault(require("path"));
|
|
11
|
-
const MyDBMigration_1 = __importDefault(require("./MyDBMigration"));
|
|
12
|
-
class ModelGenerator {
|
|
13
|
-
constructor(dbInfo, model) {
|
|
14
|
-
this.printResultLength = (results, executionTime, queryResult) => {
|
|
15
|
-
if (results.length) {
|
|
16
|
-
const executionTimeMessage = executionTime > 1000
|
|
17
|
-
? chalk_1.default.bgRed(`${executionTime} ` + " ms")
|
|
18
|
-
: chalk_1.default.yellow(`${executionTime} ` + " ms");
|
|
19
|
-
console.log(`${chalk_1.default.green(queryResult.sql)} ${chalk_1.default.cyan(`found ${results.length} rows.`)} ${executionTimeMessage}`);
|
|
20
|
-
}
|
|
21
|
-
};
|
|
22
|
-
this.model = model;
|
|
23
|
-
this.dbInfo = dbInfo;
|
|
24
|
-
this.isConnected = false;
|
|
25
|
-
}
|
|
26
|
-
async init() {
|
|
27
|
-
console.log("init");
|
|
28
|
-
this.connection = await promise_1.default.createConnection({
|
|
29
|
-
host: this.dbInfo.host,
|
|
30
|
-
user: this.dbInfo.user,
|
|
31
|
-
password: this.dbInfo.password,
|
|
32
|
-
});
|
|
33
|
-
this.isConnected = true;
|
|
34
|
-
await this.createDatabaseIfNotExist();
|
|
35
|
-
}
|
|
36
|
-
async createDatabaseIfNotExist() {
|
|
37
|
-
const databaseName = this.dbInfo.database;
|
|
38
|
-
await this.connection.query(`CREATE DATABASE IF NOT EXISTS ${databaseName}`);
|
|
39
|
-
await this.connection.query(`USE ${databaseName}`);
|
|
40
|
-
}
|
|
41
|
-
async pushModelToDB() {
|
|
42
|
-
const migrator = new MyDBMigration_1.default(this);
|
|
43
|
-
await migrator.migrateTable(this.model);
|
|
44
|
-
}
|
|
45
|
-
async pushViewToDB(viewDirectory) {
|
|
46
|
-
const migrator = new MyDBMigration_1.default(this);
|
|
47
|
-
await migrator.migrateView(viewDirectory);
|
|
48
|
-
}
|
|
49
|
-
waitUntilInitSuccess(interval) {
|
|
50
|
-
if (this.isConnected === true)
|
|
51
|
-
return true;
|
|
52
|
-
return new Promise((resolve, reject) => {
|
|
53
|
-
setTimeout(() => {
|
|
54
|
-
if (this.isConnected === false) {
|
|
55
|
-
console.log(`${this.dbInfo.database} > wait for database connection . . .`);
|
|
56
|
-
this.waitUntilInitSuccess(interval);
|
|
57
|
-
}
|
|
58
|
-
else {
|
|
59
|
-
return resolve(true);
|
|
60
|
-
}
|
|
61
|
-
}, interval);
|
|
62
|
-
});
|
|
63
|
-
}
|
|
64
|
-
async query(statment, parameter, isPrint) {
|
|
65
|
-
await this.waitUntilInitSuccess(1000);
|
|
66
|
-
return new Promise((resolve, reject) => {
|
|
67
|
-
const uniqueTime = `${(0, crypto_1.randomUUID)()} ${statment}`;
|
|
68
|
-
console.time(uniqueTime);
|
|
69
|
-
const startTime = new Date();
|
|
70
|
-
const queryResult = this.connection
|
|
71
|
-
.query(statment, parameter)
|
|
72
|
-
.then((result) => {
|
|
73
|
-
const endTime = new Date();
|
|
74
|
-
const executionTime = endTime.getTime() - startTime.getTime();
|
|
75
|
-
if (isPrint === true)
|
|
76
|
-
this.printResultLength(result, executionTime, queryResult);
|
|
77
|
-
resolve(result[0]);
|
|
78
|
-
})
|
|
79
|
-
.catch((error) => {
|
|
80
|
-
// _getCallerFile()
|
|
81
|
-
console.log(chalk_1.default.bgRed("Query Error"));
|
|
82
|
-
console.log(chalk_1.default.bgRed(error));
|
|
83
|
-
console.log(chalk_1.default.bgRed("Statement : "));
|
|
84
|
-
console.log(statment);
|
|
85
|
-
console.log(chalk_1.default.bgRed("Parameter : "));
|
|
86
|
-
console.log(parameter);
|
|
87
|
-
console.log("------------------------------------------------");
|
|
88
|
-
console.log(chalk_1.default.bgRed(queryResult.sql));
|
|
89
|
-
reject(error);
|
|
90
|
-
});
|
|
91
|
-
});
|
|
92
|
-
}
|
|
93
|
-
async getTableNameList() {
|
|
94
|
-
let data = [];
|
|
95
|
-
let result = (await this.query("SELECT * FROM information_schema.tables WHERE table_schema = ? AND TABLE_TYPE = 'BASE TABLE'", [this.dbInfo.database], false));
|
|
96
|
-
for (let row of result)
|
|
97
|
-
data.push(row.TABLE_NAME || row.row.table_name);
|
|
98
|
-
return data;
|
|
99
|
-
}
|
|
100
|
-
async getViewNameList() {
|
|
101
|
-
let data = [];
|
|
102
|
-
let result = (await this.query("SELECT * FROM information_schema.tables WHERE table_schema = ? AND TABLE_TYPE = 'VIEW'", [this.dbInfo.database], false));
|
|
103
|
-
for (let row of result)
|
|
104
|
-
data.push(row.TABLE_NAME || row.row.table_name);
|
|
105
|
-
return data;
|
|
106
|
-
}
|
|
107
|
-
async getColumnFullList(tableName) {
|
|
108
|
-
const result = (await this.query(`SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = '${this.dbInfo.database}' AND TABLE_NAME = '${tableName}'`, undefined, false));
|
|
109
|
-
return result;
|
|
110
|
-
}
|
|
111
|
-
async generateModelInterface(params) {
|
|
112
|
-
const isTableNameExistInModel = (tableName) => {
|
|
113
|
-
if (tableName === "sessions")
|
|
114
|
-
return true;
|
|
115
|
-
const table = params.model.find((row) => row.tableName === tableName);
|
|
116
|
-
if (table)
|
|
117
|
-
return true;
|
|
118
|
-
return false;
|
|
119
|
-
};
|
|
120
|
-
const isColumnExistInModel = (tableName, columnName) => {
|
|
121
|
-
if (tableName === "sessions")
|
|
122
|
-
return true;
|
|
123
|
-
const table = params.model.find((row) => row.tableName === tableName);
|
|
124
|
-
if (table) {
|
|
125
|
-
const column = table.columns.find((row) => row.COLUMN_NAME === columnName);
|
|
126
|
-
if (column)
|
|
127
|
-
return true;
|
|
128
|
-
}
|
|
129
|
-
return false;
|
|
130
|
-
};
|
|
131
|
-
const getPossibleColumnValue = (tableName, columnName) => {
|
|
132
|
-
const foundTable = params.model.find((row) => row.tableName === tableName);
|
|
133
|
-
if (foundTable) {
|
|
134
|
-
const foundColumn = foundTable["columns"].find((row) => row.COLUMN_NAME === columnName);
|
|
135
|
-
if (foundColumn) {
|
|
136
|
-
if (foundColumn.POSSIBLE_VALUE)
|
|
137
|
-
return foundColumn.POSSIBLE_VALUE;
|
|
138
|
-
}
|
|
139
|
-
else
|
|
140
|
-
return false;
|
|
141
|
-
}
|
|
142
|
-
};
|
|
143
|
-
const tableList = [
|
|
144
|
-
...(await this.getTableNameList()),
|
|
145
|
-
...(await this.getViewNameList()),
|
|
146
|
-
];
|
|
147
|
-
const array = [];
|
|
148
|
-
for (let i = 0; i < tableList.length; i++) {
|
|
149
|
-
const tableName = tableList[i];
|
|
150
|
-
if (isTableNameExistInModel(tableName) ||
|
|
151
|
-
tableName.search("_view") >= 0) {
|
|
152
|
-
const columnList = await this.getColumnFullList(tableName);
|
|
153
|
-
let singleInterfaceContent = "";
|
|
154
|
-
for (const cRow of columnList) {
|
|
155
|
-
const columnChecker = isColumnExistInModel(tableName, cRow.COLUMN_NAME);
|
|
156
|
-
if (columnChecker || tableName.search("_view") >= 0) {
|
|
157
|
-
const possibleValue = getPossibleColumnValue(cRow.TABLE_NAME, cRow.COLUMN_NAME);
|
|
158
|
-
if (possibleValue) {
|
|
159
|
-
singleInterfaceContent = `${singleInterfaceContent} \n ${cRow.COLUMN_NAME}: ${possibleValue.map((row) => `"${row}"`).join(" | ")};`;
|
|
160
|
-
}
|
|
161
|
-
else {
|
|
162
|
-
singleInterfaceContent = `${singleInterfaceContent} \n ${cRow.COLUMN_NAME}: ${convertDataType(cRow.DATA_TYPE)};`;
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
else {
|
|
166
|
-
console.log(`${tableName}-${cRow.COLUMN_NAME} not exist in model `);
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
const str = ` ${tableName} : { ${singleInterfaceContent} }`;
|
|
170
|
-
array.push(str);
|
|
171
|
-
// console.log(`generate interface from ${tableName}`)
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
const fileName = `${params.appName}_INF.ts`;
|
|
175
|
-
const code = `export default interface ${params.appName}_INF { ${array.join("\n")} }`;
|
|
176
|
-
for (let row of params.outputDirectory) {
|
|
177
|
-
const serverFilePath = path_1.default.join(row, fileName);
|
|
178
|
-
await fs_1.default.writeFileSync(serverFilePath, code);
|
|
179
|
-
console.log("save to ", serverFilePath);
|
|
180
|
-
}
|
|
181
|
-
console.log(`generate interface ${params.appName} ${chalk_1.default.bgGreen(" SUCCESS ")}`);
|
|
182
|
-
return code;
|
|
183
|
-
}
|
|
184
|
-
async generateModelConstStructure(params) {
|
|
185
|
-
const isTableNameExistInModel = (tableName) => {
|
|
186
|
-
if (tableName === "sessions")
|
|
187
|
-
return true;
|
|
188
|
-
const table = params.model.find((row) => row.tableName === tableName);
|
|
189
|
-
if (table)
|
|
190
|
-
return true;
|
|
191
|
-
return false;
|
|
192
|
-
};
|
|
193
|
-
const isColumnExistInModel = (tableName, columnName) => {
|
|
194
|
-
if (tableName === "sessions")
|
|
195
|
-
return true;
|
|
196
|
-
const table = params.model.find((row) => row.tableName === tableName);
|
|
197
|
-
if (table) {
|
|
198
|
-
const column = table.columns.find((row) => row.COLUMN_NAME === columnName);
|
|
199
|
-
if (column)
|
|
200
|
-
return true;
|
|
201
|
-
}
|
|
202
|
-
return false;
|
|
203
|
-
};
|
|
204
|
-
const getPossibleColumnValue = (tableName, columnName) => {
|
|
205
|
-
const foundTable = params.model.find((row) => row.tableName === tableName);
|
|
206
|
-
if (foundTable) {
|
|
207
|
-
const foundColumn = foundTable["columns"].find((row) => row.COLUMN_NAME === columnName);
|
|
208
|
-
if (foundColumn) {
|
|
209
|
-
if (foundColumn.POSSIBLE_VALUE)
|
|
210
|
-
return foundColumn.POSSIBLE_VALUE;
|
|
211
|
-
}
|
|
212
|
-
else
|
|
213
|
-
return false;
|
|
214
|
-
}
|
|
215
|
-
};
|
|
216
|
-
const tableList = [
|
|
217
|
-
...(await this.getTableNameList()),
|
|
218
|
-
...(await this.getViewNameList()),
|
|
219
|
-
];
|
|
220
|
-
const array = [];
|
|
221
|
-
for (let i = 0; i < tableList.length; i++) {
|
|
222
|
-
const tableName = tableList[i];
|
|
223
|
-
if (isTableNameExistInModel(tableName) ||
|
|
224
|
-
tableName.search("_view") >= 0) {
|
|
225
|
-
const columnList = await this.getColumnFullList(tableName);
|
|
226
|
-
let value = "";
|
|
227
|
-
for (const cRow of columnList) {
|
|
228
|
-
const columnChecker = isColumnExistInModel(tableName, cRow.COLUMN_NAME);
|
|
229
|
-
if (columnChecker || tableName.search("_view") >= 0) {
|
|
230
|
-
const possibleValue = getPossibleColumnValue(cRow.TABLE_NAME, cRow.COLUMN_NAME);
|
|
231
|
-
if (possibleValue) {
|
|
232
|
-
value = `${value} \n ${cRow.COLUMN_NAME}: [${possibleValue
|
|
233
|
-
.map((row) => {
|
|
234
|
-
if (typeof row === "string")
|
|
235
|
-
return `"${row}"`;
|
|
236
|
-
else if (typeof row === "number")
|
|
237
|
-
return `${row}`;
|
|
238
|
-
})
|
|
239
|
-
.join(" , ")}] as ${typeof possibleValue[0]}[],`;
|
|
240
|
-
}
|
|
241
|
-
else {
|
|
242
|
-
value = `${value} \n ${cRow.COLUMN_NAME}: "${convertDataType(cRow.DATA_TYPE)}",`;
|
|
243
|
-
}
|
|
244
|
-
}
|
|
245
|
-
else {
|
|
246
|
-
console.log(`${tableName}-${cRow.COLUMN_NAME} not exist in model `);
|
|
247
|
-
}
|
|
248
|
-
}
|
|
249
|
-
const str = ` ${tableName} : { ${value} }`;
|
|
250
|
-
array.push(str);
|
|
251
|
-
// console.log(`generate interface from ${tableName}`)
|
|
252
|
-
}
|
|
253
|
-
}
|
|
254
|
-
const fileName = `${params.appName}_model_const.ts`;
|
|
255
|
-
const code = `
|
|
256
|
-
export const ${params.appName}_model_const = { ${array.join(",")} } as const`;
|
|
257
|
-
for (let row of params.outputDirectory) {
|
|
258
|
-
const serverFilePath = path_1.default.join(row, fileName);
|
|
259
|
-
await fs_1.default.writeFileSync(serverFilePath, code);
|
|
260
|
-
console.log("save to ", serverFilePath);
|
|
261
|
-
}
|
|
262
|
-
console.log(`generate interface ${params.appName} ${chalk_1.default.bgGreen(" SUCCESS ")}`);
|
|
263
|
-
return code;
|
|
264
|
-
}
|
|
265
|
-
}
|
|
266
|
-
exports.default = ModelGenerator;
|
|
267
|
-
function convertDataType(inputDataType) {
|
|
268
|
-
let result = "string";
|
|
269
|
-
if (inputDataType === "int") {
|
|
270
|
-
result = "number";
|
|
271
|
-
}
|
|
272
|
-
else if (inputDataType === "tinyint") {
|
|
273
|
-
result = "number";
|
|
274
|
-
}
|
|
275
|
-
else if (inputDataType === "bigint") {
|
|
276
|
-
result = "number";
|
|
277
|
-
}
|
|
278
|
-
else if (inputDataType === "date") {
|
|
279
|
-
result = "string";
|
|
280
|
-
}
|
|
281
|
-
else if (inputDataType === "float") {
|
|
282
|
-
result = "number";
|
|
283
|
-
}
|
|
284
|
-
else if (inputDataType === "double") {
|
|
285
|
-
result = "number";
|
|
286
|
-
}
|
|
287
|
-
return result;
|
|
288
|
-
}
|
package/dist/MyDBMigration.d.ts
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import ModelGenerator from "./ModelGenerator";
|
|
2
|
-
import { columnType, MyModel } from "./myModel";
|
|
3
|
-
export default class MyDBMigration<Main> {
|
|
4
|
-
MyDB: ModelGenerator;
|
|
5
|
-
onetimeLoadColumnContent: {
|
|
6
|
-
COLUMN_NAME: string;
|
|
7
|
-
DATA_TYPE: columnType;
|
|
8
|
-
TABLE_NAME: string;
|
|
9
|
-
COLUMN_DEFAULT: string;
|
|
10
|
-
COLUMN_KEY: string;
|
|
11
|
-
}[];
|
|
12
|
-
constructor(connection: ModelGenerator);
|
|
13
|
-
migrateTable(model: MyModel[]): Promise<void>;
|
|
14
|
-
private createTableIfNotExist;
|
|
15
|
-
private columnInTableExist;
|
|
16
|
-
private loadColumnContentIfEmpty;
|
|
17
|
-
private checkIsColumnDataTypeChange;
|
|
18
|
-
alterUniqueKey(tableName: string, columnName: string, IS_UNIQUE: boolean | undefined): Promise<void>;
|
|
19
|
-
alterIndex(tableName: string, columnName: string, IS_INDEX: boolean | undefined): Promise<void>;
|
|
20
|
-
migrateView(viewDirectory: string): Promise<0 | undefined>;
|
|
21
|
-
checkIsPrimaryKey(databaseName: string, tableName: string, columnName: string): Promise<boolean>;
|
|
22
|
-
}
|
package/dist/MyDBMigration.js
DELETED
|
@@ -1,157 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
const chalk_1 = __importDefault(require("chalk"));
|
|
7
|
-
const fs_1 = __importDefault(require("fs"));
|
|
8
|
-
const path_1 = __importDefault(require("path"));
|
|
9
|
-
class MyDBMigration {
|
|
10
|
-
constructor(connection) {
|
|
11
|
-
this.MyDB = connection;
|
|
12
|
-
this.onetimeLoadColumnContent = [];
|
|
13
|
-
}
|
|
14
|
-
async migrateTable(model) {
|
|
15
|
-
let data = model.filter((row) => row.tableName !== "sessions" && row.tableName.search("prisma") < 0);
|
|
16
|
-
let index = 1;
|
|
17
|
-
for (let row of data) {
|
|
18
|
-
console.log(chalk_1.default.bgBlue(`${index}/${data.length} ${row.tableName}`));
|
|
19
|
-
await this.createTableIfNotExist(row.tableName);
|
|
20
|
-
this.onetimeLoadColumnContent = [];
|
|
21
|
-
await this.loadColumnContentIfEmpty();
|
|
22
|
-
index++;
|
|
23
|
-
for (let col of row.columns) {
|
|
24
|
-
console.log(`${row.tableName} (${col.DATA_TYPE}) - ${col.COLUMN_NAME} `);
|
|
25
|
-
if (col.COLUMN_NAME === "id" && col.AUTO_INCREMENT === true) {
|
|
26
|
-
if ((await this.checkIsPrimaryKey(this.MyDB.dbInfo.database, row.tableName, col.COLUMN_NAME)) === false)
|
|
27
|
-
await this.MyDB.query(`ALTER TABLE ${row.tableName} ADD PRIMARY KEY(id);`);
|
|
28
|
-
}
|
|
29
|
-
const AutoIncrement = col.AUTO_INCREMENT ? "AUTO_INCREMENT" : "";
|
|
30
|
-
const columnDefault = col.COLUMN_DEFAULT
|
|
31
|
-
? `DEFAULT ${col.COLUMN_DEFAULT}`
|
|
32
|
-
: "";
|
|
33
|
-
const isColumnChange = await this.checkIsColumnDataTypeChange(row.tableName, col.COLUMN_NAME, col.DATA_TYPE, col.COLUMN_DEFAULT);
|
|
34
|
-
if (isColumnChange) {
|
|
35
|
-
const isColumnExist = await this.columnInTableExist(row.tableName, col.COLUMN_NAME);
|
|
36
|
-
if (!isColumnExist) {
|
|
37
|
-
await this.MyDB.query(`ALTER TABLE ${row.tableName} ADD COLUMN ${col.COLUMN_NAME} ${col.DATA_TYPE} ${AutoIncrement} ${columnDefault}`);
|
|
38
|
-
}
|
|
39
|
-
else {
|
|
40
|
-
await this.MyDB.query(`ALTER TABLE ${row.tableName} MODIFY COLUMN ${col.COLUMN_NAME} ${col.DATA_TYPE} ${AutoIncrement} ${columnDefault}`);
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
await this.alterUniqueKey(row.tableName, col.COLUMN_NAME, col.IS_UNIQUE);
|
|
44
|
-
await this.alterIndex(row.tableName, col.COLUMN_NAME, col.IS_INDEX);
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
console.log("migrate table done");
|
|
48
|
-
}
|
|
49
|
-
async createTableIfNotExist(tableName) {
|
|
50
|
-
await this.MyDB.query(`CREATE TABLE IF NOT EXISTS ${tableName} (id INT(11) AUTO_INCREMENT PRIMARY KEY)`);
|
|
51
|
-
}
|
|
52
|
-
async columnInTableExist(tableName, columnName) {
|
|
53
|
-
await this.loadColumnContentIfEmpty();
|
|
54
|
-
let result = this.onetimeLoadColumnContent.filter((row) => row.COLUMN_NAME === columnName && row.TABLE_NAME === tableName);
|
|
55
|
-
if (result.length)
|
|
56
|
-
return true;
|
|
57
|
-
else
|
|
58
|
-
return false;
|
|
59
|
-
}
|
|
60
|
-
async loadColumnContentIfEmpty() {
|
|
61
|
-
if (this.onetimeLoadColumnContent.length === 0) {
|
|
62
|
-
this.onetimeLoadColumnContent = (await this.MyDB.query(`SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = '${this.MyDB.dbInfo.database}'`, undefined, false));
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
async checkIsColumnDataTypeChange(tableName, columnName, dataType, columnDefault) {
|
|
66
|
-
await this.loadColumnContentIfEmpty();
|
|
67
|
-
let temp = this.onetimeLoadColumnContent.filter((row) => row.TABLE_NAME === tableName && row.COLUMN_NAME === columnName);
|
|
68
|
-
if (columnDefault === undefined)
|
|
69
|
-
columnDefault = "NULL";
|
|
70
|
-
if (columnDefault === null)
|
|
71
|
-
columnDefault = "NULL";
|
|
72
|
-
if (temp.length > 0) {
|
|
73
|
-
if (temp[0].DATA_TYPE !== dataType) {
|
|
74
|
-
console.log(chalk_1.default.green `${temp[0].DATA_TYPE} change to ${dataType}`);
|
|
75
|
-
return true;
|
|
76
|
-
}
|
|
77
|
-
if (temp[0].COLUMN_DEFAULT !== columnDefault) {
|
|
78
|
-
console.log(chalk_1.default.green `${temp[0].COLUMN_DEFAULT} change to ${columnDefault}`);
|
|
79
|
-
return true;
|
|
80
|
-
}
|
|
81
|
-
return false;
|
|
82
|
-
}
|
|
83
|
-
else {
|
|
84
|
-
console.log(chalk_1.default.red `${columnName} not found`);
|
|
85
|
-
return true;
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
async alterUniqueKey(tableName, columnName, IS_UNIQUE) {
|
|
89
|
-
const data = this.onetimeLoadColumnContent.find((row) => row.COLUMN_NAME === columnName && row.TABLE_NAME === tableName);
|
|
90
|
-
if (data) {
|
|
91
|
-
if ((data.COLUMN_KEY === "UNI" && !IS_UNIQUE) ||
|
|
92
|
-
(data.COLUMN_KEY !== "UNI" && IS_UNIQUE === true))
|
|
93
|
-
if (IS_UNIQUE === true) {
|
|
94
|
-
await this.MyDB.query(`ALTER TABLE ${tableName} ADD UNIQUE (${columnName});`);
|
|
95
|
-
}
|
|
96
|
-
else {
|
|
97
|
-
await this.MyDB.query(`ALTER TABLE ${tableName} DROP INDEX ${columnName};`);
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
async alterIndex(tableName, columnName, IS_INDEX) {
|
|
102
|
-
const data = this.onetimeLoadColumnContent.find((row) => row.COLUMN_NAME === columnName && row.TABLE_NAME === tableName);
|
|
103
|
-
if (data) {
|
|
104
|
-
if ((data.COLUMN_KEY === "MUL" && !IS_INDEX) ||
|
|
105
|
-
(data.COLUMN_KEY !== "MUL" && IS_INDEX === true))
|
|
106
|
-
if (IS_INDEX === true) {
|
|
107
|
-
await this.MyDB.query(`ALTER TABLE ${tableName} ADD INDEX (${columnName});`);
|
|
108
|
-
}
|
|
109
|
-
else {
|
|
110
|
-
await this.MyDB.query(`DROP INDEX ${columnName} ON ${tableName};`);
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
async migrateView(viewDirectory) {
|
|
115
|
-
if (!fs_1.default.existsSync(viewDirectory)) {
|
|
116
|
-
return 0;
|
|
117
|
-
}
|
|
118
|
-
let fileList = (await fs_1.default.readdirSync(path_1.default.join(viewDirectory)));
|
|
119
|
-
let unSuccessUpdateViewList = fileList;
|
|
120
|
-
let loopCount = 0;
|
|
121
|
-
while (unSuccessUpdateViewList.length > 0) {
|
|
122
|
-
for (const viewFileName of unSuccessUpdateViewList) {
|
|
123
|
-
const filePath = path_1.default.join(viewDirectory, viewFileName);
|
|
124
|
-
const content = fs_1.default.readFileSync(filePath, "utf8");
|
|
125
|
-
process.stdout.write(`loop : ${loopCount} unUpdate : ${unSuccessUpdateViewList.length} update view ${viewFileName}`);
|
|
126
|
-
await this.MyDB.query(content)
|
|
127
|
-
.then((result) => {
|
|
128
|
-
process.stdout.write(" " + chalk_1.default.bgGreen(`success`));
|
|
129
|
-
unSuccessUpdateViewList = unSuccessUpdateViewList.filter((row) => row !== viewFileName);
|
|
130
|
-
})
|
|
131
|
-
.catch((_error) => {
|
|
132
|
-
process.stdout.write(" " + chalk_1.default.bgRed(`fail`));
|
|
133
|
-
})
|
|
134
|
-
.finally(() => {
|
|
135
|
-
console.log("");
|
|
136
|
-
});
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
async checkIsPrimaryKey(databaseName, tableName, columnName) {
|
|
141
|
-
let result = await this.MyDB.query(`SELECT *
|
|
142
|
-
FROM INFORMATION_SCHEMA.COLUMNS
|
|
143
|
-
WHERE TABLE_SCHEMA='${databaseName}'
|
|
144
|
-
AND TABLE_NAME='${tableName}'
|
|
145
|
-
AND COLUMN_NAME='${columnName}'`);
|
|
146
|
-
console.log("result", result);
|
|
147
|
-
if (result.length) {
|
|
148
|
-
if (result[0].COLUMN_KEY === "PRI")
|
|
149
|
-
return true;
|
|
150
|
-
else
|
|
151
|
-
return false;
|
|
152
|
-
}
|
|
153
|
-
else
|
|
154
|
-
return false;
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
exports.default = MyDBMigration;
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import ModelGenerator from "../ModelGenerator/ModelGenerator";
|
|
2
|
-
import { columnType, MyModel } from "../myModel";
|
|
3
|
-
export default class MyDBMigrator {
|
|
4
|
-
MyDB: ModelGenerator;
|
|
5
|
-
onetimeLoadColumnContent: {
|
|
6
|
-
COLUMN_NAME: string;
|
|
7
|
-
DATA_TYPE: columnType;
|
|
8
|
-
TABLE_NAME: string;
|
|
9
|
-
COLUMN_DEFAULT: string;
|
|
10
|
-
COLUMN_KEY: string;
|
|
11
|
-
}[];
|
|
12
|
-
constructor(connection: ModelGenerator);
|
|
13
|
-
migrateTable(model: MyModel[]): Promise<void>;
|
|
14
|
-
private createTableIfNotExist;
|
|
15
|
-
private columnInTableExist;
|
|
16
|
-
private loadColumnContentIfEmpty;
|
|
17
|
-
private checkIsColumnDataTypeChange;
|
|
18
|
-
alterUniqueKey(tableName: string, columnName: string, IS_UNIQUE: boolean | undefined): Promise<void>;
|
|
19
|
-
alterIndex(tableName: string, columnName: string, IS_INDEX: boolean | undefined): Promise<void>;
|
|
20
|
-
migrateView(viewDirectory: string): Promise<0 | undefined>;
|
|
21
|
-
checkIsPrimaryKey(databaseName: string, tableName: string, columnName: string): Promise<boolean>;
|
|
22
|
-
}
|
|
@@ -1,157 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
const chalk_1 = __importDefault(require("chalk"));
|
|
7
|
-
const fs_1 = __importDefault(require("fs"));
|
|
8
|
-
const path_1 = __importDefault(require("path"));
|
|
9
|
-
class MyDBMigrator {
|
|
10
|
-
constructor(connection) {
|
|
11
|
-
this.MyDB = connection;
|
|
12
|
-
this.onetimeLoadColumnContent = [];
|
|
13
|
-
}
|
|
14
|
-
async migrateTable(model) {
|
|
15
|
-
let data = model.filter((row) => row.tableName !== "sessions" && row.tableName.search("prisma") < 0);
|
|
16
|
-
let index = 1;
|
|
17
|
-
for (let row of data) {
|
|
18
|
-
console.log(chalk_1.default.bgBlue(`${index}/${data.length} ${row.tableName}`));
|
|
19
|
-
await this.createTableIfNotExist(row.tableName);
|
|
20
|
-
this.onetimeLoadColumnContent = [];
|
|
21
|
-
await this.loadColumnContentIfEmpty();
|
|
22
|
-
index++;
|
|
23
|
-
for (let col of row.columns) {
|
|
24
|
-
console.log(`${row.tableName} (${col.DATA_TYPE}) - ${col.COLUMN_NAME} `);
|
|
25
|
-
if (col.COLUMN_NAME === "id" && col.AUTO_INCREMENT === true) {
|
|
26
|
-
if ((await this.checkIsPrimaryKey(this.MyDB.dbInfo.database, row.tableName, col.COLUMN_NAME)) === false)
|
|
27
|
-
await this.MyDB.query(`ALTER TABLE ${row.tableName} ADD PRIMARY KEY(id);`);
|
|
28
|
-
}
|
|
29
|
-
const AutoIncrement = col.AUTO_INCREMENT ? "AUTO_INCREMENT" : "";
|
|
30
|
-
const columnDefault = col.COLUMN_DEFAULT
|
|
31
|
-
? `DEFAULT ${col.COLUMN_DEFAULT}`
|
|
32
|
-
: "";
|
|
33
|
-
const isColumnChange = await this.checkIsColumnDataTypeChange(row.tableName, col.COLUMN_NAME, col.DATA_TYPE, col.COLUMN_DEFAULT);
|
|
34
|
-
if (isColumnChange) {
|
|
35
|
-
const isColumnExist = await this.columnInTableExist(row.tableName, col.COLUMN_NAME);
|
|
36
|
-
if (!isColumnExist) {
|
|
37
|
-
await this.MyDB.query(`ALTER TABLE ${row.tableName} ADD COLUMN ${col.COLUMN_NAME} ${col.DATA_TYPE} ${AutoIncrement} ${columnDefault}`);
|
|
38
|
-
}
|
|
39
|
-
else {
|
|
40
|
-
await this.MyDB.query(`ALTER TABLE ${row.tableName} MODIFY COLUMN ${col.COLUMN_NAME} ${col.DATA_TYPE} ${AutoIncrement} ${columnDefault}`);
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
await this.alterUniqueKey(row.tableName, col.COLUMN_NAME, col.IS_UNIQUE);
|
|
44
|
-
await this.alterIndex(row.tableName, col.COLUMN_NAME, col.IS_INDEX);
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
console.log("migrate table done");
|
|
48
|
-
}
|
|
49
|
-
async createTableIfNotExist(tableName) {
|
|
50
|
-
await this.MyDB.query(`CREATE TABLE IF NOT EXISTS ${tableName} (id INT(11) AUTO_INCREMENT PRIMARY KEY)`);
|
|
51
|
-
}
|
|
52
|
-
async columnInTableExist(tableName, columnName) {
|
|
53
|
-
await this.loadColumnContentIfEmpty();
|
|
54
|
-
let result = this.onetimeLoadColumnContent.filter((row) => row.COLUMN_NAME === columnName && row.TABLE_NAME === tableName);
|
|
55
|
-
if (result.length)
|
|
56
|
-
return true;
|
|
57
|
-
else
|
|
58
|
-
return false;
|
|
59
|
-
}
|
|
60
|
-
async loadColumnContentIfEmpty() {
|
|
61
|
-
if (this.onetimeLoadColumnContent.length === 0) {
|
|
62
|
-
this.onetimeLoadColumnContent = (await this.MyDB.query(`SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = '${this.MyDB.dbInfo.database}'`, undefined, false));
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
async checkIsColumnDataTypeChange(tableName, columnName, dataType, columnDefault) {
|
|
66
|
-
await this.loadColumnContentIfEmpty();
|
|
67
|
-
let temp = this.onetimeLoadColumnContent.filter((row) => row.TABLE_NAME === tableName && row.COLUMN_NAME === columnName);
|
|
68
|
-
if (columnDefault === undefined)
|
|
69
|
-
columnDefault = "NULL";
|
|
70
|
-
if (columnDefault === null)
|
|
71
|
-
columnDefault = "NULL";
|
|
72
|
-
if (temp.length > 0) {
|
|
73
|
-
if (temp[0].DATA_TYPE !== dataType) {
|
|
74
|
-
console.log(chalk_1.default.green `${temp[0].DATA_TYPE} change to ${dataType}`);
|
|
75
|
-
return true;
|
|
76
|
-
}
|
|
77
|
-
if (temp[0].COLUMN_DEFAULT !== columnDefault) {
|
|
78
|
-
console.log(chalk_1.default.green `${temp[0].COLUMN_DEFAULT} change to ${columnDefault}`);
|
|
79
|
-
return true;
|
|
80
|
-
}
|
|
81
|
-
return false;
|
|
82
|
-
}
|
|
83
|
-
else {
|
|
84
|
-
console.log(chalk_1.default.red `${columnName} not found`);
|
|
85
|
-
return true;
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
async alterUniqueKey(tableName, columnName, IS_UNIQUE) {
|
|
89
|
-
const data = this.onetimeLoadColumnContent.find((row) => row.COLUMN_NAME === columnName && row.TABLE_NAME === tableName);
|
|
90
|
-
if (data) {
|
|
91
|
-
if ((data.COLUMN_KEY === "UNI" && !IS_UNIQUE) ||
|
|
92
|
-
(data.COLUMN_KEY !== "UNI" && IS_UNIQUE === true))
|
|
93
|
-
if (IS_UNIQUE === true) {
|
|
94
|
-
await this.MyDB.query(`ALTER TABLE ${tableName} ADD UNIQUE (${columnName});`);
|
|
95
|
-
}
|
|
96
|
-
else {
|
|
97
|
-
await this.MyDB.query(`ALTER TABLE ${tableName} DROP INDEX ${columnName};`);
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
async alterIndex(tableName, columnName, IS_INDEX) {
|
|
102
|
-
const data = this.onetimeLoadColumnContent.find((row) => row.COLUMN_NAME === columnName && row.TABLE_NAME === tableName);
|
|
103
|
-
if (data) {
|
|
104
|
-
if ((data.COLUMN_KEY === "MUL" && !IS_INDEX) ||
|
|
105
|
-
(data.COLUMN_KEY !== "MUL" && IS_INDEX === true))
|
|
106
|
-
if (IS_INDEX === true) {
|
|
107
|
-
await this.MyDB.query(`ALTER TABLE ${tableName} ADD INDEX (${columnName});`);
|
|
108
|
-
}
|
|
109
|
-
else {
|
|
110
|
-
await this.MyDB.query(`DROP INDEX ${columnName} ON ${tableName};`);
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
async migrateView(viewDirectory) {
|
|
115
|
-
if (!fs_1.default.existsSync(viewDirectory)) {
|
|
116
|
-
return 0;
|
|
117
|
-
}
|
|
118
|
-
let fileList = (await fs_1.default.readdirSync(path_1.default.join(viewDirectory)));
|
|
119
|
-
let unSuccessUpdateViewList = fileList;
|
|
120
|
-
let loopCount = 0;
|
|
121
|
-
while (unSuccessUpdateViewList.length > 0) {
|
|
122
|
-
for (const viewFileName of unSuccessUpdateViewList) {
|
|
123
|
-
const filePath = path_1.default.join(viewDirectory, viewFileName);
|
|
124
|
-
const content = fs_1.default.readFileSync(filePath, "utf8");
|
|
125
|
-
process.stdout.write(`loop : ${loopCount} unUpdate : ${unSuccessUpdateViewList.length} update view ${viewFileName}`);
|
|
126
|
-
await this.MyDB.query(content)
|
|
127
|
-
.then((result) => {
|
|
128
|
-
process.stdout.write(" " + chalk_1.default.bgGreen(`success`));
|
|
129
|
-
unSuccessUpdateViewList = unSuccessUpdateViewList.filter((row) => row !== viewFileName);
|
|
130
|
-
})
|
|
131
|
-
.catch((_error) => {
|
|
132
|
-
process.stdout.write(" " + chalk_1.default.bgRed(`fail`));
|
|
133
|
-
})
|
|
134
|
-
.finally(() => {
|
|
135
|
-
console.log("");
|
|
136
|
-
});
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
async checkIsPrimaryKey(databaseName, tableName, columnName) {
|
|
141
|
-
let result = await this.MyDB.query(`SELECT *
|
|
142
|
-
FROM INFORMATION_SCHEMA.COLUMNS
|
|
143
|
-
WHERE TABLE_SCHEMA='${databaseName}'
|
|
144
|
-
AND TABLE_NAME='${tableName}'
|
|
145
|
-
AND COLUMN_NAME='${columnName}'`);
|
|
146
|
-
console.log("result", result);
|
|
147
|
-
if (result.length) {
|
|
148
|
-
if (result[0].COLUMN_KEY === "PRI")
|
|
149
|
-
return true;
|
|
150
|
-
else
|
|
151
|
-
return false;
|
|
152
|
-
}
|
|
153
|
-
else
|
|
154
|
-
return false;
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
exports.default = MyDBMigrator;
|
package/dist/index.d.ts
DELETED
|
@@ -1,4 +0,0 @@
|
|
|
1
|
-
export { MyModel, columnContent, columnType } from "./myModel";
|
|
2
|
-
export { DatabaseConfigInterface } from "./ModelGenerator/ModelGenerator";
|
|
3
|
-
export { default as ModelGenerator } from "./ModelGenerator/ModelGenerator";
|
|
4
|
-
export { default as GGMySQLConnector } from "./GGMySQLConnector/GGMySQLConnector";
|
package/dist/index.js
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.GGMySQLConnector = exports.ModelGenerator = void 0;
|
|
7
|
-
var ModelGenerator_1 = require("./ModelGenerator/ModelGenerator");
|
|
8
|
-
Object.defineProperty(exports, "ModelGenerator", { enumerable: true, get: function () { return __importDefault(ModelGenerator_1).default; } });
|
|
9
|
-
var GGMySQLConnector_1 = require("./GGMySQLConnector/GGMySQLConnector");
|
|
10
|
-
Object.defineProperty(exports, "GGMySQLConnector", { enumerable: true, get: function () { return __importDefault(GGMySQLConnector_1).default; } });
|