gg-mysql-connector 1.0.37 → 1.0.39
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/app_INF.ts +24 -0
- package/app_model_const.ts +24 -0
- package/dist/app_INF.d.ts +8 -1
- package/dist/app_model_const.d.ts +24 -0
- package/dist/app_model_const.js +27 -0
- package/dist/seed_INF.d.ts +6 -0
- package/dist/seed_INF.js +2 -0
- package/dist/src/GGMySQLConnector/GGMySQLConnector.d.ts +72 -0
- package/dist/src/GGMySQLConnector/GGMySQLConnector.js +296 -0
- package/dist/src/ModelGenerator/ModelGenerator.d.ts +39 -0
- package/dist/src/ModelGenerator/ModelGenerator.js +317 -0
- package/dist/src/ModelGenerator/SeedGenerator.d.ts +0 -0
- package/dist/src/ModelGenerator/SeedGenerator.js +30 -0
- package/dist/src/MyDBMigrator/MyDBMigrator.d.ts +24 -0
- package/dist/src/MyDBMigrator/MyDBMigrator.js +196 -0
- package/dist/src/app_INF.d.ts +17 -0
- package/dist/src/app_INF.js +2 -0
- package/dist/src/index.d.ts +4 -0
- package/dist/src/index.js +10 -0
- package/dist/src/myModel.d.ts +21 -0
- package/dist/src/myModel.js +2 -0
- package/package.json +4 -3
- package/seed_INF.ts +3 -0
- package/src/ModelGenerator/ModelGenerator.ts +44 -19
- package/src/ModelGenerator/SeedGenerator.ts +32 -2
- package/src/MyDBMigrator/MyDBMigrator.ts +65 -7
- package/src/myModel.ts +7 -5
package/app_INF.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export default interface app_INF {
|
|
2
|
+
item: {
|
|
3
|
+
id: number
|
|
4
|
+
name: "pen" | "ruler"
|
|
5
|
+
price: number
|
|
6
|
+
description: string
|
|
7
|
+
amount: number
|
|
8
|
+
}
|
|
9
|
+
user: {
|
|
10
|
+
id: number
|
|
11
|
+
name: string
|
|
12
|
+
}
|
|
13
|
+
test_view: {
|
|
14
|
+
id: number
|
|
15
|
+
name: string
|
|
16
|
+
itemName: "pen" | "ruler"
|
|
17
|
+
}
|
|
18
|
+
item_view: {
|
|
19
|
+
id: number
|
|
20
|
+
name: "pen" | "ruler"
|
|
21
|
+
amount: number
|
|
22
|
+
price: number
|
|
23
|
+
}
|
|
24
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export const app_model_const = {
|
|
2
|
+
item: {
|
|
3
|
+
id: "number",
|
|
4
|
+
name: ["pen", "ruler"] as string[],
|
|
5
|
+
price: "number",
|
|
6
|
+
description: "string",
|
|
7
|
+
amount: "number",
|
|
8
|
+
},
|
|
9
|
+
user: {
|
|
10
|
+
id: "number",
|
|
11
|
+
name: "string",
|
|
12
|
+
},
|
|
13
|
+
test_view: {
|
|
14
|
+
id: "number",
|
|
15
|
+
name: "string",
|
|
16
|
+
itemName: ["pen", "ruler"] as string[],
|
|
17
|
+
},
|
|
18
|
+
item_view: {
|
|
19
|
+
id: "number",
|
|
20
|
+
name: ["pen", "ruler"] as string[],
|
|
21
|
+
amount: "number",
|
|
22
|
+
price: "number",
|
|
23
|
+
},
|
|
24
|
+
} as const
|
package/dist/app_INF.d.ts
CHANGED
|
@@ -4,6 +4,7 @@ export default interface app_INF {
|
|
|
4
4
|
name: "pen" | "ruler";
|
|
5
5
|
price: number;
|
|
6
6
|
description: string;
|
|
7
|
+
amount: number;
|
|
7
8
|
};
|
|
8
9
|
user: {
|
|
9
10
|
id: number;
|
|
@@ -12,6 +13,12 @@ export default interface app_INF {
|
|
|
12
13
|
test_view: {
|
|
13
14
|
id: number;
|
|
14
15
|
name: string;
|
|
15
|
-
itemName:
|
|
16
|
+
itemName: "pen" | "ruler";
|
|
17
|
+
};
|
|
18
|
+
item_view: {
|
|
19
|
+
id: number;
|
|
20
|
+
name: "pen" | "ruler";
|
|
21
|
+
amount: number;
|
|
22
|
+
price: number;
|
|
16
23
|
};
|
|
17
24
|
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export declare const app_model_const: {
|
|
2
|
+
readonly item: {
|
|
3
|
+
readonly id: "number";
|
|
4
|
+
readonly name: string[];
|
|
5
|
+
readonly price: "number";
|
|
6
|
+
readonly description: "string";
|
|
7
|
+
readonly amount: "number";
|
|
8
|
+
};
|
|
9
|
+
readonly user: {
|
|
10
|
+
readonly id: "number";
|
|
11
|
+
readonly name: "string";
|
|
12
|
+
};
|
|
13
|
+
readonly test_view: {
|
|
14
|
+
readonly id: "number";
|
|
15
|
+
readonly name: "string";
|
|
16
|
+
readonly itemName: string[];
|
|
17
|
+
};
|
|
18
|
+
readonly item_view: {
|
|
19
|
+
readonly id: "number";
|
|
20
|
+
readonly name: string[];
|
|
21
|
+
readonly amount: "number";
|
|
22
|
+
readonly price: "number";
|
|
23
|
+
};
|
|
24
|
+
};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.app_model_const = void 0;
|
|
4
|
+
exports.app_model_const = {
|
|
5
|
+
item: {
|
|
6
|
+
id: "number",
|
|
7
|
+
name: ["pen", "ruler"],
|
|
8
|
+
price: "number",
|
|
9
|
+
description: "string",
|
|
10
|
+
amount: "number",
|
|
11
|
+
},
|
|
12
|
+
user: {
|
|
13
|
+
id: "number",
|
|
14
|
+
name: "string",
|
|
15
|
+
},
|
|
16
|
+
test_view: {
|
|
17
|
+
id: "number",
|
|
18
|
+
name: "string",
|
|
19
|
+
itemName: ["pen", "ruler"],
|
|
20
|
+
},
|
|
21
|
+
item_view: {
|
|
22
|
+
id: "number",
|
|
23
|
+
name: ["pen", "ruler"],
|
|
24
|
+
amount: "number",
|
|
25
|
+
price: "number",
|
|
26
|
+
},
|
|
27
|
+
};
|
package/dist/seed_INF.js
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import mysql, { QueryResult, RowDataPacket } from "mysql2";
|
|
2
|
+
import { DatabaseConfigInterface } from "../ModelGenerator/ModelGenerator";
|
|
3
|
+
export interface ClassDBInterface<Main> {
|
|
4
|
+
connection: mysql.Connection;
|
|
5
|
+
query<T>(statement: string, parameter?: object, isPrint?: boolean): Promise<T | QueryResult>;
|
|
6
|
+
getColumnList(tableName: string): Promise<string[]>;
|
|
7
|
+
select<T extends keyof Main>(tableName: T extends string ? T : string): Promise<Main[T][]>;
|
|
8
|
+
selectByID<T extends keyof Main>(tableName: T extends string ? T : string, id: number): Promise<Main[T][]>;
|
|
9
|
+
selectByMatchParams<T extends keyof Main>(tableName: T extends string ? T : string, params: object): Promise<Main[T][]>;
|
|
10
|
+
selectBySearchTextInRow(tableName: string, textToSearch: string): Promise<RowDataPacket[]>;
|
|
11
|
+
insert<T extends keyof Main>(tableName: T, params: any): Promise<mysql.OkPacket>;
|
|
12
|
+
update(tableName: string, parameter: object | object[]): Promise<mysql.OkPacket>;
|
|
13
|
+
updateOnlyID<T extends keyof Main extends string ? keyof Main : string>(tableName: T, params: {
|
|
14
|
+
oldID: number;
|
|
15
|
+
newID: number;
|
|
16
|
+
}): Promise<mysql.OkPacket | null>;
|
|
17
|
+
deleteByID(tableName: string, id: number): Promise<mysql.OkPacket>;
|
|
18
|
+
deleteByMatchParams(tableName: string, parameter: any): Promise<mysql.OkPacket | null>;
|
|
19
|
+
getTableNameList(): Promise<string[]>;
|
|
20
|
+
getViewNameList(): Promise<string[]>;
|
|
21
|
+
getViewNameIfExist(tableName: string): Promise<string>;
|
|
22
|
+
loadTableAndViewNameList(): Promise<string[]>;
|
|
23
|
+
createDatabaseIfNotExist(tempConnection: mysql.Connection): Promise<void>;
|
|
24
|
+
}
|
|
25
|
+
export type Unarray<T> = T extends Array<infer U> ? U : T;
|
|
26
|
+
export default class GGMySQLConnector<Main> implements ClassDBInterface<Main> {
|
|
27
|
+
DBInfo: DatabaseConfigInterface;
|
|
28
|
+
connection: mysql.Connection;
|
|
29
|
+
isPrintStatement: boolean;
|
|
30
|
+
tableAndViewNameList: string[];
|
|
31
|
+
columnAndTableListCache: {
|
|
32
|
+
COLUMN_NAME: string;
|
|
33
|
+
TABLE_NAME: string;
|
|
34
|
+
DATA_TYPE: string;
|
|
35
|
+
}[] | null;
|
|
36
|
+
isConnected: boolean;
|
|
37
|
+
constructor(DBInfo: DatabaseConfigInterface);
|
|
38
|
+
createDatabaseIfNotExist(tempConnection: mysql.Connection): Promise<void>;
|
|
39
|
+
selectBySearchTextInRow(tableName: string, textToSearch: string): Promise<mysql.RowDataPacket[]>;
|
|
40
|
+
loadTableAndViewNameList(): Promise<string[]>;
|
|
41
|
+
getViewNameIfExist(tableName: string): Promise<string>;
|
|
42
|
+
selectByMatchParams<T extends keyof Main>(tableName: T extends string ? T : string, params: Partial<{
|
|
43
|
+
[K in keyof Main[T]]: string | number | undefined | null;
|
|
44
|
+
}>): Promise<Main[T][]>;
|
|
45
|
+
selectByID<T extends keyof Main>(tableName: T extends string ? T : string, id: number): Promise<Main[T][]>;
|
|
46
|
+
select<T extends keyof Main>(tableName: T extends string ? T : string): Promise<Main[T][]>;
|
|
47
|
+
getTableNameList(): Promise<string[]>;
|
|
48
|
+
getViewNameList(): Promise<string[]>;
|
|
49
|
+
deleteByMatchParams<T extends keyof Main>(tableName: T extends string ? T : string, params: Partial<{
|
|
50
|
+
[K in keyof Main[T]]: T | string | number | undefined | null;
|
|
51
|
+
}>): Promise<mysql.OkPacket | null>;
|
|
52
|
+
deleteByID<T extends keyof Main>(tableName: T extends string ? T : string, id: number): Promise<mysql.OkPacket>;
|
|
53
|
+
insert<T extends keyof Main>(tableName: T, parameter: Partial<{
|
|
54
|
+
[R in keyof Unarray<Main[T]>]: any;
|
|
55
|
+
}> | Partial<{
|
|
56
|
+
[R in keyof Unarray<Main[T]>]: any;
|
|
57
|
+
}>[]): Promise<mysql.OkPacket>;
|
|
58
|
+
updateOnlyID<T extends keyof Main extends string ? keyof Main : string>(tableName: T, params: {
|
|
59
|
+
oldID: number;
|
|
60
|
+
newID: number;
|
|
61
|
+
}): Promise<mysql.OkPacket | null>;
|
|
62
|
+
update<T extends keyof Main>(tableName: T extends string ? T : string, parameter: Partial<{
|
|
63
|
+
[K in keyof Main[T]]: Main[T][K];
|
|
64
|
+
}>): Promise<mysql.OkPacket>;
|
|
65
|
+
init(): Promise<void>;
|
|
66
|
+
loadColumnListToCache(): Promise<void>;
|
|
67
|
+
getColumnList(tableName: string): Promise<string[]>;
|
|
68
|
+
getColumnFullList(tableName: string): Promise<mysql.RowDataPacket[]>;
|
|
69
|
+
waitUntilInitSuccess(interval: number): true | Promise<unknown>;
|
|
70
|
+
query<T>(statment: string, parameter?: any[], isPrint?: boolean): Promise<T | QueryResult>;
|
|
71
|
+
private printResultLength;
|
|
72
|
+
}
|
|
@@ -0,0 +1,296 @@
|
|
|
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 mysql2_1 = __importDefault(require("mysql2"));
|
|
9
|
+
class GGMySQLConnector {
|
|
10
|
+
constructor(DBInfo) {
|
|
11
|
+
this.printResultLength = (results, executionTime, queryResult) => {
|
|
12
|
+
if (results.length) {
|
|
13
|
+
const executionTimeMessage = executionTime > 1000
|
|
14
|
+
? chalk_1.default.bgRed(`${executionTime} ` + " ms")
|
|
15
|
+
: chalk_1.default.yellow(`${executionTime} ` + " ms");
|
|
16
|
+
console.log(`${this.DBInfo.database} > ${chalk_1.default.green(queryResult.sql)} ${chalk_1.default.cyan(`found ${results.length} rows.`)} ${executionTimeMessage}`);
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
this.isPrintStatement = false;
|
|
20
|
+
this.tableAndViewNameList = [];
|
|
21
|
+
this.columnAndTableListCache = null;
|
|
22
|
+
this.DBInfo = DBInfo;
|
|
23
|
+
this.isConnected = false;
|
|
24
|
+
}
|
|
25
|
+
async createDatabaseIfNotExist(tempConnection) {
|
|
26
|
+
const currentDatabaseName = this.DBInfo.database;
|
|
27
|
+
console.log("currentDatabaseName", currentDatabaseName);
|
|
28
|
+
return new Promise((resolve, reject) => {
|
|
29
|
+
tempConnection.query(`CREATE DATABASE IF NOT EXISTS ${currentDatabaseName}`, () => {
|
|
30
|
+
resolve();
|
|
31
|
+
});
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
async selectBySearchTextInRow(tableName, textToSearch) {
|
|
35
|
+
const result = (await this.query(`SELECT * FROM ${tableName}`));
|
|
36
|
+
const wordList = textToSearch.split(" ");
|
|
37
|
+
const temp = result.filter((row) => {
|
|
38
|
+
const rawText = Object.values(row).flat().join(" ");
|
|
39
|
+
const wordFilterResult = wordList.filter((wRow) => rawText.search(wRow) >= 0);
|
|
40
|
+
if (wordFilterResult.length === wordList.length)
|
|
41
|
+
return true;
|
|
42
|
+
return false;
|
|
43
|
+
});
|
|
44
|
+
return temp;
|
|
45
|
+
}
|
|
46
|
+
async loadTableAndViewNameList() {
|
|
47
|
+
let result = (await this.query("SELECT TABLE_NAME FROM information_schema.tables WHERE table_schema = ?", [this.DBInfo.database], false));
|
|
48
|
+
this.tableAndViewNameList = result.map((row) => row.TABLE_NAME);
|
|
49
|
+
return this.tableAndViewNameList;
|
|
50
|
+
}
|
|
51
|
+
async getViewNameIfExist(tableName) {
|
|
52
|
+
const tableList = this.tableAndViewNameList;
|
|
53
|
+
const viewPostFixArray = ["_view", "__view", "__view_list"];
|
|
54
|
+
for (let i = 0; i < viewPostFixArray.length; i++) {
|
|
55
|
+
const expectViewName = tableName + viewPostFixArray[i];
|
|
56
|
+
for (let j = 0; j < tableList.length; j++) {
|
|
57
|
+
const row = tableList[j];
|
|
58
|
+
if (row === expectViewName) {
|
|
59
|
+
return expectViewName;
|
|
60
|
+
}
|
|
61
|
+
else if (i == viewPostFixArray.length - 1 &&
|
|
62
|
+
j == tableList.length - 1) {
|
|
63
|
+
return tableName;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
return tableName;
|
|
68
|
+
}
|
|
69
|
+
async selectByMatchParams(tableName, params) {
|
|
70
|
+
const columnList = await this.getColumnList(tableName);
|
|
71
|
+
const keyList = Object.keys(params);
|
|
72
|
+
const keyToSearchList = [];
|
|
73
|
+
const valueToSearchList = [];
|
|
74
|
+
for (const key of keyList) {
|
|
75
|
+
const temp = columnList.find((fkey) => key === fkey);
|
|
76
|
+
if (temp) {
|
|
77
|
+
keyToSearchList.push(` ${key} = ? `);
|
|
78
|
+
valueToSearchList.push(params[key]);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
if (keyToSearchList.length === 0) {
|
|
82
|
+
return [];
|
|
83
|
+
}
|
|
84
|
+
let result = (await this.query(`SELECT * FROM ${tableName} WHERE ${keyToSearchList.join(" AND ")}`, valueToSearchList));
|
|
85
|
+
return result;
|
|
86
|
+
}
|
|
87
|
+
async selectByID(tableName, id) {
|
|
88
|
+
let result = (await this.query(`SELECT * FROM ${tableName} WHERE id = ? LIMIT 1`, [id]));
|
|
89
|
+
return result;
|
|
90
|
+
}
|
|
91
|
+
async select(tableName) {
|
|
92
|
+
let result = (await this.query(`SELECT * FROM ${tableName}`));
|
|
93
|
+
return result;
|
|
94
|
+
}
|
|
95
|
+
async getTableNameList() {
|
|
96
|
+
let data = [];
|
|
97
|
+
let result = (await this.query("SELECT * FROM information_schema.tables WHERE table_schema = ? AND TABLE_TYPE = 'BASE TABLE'", [this.DBInfo.database], false));
|
|
98
|
+
for (let row of result)
|
|
99
|
+
data.push(row.TABLE_NAME || row.row.table_name);
|
|
100
|
+
return data;
|
|
101
|
+
}
|
|
102
|
+
async getViewNameList() {
|
|
103
|
+
let data = [];
|
|
104
|
+
let result = (await this.query("SELECT * FROM information_schema.tables WHERE table_schema = ? AND TABLE_TYPE = 'VIEW'", [this.DBInfo.database], false));
|
|
105
|
+
for (let row of result)
|
|
106
|
+
data.push(row.TABLE_NAME || row.row.table_name);
|
|
107
|
+
return data;
|
|
108
|
+
}
|
|
109
|
+
async deleteByMatchParams(tableName, params) {
|
|
110
|
+
const columnList = await this.getColumnList(tableName);
|
|
111
|
+
const keyList = Object.keys(params);
|
|
112
|
+
const keyToSearchList = [];
|
|
113
|
+
const valueToSearchList = [];
|
|
114
|
+
for (const key of keyList) {
|
|
115
|
+
const temp = columnList.find((fkey) => key === fkey);
|
|
116
|
+
if (temp) {
|
|
117
|
+
keyToSearchList.push(` ${key} = ? `);
|
|
118
|
+
valueToSearchList.push(params[key]);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
if (keyToSearchList.length === 0)
|
|
122
|
+
return null;
|
|
123
|
+
let result = (await this.query(`DELETE FROM ${tableName} WHERE ${keyToSearchList.join(" AND ")}`, valueToSearchList));
|
|
124
|
+
// this.webSocket.sendByURL("/websocket/watchTableChange", tableName)
|
|
125
|
+
return result;
|
|
126
|
+
}
|
|
127
|
+
async deleteByID(tableName, id) {
|
|
128
|
+
let result = (await this.query(`DELETE FROM ${tableName} WHERE id = ?`, [
|
|
129
|
+
id,
|
|
130
|
+
]));
|
|
131
|
+
console.log("Delete Success");
|
|
132
|
+
// this.webSocket.sendByURL("/websocket/watchTableChange", tableName)
|
|
133
|
+
return result;
|
|
134
|
+
}
|
|
135
|
+
async insert(tableName, parameter) {
|
|
136
|
+
let params = [];
|
|
137
|
+
if (Array.isArray(parameter) === true) {
|
|
138
|
+
params = parameter;
|
|
139
|
+
}
|
|
140
|
+
else
|
|
141
|
+
params = [parameter];
|
|
142
|
+
const columnList = await this.getColumnList(tableName);
|
|
143
|
+
const exampleParams = params[0];
|
|
144
|
+
const keyList = Object.keys(exampleParams);
|
|
145
|
+
const keyListToInsert = [];
|
|
146
|
+
for (const row of keyList) {
|
|
147
|
+
const temp = columnList.find((fRow) => row === fRow);
|
|
148
|
+
if (temp)
|
|
149
|
+
keyListToInsert.push(row);
|
|
150
|
+
}
|
|
151
|
+
const valueArrayToInsertInQuery = [];
|
|
152
|
+
for (let i = 0; i < params.length; i++) {
|
|
153
|
+
const valueListToInsert = [];
|
|
154
|
+
for (const row of keyList) {
|
|
155
|
+
const temp = columnList.find((fRow) => row === fRow);
|
|
156
|
+
if (temp) {
|
|
157
|
+
valueListToInsert.push(params[i][row]);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
valueArrayToInsertInQuery.push(valueListToInsert);
|
|
161
|
+
}
|
|
162
|
+
console.log(valueArrayToInsertInQuery.slice(0, 5));
|
|
163
|
+
const result = (await this.query(`INSERT INTO ${tableName} (${keyListToInsert.join(",")}) VALUES ?`, [valueArrayToInsertInQuery]));
|
|
164
|
+
console.log("Insert Success");
|
|
165
|
+
// this.webSocket.sendByURL("/websocket/watchTableChange", String(tableName))
|
|
166
|
+
return result;
|
|
167
|
+
}
|
|
168
|
+
async updateOnlyID(tableName, params) {
|
|
169
|
+
const isNewIdExist = await this.query(`SELECT * FROM ${tableName} WHERE id = ? `, [params.newID]);
|
|
170
|
+
if (isNewIdExist.length >= 1) {
|
|
171
|
+
console.log(`error : newID (${params.newID}) is already exist`);
|
|
172
|
+
return null;
|
|
173
|
+
}
|
|
174
|
+
let result = (await this.query(`UPDATE ${tableName} SET id = ? WHERE id = ?`, [params.newID, params.oldID], false));
|
|
175
|
+
console.log("UpdateOnlyID Success");
|
|
176
|
+
// this.webSocket.sendByURL("/websocket/watchTableChange", String(tableName))
|
|
177
|
+
return result;
|
|
178
|
+
}
|
|
179
|
+
async update(tableName, parameter) {
|
|
180
|
+
const columnList = await this.getColumnList(tableName);
|
|
181
|
+
console.log(parameter);
|
|
182
|
+
const keyList = Object.keys(parameter);
|
|
183
|
+
const keyListToInsert = [];
|
|
184
|
+
const valueListToInsert = [];
|
|
185
|
+
for (const row of keyList) {
|
|
186
|
+
const temp = columnList.find((fRow) => row === fRow);
|
|
187
|
+
if (temp) {
|
|
188
|
+
keyListToInsert.push(row);
|
|
189
|
+
valueListToInsert.push(parameter[row]);
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
// build set sql
|
|
193
|
+
const temp = [];
|
|
194
|
+
let index = 0;
|
|
195
|
+
for (const row of keyListToInsert) {
|
|
196
|
+
if (valueListToInsert[index] === null) {
|
|
197
|
+
temp.push(`${row} = NULL`);
|
|
198
|
+
}
|
|
199
|
+
else {
|
|
200
|
+
let value = valueListToInsert[index];
|
|
201
|
+
if (typeof value === "string") {
|
|
202
|
+
value = value.replace(/'/g, "");
|
|
203
|
+
}
|
|
204
|
+
temp.push(`${row} = '${value}'`);
|
|
205
|
+
}
|
|
206
|
+
index++;
|
|
207
|
+
}
|
|
208
|
+
let result = (await this.query(`UPDATE ${tableName} SET ${temp.join(",")} WHERE id = ${parameter.id}`, [], false));
|
|
209
|
+
console.log("Update Success");
|
|
210
|
+
// this.webSocket.sendByURL("/websocket/watchTableChange", String(tableName))
|
|
211
|
+
return result;
|
|
212
|
+
}
|
|
213
|
+
async init() {
|
|
214
|
+
const currentConnection = mysql2_1.default.createConnection({
|
|
215
|
+
host: this.DBInfo.host,
|
|
216
|
+
user: this.DBInfo.user,
|
|
217
|
+
password: this.DBInfo.password,
|
|
218
|
+
});
|
|
219
|
+
this.isConnected = true;
|
|
220
|
+
await this.createDatabaseIfNotExist(currentConnection);
|
|
221
|
+
// await currentConnection.end()
|
|
222
|
+
this.connection = mysql2_1.default.createConnection(Object.assign({}, this.DBInfo));
|
|
223
|
+
// await this.query("SET global sql_mode=''")
|
|
224
|
+
// await this.query("SET global query_cache_type='ON'")
|
|
225
|
+
// await this.query("SET global query_cache_size=16777216")
|
|
226
|
+
// await this.query("SET global query_cache_limit=16777216")
|
|
227
|
+
await this.loadTableAndViewNameList();
|
|
228
|
+
await this.loadColumnListToCache();
|
|
229
|
+
}
|
|
230
|
+
async loadColumnListToCache() {
|
|
231
|
+
const result = (await this.query(`SELECT COLUMN_NAME,TABLE_NAME,DATA_TYPE FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = '${this.DBInfo.database}'`, undefined, false));
|
|
232
|
+
this.columnAndTableListCache = result;
|
|
233
|
+
}
|
|
234
|
+
async getColumnList(tableName) {
|
|
235
|
+
if (this.columnAndTableListCache !== null) {
|
|
236
|
+
return this.columnAndTableListCache
|
|
237
|
+
.filter((row) => row.TABLE_NAME === tableName)
|
|
238
|
+
.map((row) => row.COLUMN_NAME);
|
|
239
|
+
}
|
|
240
|
+
const result = await this.query(`SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = '${this.DBInfo.database}' AND TABLE_NAME = '${tableName}'`, undefined, false);
|
|
241
|
+
return result.map((row) => row.COLUMN_NAME);
|
|
242
|
+
}
|
|
243
|
+
async getColumnFullList(tableName) {
|
|
244
|
+
const result = (await this.query(`SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = '${this.DBInfo.database}' AND TABLE_NAME = '${tableName}'`, undefined, false));
|
|
245
|
+
return result;
|
|
246
|
+
}
|
|
247
|
+
waitUntilInitSuccess(interval) {
|
|
248
|
+
if (this.isConnected === true)
|
|
249
|
+
return true;
|
|
250
|
+
return new Promise((resolve, reject) => {
|
|
251
|
+
setTimeout(() => {
|
|
252
|
+
if (this.isConnected === false) {
|
|
253
|
+
console.log(`${this.DBInfo.database} > wait for database connection . . .`);
|
|
254
|
+
this.waitUntilInitSuccess(interval);
|
|
255
|
+
}
|
|
256
|
+
else {
|
|
257
|
+
return resolve(true);
|
|
258
|
+
}
|
|
259
|
+
}, interval);
|
|
260
|
+
});
|
|
261
|
+
}
|
|
262
|
+
async query(statment, parameter, isPrint) {
|
|
263
|
+
await this.waitUntilInitSuccess(1000);
|
|
264
|
+
return new Promise((resolve, reject) => {
|
|
265
|
+
const uniqueTime = `${(0, crypto_1.randomUUID)()} ${statment}`;
|
|
266
|
+
console.time(uniqueTime);
|
|
267
|
+
const startTime = new Date();
|
|
268
|
+
const queryResult = this.connection.query(statment, parameter, (error, results, fields) => {
|
|
269
|
+
if (error) {
|
|
270
|
+
// _getCallerFile()
|
|
271
|
+
console.log(chalk_1.default.bgRed("Query Error"));
|
|
272
|
+
console.log(chalk_1.default.bgRed(error));
|
|
273
|
+
console.log(chalk_1.default.bgRed("Statement : "));
|
|
274
|
+
console.log(statment);
|
|
275
|
+
console.log(chalk_1.default.bgRed("Parameter : "));
|
|
276
|
+
console.log(parameter);
|
|
277
|
+
console.log("------------------------------------------------");
|
|
278
|
+
console.log(chalk_1.default.bgRed(queryResult.sql));
|
|
279
|
+
reject(error);
|
|
280
|
+
}
|
|
281
|
+
else {
|
|
282
|
+
const endTime = new Date();
|
|
283
|
+
const executionTime = endTime.getTime() - startTime.getTime();
|
|
284
|
+
if (isPrint === true) {
|
|
285
|
+
this.printResultLength(results, executionTime, queryResult);
|
|
286
|
+
}
|
|
287
|
+
else if (this.isPrintStatement) {
|
|
288
|
+
this.printResultLength(results, executionTime, queryResult);
|
|
289
|
+
}
|
|
290
|
+
resolve(results);
|
|
291
|
+
}
|
|
292
|
+
});
|
|
293
|
+
});
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
exports.default = GGMySQLConnector;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import mysql from "mysql2/promise";
|
|
2
|
+
import { MyModel, MyViewModel } from "../myModel";
|
|
3
|
+
export interface DatabaseConfigInterface {
|
|
4
|
+
host: string;
|
|
5
|
+
user: string;
|
|
6
|
+
password: string;
|
|
7
|
+
database: string;
|
|
8
|
+
}
|
|
9
|
+
export default class ModelGenerator {
|
|
10
|
+
model: MyModel[];
|
|
11
|
+
dbInfo: DatabaseConfigInterface;
|
|
12
|
+
connection: mysql.Connection;
|
|
13
|
+
isConnected: boolean;
|
|
14
|
+
constructor(dbInfo: DatabaseConfigInterface, model: MyModel[]);
|
|
15
|
+
init(): Promise<void>;
|
|
16
|
+
private createDatabaseIfNotExist;
|
|
17
|
+
pushModelToDB(): Promise<void>;
|
|
18
|
+
pushViewToDB(viewDirectory: string): Promise<void>;
|
|
19
|
+
pushViewToDB_v2(viewModel: MyViewModel[]): Promise<void>;
|
|
20
|
+
waitUntilInitSuccess(interval: number): true | Promise<unknown>;
|
|
21
|
+
query<T>(statment: string, parameter?: any[], isPrint?: boolean): Promise<T>;
|
|
22
|
+
private printResultLength;
|
|
23
|
+
getTableNameList(): Promise<string[]>;
|
|
24
|
+
getViewNameList(): Promise<string[]>;
|
|
25
|
+
getColumnFullList(tableName: string): Promise<mysql.RowDataPacket[]>;
|
|
26
|
+
private isTableNameExistInModel;
|
|
27
|
+
private isColumnExistInModel;
|
|
28
|
+
getPossibleColumnValue(model: MyModel[], tableName: string, columnName: string, columnType: string): false | string[] | undefined;
|
|
29
|
+
generateModelInterface(params: {
|
|
30
|
+
appName: string;
|
|
31
|
+
model: MyModel[];
|
|
32
|
+
outputDirectory: string[];
|
|
33
|
+
}): Promise<string>;
|
|
34
|
+
generateModelConstStructure(params: {
|
|
35
|
+
appName: string;
|
|
36
|
+
model: MyModel[];
|
|
37
|
+
outputDirectory: string[];
|
|
38
|
+
}): Promise<string>;
|
|
39
|
+
}
|