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
|
@@ -4,7 +4,7 @@ import fs from "fs"
|
|
|
4
4
|
import mysql from "mysql2/promise"
|
|
5
5
|
import path from "path"
|
|
6
6
|
import MyDBMigrator from "../MyDBMigrator/MyDBMigrator"
|
|
7
|
-
import { MyModel } from "../myModel"
|
|
7
|
+
import { MyModel, MyViewModel } from "../myModel"
|
|
8
8
|
export interface DatabaseConfigInterface {
|
|
9
9
|
host: string
|
|
10
10
|
user: string
|
|
@@ -46,6 +46,11 @@ export default class ModelGenerator {
|
|
|
46
46
|
const migrator = new MyDBMigrator(this)
|
|
47
47
|
await migrator.migrateView(viewDirectory)
|
|
48
48
|
}
|
|
49
|
+
async pushViewToDB_v2(viewModel: MyViewModel[]) {
|
|
50
|
+
const migrator = new MyDBMigrator(this)
|
|
51
|
+
await migrator.migrateView_v2(viewModel)
|
|
52
|
+
}
|
|
53
|
+
|
|
49
54
|
waitUntilInitSuccess(interval: number) {
|
|
50
55
|
if (this.isConnected === true) return true
|
|
51
56
|
return new Promise((resolve, reject) => {
|
|
@@ -164,16 +169,32 @@ export default class ModelGenerator {
|
|
|
164
169
|
getPossibleColumnValue(
|
|
165
170
|
model: MyModel[],
|
|
166
171
|
tableName: string,
|
|
167
|
-
columnName: string
|
|
172
|
+
columnName: string,
|
|
173
|
+
columnType: string
|
|
168
174
|
) {
|
|
169
175
|
const foundTable = model.find((row) => row.tableName === tableName)
|
|
176
|
+
// search in table model
|
|
170
177
|
if (foundTable) {
|
|
171
178
|
const foundColumn = foundTable["columns"].find(
|
|
172
179
|
(row) => row.COLUMN_NAME === columnName
|
|
173
180
|
)
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
181
|
+
// find in possible value in table model first
|
|
182
|
+
if (foundColumn && foundColumn.POSSIBLE_VALUE) {
|
|
183
|
+
return foundColumn.POSSIBLE_VALUE
|
|
184
|
+
} else {
|
|
185
|
+
return false
|
|
186
|
+
}
|
|
187
|
+
} else {
|
|
188
|
+
// search in database view data structure
|
|
189
|
+
// if not found. search in view stucture in db engine structure
|
|
190
|
+
if (columnType.search("enum") >= 0) {
|
|
191
|
+
let temp = columnType.replace("enum(", "")
|
|
192
|
+
temp = temp.slice(0, -1)
|
|
193
|
+
temp = temp.replace(/"/g, "")
|
|
194
|
+
temp = temp.replace(/'/g, "")
|
|
195
|
+
let word: string[] = temp.split(",")
|
|
196
|
+
return word
|
|
197
|
+
}
|
|
177
198
|
}
|
|
178
199
|
}
|
|
179
200
|
async generateModelInterface(params: {
|
|
@@ -198,6 +219,7 @@ export default class ModelGenerator {
|
|
|
198
219
|
|
|
199
220
|
let stringOfRawColumnKeyAndValue = ""
|
|
200
221
|
for (const cRow of columnList) {
|
|
222
|
+
// console.log("cRow", cRow.COLUMN_TYPE)
|
|
201
223
|
const isColumnExistInModel = this.isColumnExistInModel(
|
|
202
224
|
params.model,
|
|
203
225
|
tableName,
|
|
@@ -207,7 +229,8 @@ export default class ModelGenerator {
|
|
|
207
229
|
const possibleValue = this.getPossibleColumnValue(
|
|
208
230
|
params.model,
|
|
209
231
|
cRow.TABLE_NAME,
|
|
210
|
-
cRow.COLUMN_NAME
|
|
232
|
+
cRow.COLUMN_NAME,
|
|
233
|
+
cRow.COLUMN_TYPE
|
|
211
234
|
)
|
|
212
235
|
if (possibleValue) {
|
|
213
236
|
stringOfRawColumnKeyAndValue = `${stringOfRawColumnKeyAndValue} \n ${
|
|
@@ -283,17 +306,17 @@ export default class ModelGenerator {
|
|
|
283
306
|
}
|
|
284
307
|
return false
|
|
285
308
|
}
|
|
286
|
-
const getPossibleColumnValue = (tableName: string, columnName: string) => {
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
}
|
|
309
|
+
// const getPossibleColumnValue = (tableName: string, columnName: string) => {
|
|
310
|
+
// const foundTable = params.model.find((row) => row.tableName === tableName)
|
|
311
|
+
// if (foundTable) {
|
|
312
|
+
// const foundColumn = foundTable["columns"].find(
|
|
313
|
+
// (row) => row.COLUMN_NAME === columnName
|
|
314
|
+
// )
|
|
315
|
+
// if (foundColumn) {
|
|
316
|
+
// if (foundColumn.POSSIBLE_VALUE) return foundColumn.POSSIBLE_VALUE
|
|
317
|
+
// } else return false
|
|
318
|
+
// }
|
|
319
|
+
// }
|
|
297
320
|
const tableList = [
|
|
298
321
|
...(await this.getTableNameList()),
|
|
299
322
|
...(await this.getViewNameList()),
|
|
@@ -315,9 +338,11 @@ export default class ModelGenerator {
|
|
|
315
338
|
cRow.COLUMN_NAME
|
|
316
339
|
)
|
|
317
340
|
if (columnChecker || tableName.search("_view") >= 0) {
|
|
318
|
-
const possibleValue = getPossibleColumnValue(
|
|
341
|
+
const possibleValue = this.getPossibleColumnValue(
|
|
342
|
+
params.model,
|
|
319
343
|
cRow.TABLE_NAME,
|
|
320
|
-
cRow.COLUMN_NAME
|
|
344
|
+
cRow.COLUMN_NAME,
|
|
345
|
+
cRow.COLUMN_TYPE
|
|
321
346
|
)
|
|
322
347
|
if (possibleValue) {
|
|
323
348
|
value = `${value} \n ${cRow.COLUMN_NAME}: [${possibleValue
|
|
@@ -1,3 +1,33 @@
|
|
|
1
|
-
import { MyModel } from "../myModel"
|
|
1
|
+
// import { MyModel } from "../myModel"
|
|
2
2
|
|
|
3
|
-
function
|
|
3
|
+
// function generateSeedInterfaceNoDBConnection(models: MyModel[]) {
|
|
4
|
+
// for (const tableModel of models) {
|
|
5
|
+
// const arrayOfColumnKeyValue = []
|
|
6
|
+
// const arrayOfSeedColumnKeyValue = []
|
|
7
|
+
|
|
8
|
+
// const columnList = tableModel.columns
|
|
9
|
+
|
|
10
|
+
// let stringOfRawColumnKeyAndValue = ""
|
|
11
|
+
// for (const cRow of columnList) {
|
|
12
|
+
// const possibleValue = cRow.POSSIBLE_VALUE
|
|
13
|
+
// if (possibleValue) {
|
|
14
|
+
// stringOfRawColumnKeyAndValue = `${stringOfRawColumnKeyAndValue} \n ${
|
|
15
|
+
// cRow.COLUMN_NAME
|
|
16
|
+
// }: ${possibleValue.map((row) => `"${row}"`).join(" | ")};`
|
|
17
|
+
// } else {
|
|
18
|
+
// stringOfRawColumnKeyAndValue = `${stringOfRawColumnKeyAndValue} \n ${
|
|
19
|
+
// cRow.COLUMN_NAME
|
|
20
|
+
// }: ${convertDataType(cRow.DATA_TYPE)};`
|
|
21
|
+
// }
|
|
22
|
+
// }
|
|
23
|
+
|
|
24
|
+
// // normal app model code
|
|
25
|
+
// const str = ` ${tableName} : { ${stringOfRawColumnKeyAndValue} }`
|
|
26
|
+
// arrayOfColumnKeyValue.push(str)
|
|
27
|
+
// // only seed model code
|
|
28
|
+
// const tempSeedModel = params.model.find(
|
|
29
|
+
// (row) => row.tableName === tableName && row.isSeed
|
|
30
|
+
// )
|
|
31
|
+
// if (tempSeedModel) arrayOfSeedColumnKeyValue.push(str)
|
|
32
|
+
// }
|
|
33
|
+
// }
|
|
@@ -2,7 +2,7 @@ import chalk from "chalk"
|
|
|
2
2
|
import fs from "fs"
|
|
3
3
|
import path from "path"
|
|
4
4
|
import ModelGenerator from "../ModelGenerator/ModelGenerator"
|
|
5
|
-
import { columnType, MyModel } from "../myModel"
|
|
5
|
+
import { columnType, MyModel, MyViewModel } from "../myModel"
|
|
6
6
|
|
|
7
7
|
export default class MyDBMigrator {
|
|
8
8
|
MyDB: ModelGenerator
|
|
@@ -35,13 +35,12 @@ export default class MyDBMigrator {
|
|
|
35
35
|
console.log(`${row.tableName} (${col.DATA_TYPE}) - ${col.COLUMN_NAME} `)
|
|
36
36
|
|
|
37
37
|
if (col.COLUMN_NAME === "id" && col.AUTO_INCREMENT === true) {
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
col.COLUMN_NAME
|
|
43
|
-
)) === false
|
|
38
|
+
const isPrimaryKey = await this.checkIsPrimaryKey(
|
|
39
|
+
this.MyDB.dbInfo.database,
|
|
40
|
+
row.tableName,
|
|
41
|
+
col.COLUMN_NAME
|
|
44
42
|
)
|
|
43
|
+
if (isPrimaryKey === false)
|
|
45
44
|
await this.MyDB.query(
|
|
46
45
|
`ALTER TABLE ${row.tableName} ADD PRIMARY KEY(id);`
|
|
47
46
|
)
|
|
@@ -74,6 +73,11 @@ export default class MyDBMigrator {
|
|
|
74
73
|
}
|
|
75
74
|
await this.alterUniqueKey(row.tableName, col.COLUMN_NAME, col.IS_UNIQUE)
|
|
76
75
|
await this.alterIndex(row.tableName, col.COLUMN_NAME, col.IS_INDEX)
|
|
76
|
+
await this.alterPossibleEnum(
|
|
77
|
+
row.tableName,
|
|
78
|
+
col.COLUMN_NAME,
|
|
79
|
+
col.POSSIBLE_VALUE
|
|
80
|
+
)
|
|
77
81
|
}
|
|
78
82
|
}
|
|
79
83
|
console.log("migrate table done")
|
|
@@ -182,6 +186,25 @@ export default class MyDBMigrator {
|
|
|
182
186
|
}
|
|
183
187
|
}
|
|
184
188
|
|
|
189
|
+
async alterPossibleEnum(
|
|
190
|
+
tableName: string,
|
|
191
|
+
columnName: string,
|
|
192
|
+
possibleValue?: string[]
|
|
193
|
+
) {
|
|
194
|
+
if (!possibleValue) return null
|
|
195
|
+
const data = this.onetimeLoadColumnContent.find(
|
|
196
|
+
(row) => row.COLUMN_NAME === columnName && row.TABLE_NAME === tableName
|
|
197
|
+
)
|
|
198
|
+
if (data) {
|
|
199
|
+
const possibleValueString = possibleValue
|
|
200
|
+
.map((row) => `"${row}"`)
|
|
201
|
+
.join(", ")
|
|
202
|
+
await this.MyDB.query(
|
|
203
|
+
`ALTER TABLE ${tableName} MODIFY COLUMN ${columnName} ENUM(${possibleValueString}) NOT NULL;`
|
|
204
|
+
)
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
|
|
185
208
|
async migrateView(viewDirectory: string) {
|
|
186
209
|
if (!fs.existsSync(viewDirectory)) {
|
|
187
210
|
return 0
|
|
@@ -214,6 +237,41 @@ export default class MyDBMigrator {
|
|
|
214
237
|
}
|
|
215
238
|
}
|
|
216
239
|
}
|
|
240
|
+
|
|
241
|
+
async migrateView_v2(viewModel: MyViewModel[]) {
|
|
242
|
+
let unSuccessUpdateViewList: MyViewModel[] = viewModel
|
|
243
|
+
let loopCount = 0
|
|
244
|
+
while (unSuccessUpdateViewList.length > 0) {
|
|
245
|
+
for (const viewRow of unSuccessUpdateViewList) {
|
|
246
|
+
const content = viewRow.sqlStatement
|
|
247
|
+
process.stdout.write(
|
|
248
|
+
`loop : ${loopCount} unUpdate : ${unSuccessUpdateViewList.length} update view ${viewRow.viewName}`
|
|
249
|
+
)
|
|
250
|
+
await this.MyDB.query(content as string)
|
|
251
|
+
.then((result) => {
|
|
252
|
+
process.stdout.write(" " + chalk.bgGreen(`success`))
|
|
253
|
+
unSuccessUpdateViewList = unSuccessUpdateViewList.filter(
|
|
254
|
+
(row) => row.viewName !== viewRow.viewName
|
|
255
|
+
)
|
|
256
|
+
})
|
|
257
|
+
.catch((_error) => {
|
|
258
|
+
process.stdout.write(" " + chalk.bgRed(`fail`))
|
|
259
|
+
})
|
|
260
|
+
.finally(() => {
|
|
261
|
+
console.log("")
|
|
262
|
+
})
|
|
263
|
+
loopCount++
|
|
264
|
+
|
|
265
|
+
if (loopCount > viewModel.length * 5) {
|
|
266
|
+
console.log(
|
|
267
|
+
"error while updating view, reason is loop update counter is over 5 time fail"
|
|
268
|
+
)
|
|
269
|
+
process.exit(1)
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
|
|
217
275
|
async checkIsPrimaryKey(
|
|
218
276
|
databaseName: string,
|
|
219
277
|
tableName: string,
|
package/src/myModel.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { SQLStatement } from "sql-template-strings"
|
|
2
|
+
|
|
1
3
|
export type columnType =
|
|
2
4
|
| "int"
|
|
3
5
|
| "tinyint"
|
|
@@ -20,7 +22,7 @@ export interface columnContent {
|
|
|
20
22
|
DATA_TYPE: columnType
|
|
21
23
|
COLUMN_DEFAULT?: string | number | null
|
|
22
24
|
AUTO_INCREMENT?: boolean
|
|
23
|
-
POSSIBLE_VALUE?:
|
|
25
|
+
POSSIBLE_VALUE?: string[]
|
|
24
26
|
IS_UNIQUE?: boolean
|
|
25
27
|
IS_INDEX?: boolean
|
|
26
28
|
}
|
|
@@ -30,7 +32,7 @@ export interface MyModel {
|
|
|
30
32
|
columns: [...columnContent[]]
|
|
31
33
|
meaning?: string
|
|
32
34
|
}
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
35
|
+
export interface MyViewModel {
|
|
36
|
+
viewName: string
|
|
37
|
+
sqlStatement: string | SQLStatement
|
|
38
|
+
}
|