gg-mysql-connector 1.0.5
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/.vscode/settings.json +3 -0
- package/dist/GGMySQLConnector.d.ts +70 -0
- package/dist/GGMySQLConnector.js +278 -0
- package/dist/ModelGenerator.d.ts +24 -0
- package/dist/ModelGenerator.js +189 -0
- package/dist/MyDBMigration.d.ts +22 -0
- package/dist/MyDBMigration.js +157 -0
- package/dist/app_INF.d.ts +17 -0
- package/dist/app_INF.js +2 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +10 -0
- package/dist/myModel.d.ts +17 -0
- package/dist/myModel.js +6 -0
- package/dist/test.d.ts +1 -0
- package/dist/test.js +47 -0
- package/package.json +22 -0
- package/src/GGMySQLConnector.ts +457 -0
- package/src/ModelGenerator.ts +238 -0
- package/src/MyDBMigration.ts +233 -0
- package/src/app_INF.ts +12 -0
- package/src/index.ts +4 -0
- package/src/myModel.ts +38 -0
- package/src/views/test_view.sql +4 -0
- package/tsconfig.json +110 -0
|
@@ -0,0 +1,157 @@
|
|
|
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;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export default interface app_INF {
|
|
2
|
+
item: {
|
|
3
|
+
id: number;
|
|
4
|
+
name: "pen" | "ruler";
|
|
5
|
+
price: number;
|
|
6
|
+
description: string;
|
|
7
|
+
};
|
|
8
|
+
user: {
|
|
9
|
+
id: number;
|
|
10
|
+
name: string;
|
|
11
|
+
};
|
|
12
|
+
test_view: {
|
|
13
|
+
id: number;
|
|
14
|
+
name: string;
|
|
15
|
+
itemName: string;
|
|
16
|
+
};
|
|
17
|
+
}
|
package/dist/app_INF.js
ADDED
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
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");
|
|
8
|
+
Object.defineProperty(exports, "ModelGenerator", { enumerable: true, get: function () { return __importDefault(ModelGenerator_1).default; } });
|
|
9
|
+
var GGMySQLConnector_1 = require("./GGMySQLConnector");
|
|
10
|
+
Object.defineProperty(exports, "GGMySQLConnector", { enumerable: true, get: function () { return __importDefault(GGMySQLConnector_1).default; } });
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export type columnType = "int" | "tinyint" | "float" | "double" | "text" | "varchar(8)" | "varchar(16)" | "varchar(32)" | "varchar(64)" | "varchar(128)" | "varchar(256)" | "varchar(512)" | "longtext" | "date" | "time" | "datetime";
|
|
2
|
+
export interface columnContent {
|
|
3
|
+
COLUMN_NAME: string;
|
|
4
|
+
DATA_TYPE: columnType;
|
|
5
|
+
COLUMN_DEFAULT?: string | number | null;
|
|
6
|
+
AUTO_INCREMENT?: boolean;
|
|
7
|
+
POSSIBLE_VALUE?: any[];
|
|
8
|
+
IS_UNIQUE?: boolean;
|
|
9
|
+
IS_INDEX?: boolean;
|
|
10
|
+
}
|
|
11
|
+
export interface MyModel {
|
|
12
|
+
tableName: string;
|
|
13
|
+
columns: [
|
|
14
|
+
...columnContent[]
|
|
15
|
+
];
|
|
16
|
+
meaning?: string;
|
|
17
|
+
}
|
package/dist/myModel.js
ADDED
package/dist/test.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/test.js
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const model = [
|
|
4
|
+
{
|
|
5
|
+
tableName: "user",
|
|
6
|
+
columns: [
|
|
7
|
+
{
|
|
8
|
+
COLUMN_NAME: "id",
|
|
9
|
+
DATA_TYPE: "int",
|
|
10
|
+
AUTO_INCREMENT: true,
|
|
11
|
+
},
|
|
12
|
+
{
|
|
13
|
+
COLUMN_NAME: "name",
|
|
14
|
+
DATA_TYPE: "varchar(512)",
|
|
15
|
+
},
|
|
16
|
+
],
|
|
17
|
+
meaning: "",
|
|
18
|
+
},
|
|
19
|
+
{
|
|
20
|
+
tableName: "item",
|
|
21
|
+
columns: [
|
|
22
|
+
{
|
|
23
|
+
COLUMN_NAME: "id",
|
|
24
|
+
DATA_TYPE: "int",
|
|
25
|
+
AUTO_INCREMENT: true,
|
|
26
|
+
},
|
|
27
|
+
{
|
|
28
|
+
COLUMN_NAME: "name",
|
|
29
|
+
DATA_TYPE: "varchar(512)",
|
|
30
|
+
POSSIBLE_VALUE: ["pen", "ruler"],
|
|
31
|
+
},
|
|
32
|
+
{
|
|
33
|
+
COLUMN_NAME: "price",
|
|
34
|
+
DATA_TYPE: "float",
|
|
35
|
+
},
|
|
36
|
+
{
|
|
37
|
+
COLUMN_NAME: "description",
|
|
38
|
+
DATA_TYPE: "varchar(512)",
|
|
39
|
+
},
|
|
40
|
+
],
|
|
41
|
+
meaning: "",
|
|
42
|
+
},
|
|
43
|
+
];
|
|
44
|
+
async function run() {
|
|
45
|
+
process.exit();
|
|
46
|
+
}
|
|
47
|
+
run();
|
package/package.json
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "gg-mysql-connector",
|
|
3
|
+
"version": "1.0.5",
|
|
4
|
+
"description": "",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"test": "echo \"Error: no test specified\" && exit 1",
|
|
8
|
+
"build": "tsc && npm version patch"
|
|
9
|
+
},
|
|
10
|
+
"author": "",
|
|
11
|
+
"license": "ISC",
|
|
12
|
+
"dependencies": {
|
|
13
|
+
"app-root-path": "^3.1.0",
|
|
14
|
+
"chalk": "^4.1.2",
|
|
15
|
+
"fs": "^0.0.1-security",
|
|
16
|
+
"mysql2": "^3.11.3",
|
|
17
|
+
"path": "^0.12.7"
|
|
18
|
+
},
|
|
19
|
+
"devDependencies": {
|
|
20
|
+
"@types/node": "^22.7.5"
|
|
21
|
+
}
|
|
22
|
+
}
|