gg-mysql-connector 1.0.107 → 1.0.111
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/dist/src/GGMySQLConnector/GGMySQLConnector.d.ts +4 -4
- package/dist/src/GGMySQLConnector/GGMySQLConnector.js +13 -10
- package/dist/src/MyDBMigrator/MyDBMigrator.js +0 -7
- package/package.json +1 -1
- package/src/GGMySQLConnector/GGMySQLConnector.ts +21 -16
- package/src/MyDBMigrator/MyDBMigrator.ts +0 -10
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import mysql, { QueryResult, RowDataPacket } from "mysql2";
|
|
2
2
|
import { DatabaseConfigInterface } from "../ModelGenerator/ModelGenerator";
|
|
3
3
|
export interface ClassDBInterface<Main> {
|
|
4
|
-
connection: mysql.
|
|
4
|
+
connection: mysql.Pool;
|
|
5
5
|
query<T>(statement: string, parameter?: object, isPrint?: boolean): Promise<T | QueryResult>;
|
|
6
6
|
getColumnList(tableName: string): Promise<string[]>;
|
|
7
7
|
select<T extends keyof Main>(tableName: T extends string ? T : string): Promise<Main[T][]>;
|
|
@@ -20,12 +20,12 @@ export interface ClassDBInterface<Main> {
|
|
|
20
20
|
getViewNameList(): Promise<string[]>;
|
|
21
21
|
getViewNameIfExist(tableName: string): Promise<string>;
|
|
22
22
|
loadTableAndViewNameList(): Promise<string[]>;
|
|
23
|
-
createDatabaseIfNotExist(tempConnection: mysql.
|
|
23
|
+
createDatabaseIfNotExist(tempConnection: mysql.Pool): Promise<void>;
|
|
24
24
|
}
|
|
25
25
|
export type Unarray<T> = T extends Array<infer U> ? U : T;
|
|
26
26
|
export default class GGMySQLConnector<Main> implements ClassDBInterface<Main> {
|
|
27
27
|
DBInfo: DatabaseConfigInterface;
|
|
28
|
-
connection: mysql.
|
|
28
|
+
connection: mysql.Pool;
|
|
29
29
|
isPrintStatement: boolean;
|
|
30
30
|
tableAndViewNameList: string[];
|
|
31
31
|
columnAndTableListCache: {
|
|
@@ -37,7 +37,7 @@ export default class GGMySQLConnector<Main> implements ClassDBInterface<Main> {
|
|
|
37
37
|
isConnected: boolean;
|
|
38
38
|
constructor(DBInfo: DatabaseConfigInterface);
|
|
39
39
|
selectByDateRange<T extends keyof Main>(tableName: T extends string ? T : string, params: object): Promise<Main[T][]>;
|
|
40
|
-
createDatabaseIfNotExist(tempConnection: mysql.
|
|
40
|
+
createDatabaseIfNotExist(tempConnection: mysql.Pool): Promise<void>;
|
|
41
41
|
selectBySearchTextInRow(tableName: string, textToSearch: string): Promise<mysql.RowDataPacket[]>;
|
|
42
42
|
loadTableAndViewNameList(): Promise<string[]>;
|
|
43
43
|
getViewNameIfExist(tableName: string): Promise<string>;
|
|
@@ -6,6 +6,10 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
6
6
|
const chalk_1 = __importDefault(require("chalk"));
|
|
7
7
|
const crypto_1 = require("crypto");
|
|
8
8
|
const mysql2_1 = __importDefault(require("mysql2"));
|
|
9
|
+
function log(input) {
|
|
10
|
+
const { action, table } = input;
|
|
11
|
+
console.log(`${action} ${table}`);
|
|
12
|
+
}
|
|
9
13
|
class GGMySQLConnector {
|
|
10
14
|
constructor(DBInfo) {
|
|
11
15
|
this.printResultLength = (results, executionTime, queryResult) => {
|
|
@@ -27,7 +31,7 @@ class GGMySQLConnector {
|
|
|
27
31
|
}
|
|
28
32
|
async createDatabaseIfNotExist(tempConnection) {
|
|
29
33
|
const currentDatabaseName = this.DBInfo.database;
|
|
30
|
-
console.log("currentDatabaseName", currentDatabaseName)
|
|
34
|
+
// console.log("currentDatabaseName", currentDatabaseName)
|
|
31
35
|
return new Promise((resolve, reject) => {
|
|
32
36
|
tempConnection.query(`CREATE DATABASE IF NOT EXISTS ${currentDatabaseName}`, () => {
|
|
33
37
|
resolve();
|
|
@@ -131,7 +135,7 @@ class GGMySQLConnector {
|
|
|
131
135
|
let result = (await this.query(`DELETE FROM ${tableName} WHERE id = ?`, [
|
|
132
136
|
id,
|
|
133
137
|
]));
|
|
134
|
-
console.log(
|
|
138
|
+
// console.log(`table ${tableName} id ${id} => Delete Success`)
|
|
135
139
|
// this.webSocket.sendByURL("/websocket/watchTableChange", tableName)
|
|
136
140
|
return result;
|
|
137
141
|
}
|
|
@@ -162,26 +166,25 @@ class GGMySQLConnector {
|
|
|
162
166
|
}
|
|
163
167
|
valueArrayToInsertInQuery.push(valueListToInsert);
|
|
164
168
|
}
|
|
165
|
-
console.log(valueArrayToInsertInQuery.slice(0, 5));
|
|
166
169
|
const result = (await this.query(`INSERT INTO ${tableName} (${keyListToInsert.join(",")}) VALUES ?`, [valueArrayToInsertInQuery]));
|
|
167
|
-
console.log(
|
|
170
|
+
// console.log(`table ${tableName as string} => Insert Success`)
|
|
168
171
|
// this.webSocket.sendByURL("/websocket/watchTableChange", String(tableName))
|
|
169
172
|
return result;
|
|
170
173
|
}
|
|
171
174
|
async updateOnlyID(tableName, params) {
|
|
172
175
|
const isNewIdExist = await this.query(`SELECT * FROM ${tableName} WHERE id = ? `, [params.newID]);
|
|
173
176
|
if (isNewIdExist.length >= 1) {
|
|
174
|
-
console.
|
|
177
|
+
console.error(`error : newID (${params.newID}) is already exist`);
|
|
175
178
|
return null;
|
|
176
179
|
}
|
|
177
180
|
let result = (await this.query(`UPDATE ${tableName} SET id = ? WHERE id = ?`, [params.newID, params.oldID], false));
|
|
178
|
-
console.log("UpdateOnlyID Success")
|
|
181
|
+
// console.log("UpdateOnlyID Success")
|
|
179
182
|
// this.webSocket.sendByURL("/websocket/watchTableChange", String(tableName))
|
|
180
183
|
return result;
|
|
181
184
|
}
|
|
182
185
|
async update(tableName, parameter) {
|
|
183
186
|
const columnList = await this.getColumnList(tableName);
|
|
184
|
-
console.log(parameter)
|
|
187
|
+
// console.log(parameter)
|
|
185
188
|
const keyList = Object.keys(parameter);
|
|
186
189
|
const keyListToInsert = [];
|
|
187
190
|
const valueListToInsert = [];
|
|
@@ -209,12 +212,12 @@ class GGMySQLConnector {
|
|
|
209
212
|
index++;
|
|
210
213
|
}
|
|
211
214
|
let result = (await this.query(`UPDATE ${tableName} SET ${temp.join(",")} WHERE id = ${parameter.id}`, [], false));
|
|
212
|
-
console.log(
|
|
215
|
+
// console.log(`table : ${tableName} => Update Success`)
|
|
213
216
|
// this.webSocket.sendByURL("/websocket/watchTableChange", String(tableName))
|
|
214
217
|
return result;
|
|
215
218
|
}
|
|
216
219
|
async init() {
|
|
217
|
-
const currentConnection = mysql2_1.default.
|
|
220
|
+
const currentConnection = mysql2_1.default.createPool({
|
|
218
221
|
host: this.DBInfo.host,
|
|
219
222
|
user: this.DBInfo.user,
|
|
220
223
|
password: this.DBInfo.password,
|
|
@@ -222,7 +225,7 @@ class GGMySQLConnector {
|
|
|
222
225
|
this.isConnected = true;
|
|
223
226
|
await this.createDatabaseIfNotExist(currentConnection);
|
|
224
227
|
// await currentConnection.end()
|
|
225
|
-
this.connection = mysql2_1.default.
|
|
228
|
+
this.connection = mysql2_1.default.createPool(Object.assign({}, this.DBInfo));
|
|
226
229
|
// await this.query("SET global sql_mode=''")
|
|
227
230
|
// await this.query("SET global query_cache_type='ON'")
|
|
228
231
|
// await this.query("SET global query_cache_size=16777216")
|
|
@@ -27,13 +27,6 @@ class MyDBMigrator {
|
|
|
27
27
|
await this.alterDataType(row.tableName, col);
|
|
28
28
|
await this.alterUniqueKey(row.tableName, col.COLUMN_NAME, col.IS_UNIQUE, indexList);
|
|
29
29
|
await this.alterIndex(row.tableName, col.COLUMN_NAME, col.IS_INDEX, indexList);
|
|
30
|
-
//!! delete this next version. need to execute only 1 time on production
|
|
31
|
-
try {
|
|
32
|
-
await this.deleteOldIndexVersion(row.tableName, col.COLUMN_NAME, indexList);
|
|
33
|
-
}
|
|
34
|
-
catch (error) {
|
|
35
|
-
console.log(error);
|
|
36
|
-
}
|
|
37
30
|
await this.alterPossibleEnum(row.tableName, col.COLUMN_NAME, col.POSSIBLE_VALUE, col.IS_NOT_NULL);
|
|
38
31
|
await this.alterForeignKey(row.tableName, col);
|
|
39
32
|
}
|
package/package.json
CHANGED
|
@@ -3,8 +3,16 @@ import { randomUUID } from "crypto"
|
|
|
3
3
|
import mysql, { QueryResult, RowDataPacket } from "mysql2"
|
|
4
4
|
import { DatabaseConfigInterface } from "../ModelGenerator/ModelGenerator"
|
|
5
5
|
|
|
6
|
+
function log(input: {
|
|
7
|
+
table: string
|
|
8
|
+
action: "select" | "update" | "delete" | "delete only id" | "delete by id"
|
|
9
|
+
}) {
|
|
10
|
+
const { action, table } = input
|
|
11
|
+
console.log(`${action} ${table}`)
|
|
12
|
+
}
|
|
13
|
+
|
|
6
14
|
export interface ClassDBInterface<Main> {
|
|
7
|
-
connection: mysql.
|
|
15
|
+
connection: mysql.Pool
|
|
8
16
|
query<T>(
|
|
9
17
|
statement: string,
|
|
10
18
|
parameter?: object,
|
|
@@ -48,12 +56,12 @@ export interface ClassDBInterface<Main> {
|
|
|
48
56
|
getViewNameList(): Promise<string[]>
|
|
49
57
|
getViewNameIfExist(tableName: string): Promise<string>
|
|
50
58
|
loadTableAndViewNameList(): Promise<string[]>
|
|
51
|
-
createDatabaseIfNotExist(tempConnection: mysql.
|
|
59
|
+
createDatabaseIfNotExist(tempConnection: mysql.Pool): Promise<void>
|
|
52
60
|
}
|
|
53
61
|
export type Unarray<T> = T extends Array<infer U> ? U : T
|
|
54
62
|
export default class GGMySQLConnector<Main> implements ClassDBInterface<Main> {
|
|
55
63
|
DBInfo: DatabaseConfigInterface
|
|
56
|
-
connection!: mysql.
|
|
64
|
+
connection!: mysql.Pool
|
|
57
65
|
isPrintStatement: boolean
|
|
58
66
|
tableAndViewNameList: string[]
|
|
59
67
|
columnAndTableListCache:
|
|
@@ -79,11 +87,9 @@ export default class GGMySQLConnector<Main> implements ClassDBInterface<Main> {
|
|
|
79
87
|
): Promise<Main[T][]> {
|
|
80
88
|
throw new Error("Method not implemented.")
|
|
81
89
|
}
|
|
82
|
-
async createDatabaseIfNotExist(
|
|
83
|
-
tempConnection: mysql.Connection
|
|
84
|
-
): Promise<void> {
|
|
90
|
+
async createDatabaseIfNotExist(tempConnection: mysql.Pool): Promise<void> {
|
|
85
91
|
const currentDatabaseName = this.DBInfo.database
|
|
86
|
-
console.log("currentDatabaseName", currentDatabaseName)
|
|
92
|
+
// console.log("currentDatabaseName", currentDatabaseName)
|
|
87
93
|
return new Promise((resolve, reject) => {
|
|
88
94
|
tempConnection.query(
|
|
89
95
|
`CREATE DATABASE IF NOT EXISTS ${currentDatabaseName}`,
|
|
@@ -240,7 +246,7 @@ export default class GGMySQLConnector<Main> implements ClassDBInterface<Main> {
|
|
|
240
246
|
let result = (await this.query(`DELETE FROM ${tableName} WHERE id = ?`, [
|
|
241
247
|
id,
|
|
242
248
|
])) as mysql.OkPacket
|
|
243
|
-
console.log(
|
|
249
|
+
// console.log(`table ${tableName} id ${id} => Delete Success`)
|
|
244
250
|
// this.webSocket.sendByURL("/websocket/watchTableChange", tableName)
|
|
245
251
|
return result
|
|
246
252
|
}
|
|
@@ -279,14 +285,13 @@ export default class GGMySQLConnector<Main> implements ClassDBInterface<Main> {
|
|
|
279
285
|
}
|
|
280
286
|
valueArrayToInsertInQuery.push(valueListToInsert)
|
|
281
287
|
}
|
|
282
|
-
console.log(valueArrayToInsertInQuery.slice(0, 5))
|
|
283
288
|
const result = (await this.query(
|
|
284
289
|
`INSERT INTO ${tableName as string} (${keyListToInsert.join(
|
|
285
290
|
","
|
|
286
291
|
)}) VALUES ?`,
|
|
287
292
|
[valueArrayToInsertInQuery]
|
|
288
293
|
)) as mysql.OkPacket
|
|
289
|
-
console.log(
|
|
294
|
+
// console.log(`table ${tableName as string} => Insert Success`)
|
|
290
295
|
// this.webSocket.sendByURL("/websocket/watchTableChange", String(tableName))
|
|
291
296
|
return result
|
|
292
297
|
}
|
|
@@ -300,7 +305,7 @@ export default class GGMySQLConnector<Main> implements ClassDBInterface<Main> {
|
|
|
300
305
|
[params.newID]
|
|
301
306
|
)
|
|
302
307
|
if (isNewIdExist.length >= 1) {
|
|
303
|
-
console.
|
|
308
|
+
console.error(`error : newID (${params.newID}) is already exist`)
|
|
304
309
|
return null
|
|
305
310
|
}
|
|
306
311
|
let result = (await this.query(
|
|
@@ -309,7 +314,7 @@ export default class GGMySQLConnector<Main> implements ClassDBInterface<Main> {
|
|
|
309
314
|
false
|
|
310
315
|
)) as mysql.OkPacket
|
|
311
316
|
|
|
312
|
-
console.log("UpdateOnlyID Success")
|
|
317
|
+
// console.log("UpdateOnlyID Success")
|
|
313
318
|
// this.webSocket.sendByURL("/websocket/watchTableChange", String(tableName))
|
|
314
319
|
return result
|
|
315
320
|
}
|
|
@@ -321,7 +326,7 @@ export default class GGMySQLConnector<Main> implements ClassDBInterface<Main> {
|
|
|
321
326
|
}>
|
|
322
327
|
) {
|
|
323
328
|
const columnList = await this.getColumnList(tableName)
|
|
324
|
-
console.log(parameter)
|
|
329
|
+
// console.log(parameter)
|
|
325
330
|
const keyList = Object.keys(parameter)
|
|
326
331
|
const keyListToInsert = []
|
|
327
332
|
const valueListToInsert = []
|
|
@@ -356,13 +361,13 @@ export default class GGMySQLConnector<Main> implements ClassDBInterface<Main> {
|
|
|
356
361
|
false
|
|
357
362
|
)) as mysql.OkPacket
|
|
358
363
|
|
|
359
|
-
console.log(
|
|
364
|
+
// console.log(`table : ${tableName} => Update Success`)
|
|
360
365
|
// this.webSocket.sendByURL("/websocket/watchTableChange", String(tableName))
|
|
361
366
|
return result
|
|
362
367
|
}
|
|
363
368
|
|
|
364
369
|
async init() {
|
|
365
|
-
const currentConnection = mysql.
|
|
370
|
+
const currentConnection = mysql.createPool({
|
|
366
371
|
host: this.DBInfo.host,
|
|
367
372
|
user: this.DBInfo.user,
|
|
368
373
|
password: this.DBInfo.password,
|
|
@@ -371,7 +376,7 @@ export default class GGMySQLConnector<Main> implements ClassDBInterface<Main> {
|
|
|
371
376
|
await this.createDatabaseIfNotExist(currentConnection)
|
|
372
377
|
// await currentConnection.end()
|
|
373
378
|
|
|
374
|
-
this.connection = mysql.
|
|
379
|
+
this.connection = mysql.createPool({ ...this.DBInfo })
|
|
375
380
|
// await this.query("SET global sql_mode=''")
|
|
376
381
|
// await this.query("SET global query_cache_type='ON'")
|
|
377
382
|
// await this.query("SET global query_cache_size=16777216")
|
|
@@ -55,16 +55,6 @@ export default class MyDBMigrator {
|
|
|
55
55
|
col.IS_INDEX,
|
|
56
56
|
indexList
|
|
57
57
|
)
|
|
58
|
-
//!! delete this next version. need to execute only 1 time on production
|
|
59
|
-
try {
|
|
60
|
-
await this.deleteOldIndexVersion(
|
|
61
|
-
row.tableName,
|
|
62
|
-
col.COLUMN_NAME,
|
|
63
|
-
indexList
|
|
64
|
-
)
|
|
65
|
-
} catch (error) {
|
|
66
|
-
console.log(error)
|
|
67
|
-
}
|
|
68
58
|
|
|
69
59
|
await this.alterPossibleEnum(
|
|
70
60
|
row.tableName,
|