gg-mysql-connector 1.0.36 → 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.
Files changed (40) hide show
  1. package/app_INF.ts +24 -0
  2. package/app_model_const.ts +24 -0
  3. package/dist/GGMySQLConnector/GGMySQLConnector.d.ts +72 -0
  4. package/dist/GGMySQLConnector/GGMySQLConnector.js +296 -0
  5. package/dist/ModelGenerator/ModelGenerator.d.ts +38 -0
  6. package/dist/ModelGenerator/ModelGenerator.js +299 -0
  7. package/dist/ModelGenerator/SeedGenerator.d.ts +1 -0
  8. package/dist/ModelGenerator/SeedGenerator.js +3 -0
  9. package/dist/MyDBMigrator/MyDBMigrator.d.ts +22 -0
  10. package/dist/MyDBMigrator/MyDBMigrator.js +157 -0
  11. package/dist/app_INF.d.ts +8 -1
  12. package/dist/app_model_const.d.ts +24 -0
  13. package/dist/app_model_const.js +27 -0
  14. package/dist/index.d.ts +3 -3
  15. package/dist/index.js +2 -2
  16. package/dist/myModel.d.ts +2 -3
  17. package/dist/seed_INF.d.ts +6 -0
  18. package/dist/seed_INF.js +2 -0
  19. package/dist/src/GGMySQLConnector/GGMySQLConnector.d.ts +72 -0
  20. package/dist/src/GGMySQLConnector/GGMySQLConnector.js +296 -0
  21. package/dist/src/ModelGenerator/ModelGenerator.d.ts +39 -0
  22. package/dist/src/ModelGenerator/ModelGenerator.js +317 -0
  23. package/dist/src/ModelGenerator/SeedGenerator.d.ts +0 -0
  24. package/dist/src/ModelGenerator/SeedGenerator.js +30 -0
  25. package/dist/src/MyDBMigrator/MyDBMigrator.d.ts +24 -0
  26. package/dist/src/MyDBMigrator/MyDBMigrator.js +196 -0
  27. package/dist/src/app_INF.d.ts +17 -0
  28. package/dist/src/app_INF.js +2 -0
  29. package/dist/src/index.d.ts +4 -0
  30. package/dist/src/index.js +10 -0
  31. package/dist/src/myModel.d.ts +21 -0
  32. package/dist/src/myModel.js +2 -0
  33. package/package.json +4 -3
  34. package/seed_INF.ts +3 -0
  35. package/src/{GGMySQLConnector.ts → GGMySQLConnector/GGMySQLConnector.ts} +1 -1
  36. package/src/{ModelGenerator.ts → ModelGenerator/ModelGenerator.ts} +109 -60
  37. package/src/ModelGenerator/SeedGenerator.ts +33 -0
  38. package/src/{MyDBMigration.ts → MyDBMigrator/MyDBMigrator.ts} +67 -9
  39. package/src/index.ts +3 -3
  40. package/src/myModel.ts +8 -8
@@ -1,7 +1,7 @@
1
1
  import chalk from "chalk"
2
2
  import { randomUUID } from "crypto"
3
3
  import mysql, { QueryResult, RowDataPacket } from "mysql2"
4
- import { DatabaseConfigInterface } from "./ModelGenerator"
4
+ import { DatabaseConfigInterface } from "../ModelGenerator/ModelGenerator"
5
5
 
6
6
  export interface ClassDBInterface<Main> {
7
7
  connection: mysql.Connection
@@ -3,8 +3,8 @@ import { randomUUID } from "crypto"
3
3
  import fs from "fs"
4
4
  import mysql from "mysql2/promise"
5
5
  import path from "path"
6
- import MyDBMigration from "./MyDBMigration"
7
- import { MyModel } from "./myModel"
6
+ import MyDBMigrator from "../MyDBMigrator/MyDBMigrator"
7
+ import { MyModel, MyViewModel } from "../myModel"
8
8
  export interface DatabaseConfigInterface {
9
9
  host: string
10
10
  user: string
@@ -39,13 +39,18 @@ export default class ModelGenerator {
39
39
  }
40
40
 
41
41
  async pushModelToDB() {
42
- const migrator = new MyDBMigration(this)
42
+ const migrator = new MyDBMigrator(this)
43
43
  await migrator.migrateTable(this.model)
44
44
  }
45
45
  async pushViewToDB(viewDirectory: string) {
46
- const migrator = new MyDBMigration(this)
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) => {
@@ -142,70 +147,97 @@ export default class ModelGenerator {
142
147
  )) as mysql.RowDataPacket[]
143
148
  return result
144
149
  }
150
+
151
+ private isTableNameExistInModel(model: MyModel[], tableName: string) {
152
+ if (tableName === "sessions") return true
153
+ const table = model.find((row) => row.tableName === tableName)
154
+ return table ? true : false
155
+ }
156
+ private isColumnExistInModel(
157
+ model: MyModel[],
158
+ tableName: string,
159
+ columnName: string
160
+ ) {
161
+ if (tableName === "sessions") return true
162
+ const table = model.find((row) => row.tableName === tableName)
163
+ if (table) {
164
+ const column = table.columns.find((row) => row.COLUMN_NAME === columnName)
165
+ if (column) return true
166
+ }
167
+ return false
168
+ }
169
+ getPossibleColumnValue(
170
+ model: MyModel[],
171
+ tableName: string,
172
+ columnName: string,
173
+ columnType: string
174
+ ) {
175
+ const foundTable = model.find((row) => row.tableName === tableName)
176
+ // search in table model
177
+ if (foundTable) {
178
+ const foundColumn = foundTable["columns"].find(
179
+ (row) => row.COLUMN_NAME === columnName
180
+ )
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
+ }
198
+ }
199
+ }
145
200
  async generateModelInterface(params: {
146
201
  appName: string
147
202
  model: MyModel[]
148
203
  outputDirectory: string[]
149
204
  }) {
150
- const isTableNameExistInModel = (tableName: string) => {
151
- if (tableName === "sessions") return true
152
- const table = params.model.find((row) => row.tableName === tableName)
153
- if (table) return true
154
- return false
155
- }
156
- const isColumnExistInModel = (tableName: string, columnName: string) => {
157
- if (tableName === "sessions") return true
158
- const table = params.model.find((row) => row.tableName === tableName)
159
- if (table) {
160
- const column = table.columns.find(
161
- (row) => row.COLUMN_NAME === columnName
162
- )
163
- if (column) return true
164
- }
165
- return false
166
- }
167
- const getPossibleColumnValue = (tableName: string, columnName: string) => {
168
- const foundTable = params.model.find((row) => row.tableName === tableName)
169
- if (foundTable) {
170
- const foundColumn = foundTable["columns"].find(
171
- (row) => row.COLUMN_NAME === columnName
172
- )
173
- if (foundColumn) {
174
- if (foundColumn.POSSIBLE_VALUE) return foundColumn.POSSIBLE_VALUE
175
- } else return false
176
- }
177
- }
178
205
  const tableList = [
179
206
  ...(await this.getTableNameList()),
180
207
  ...(await this.getViewNameList()),
181
208
  ]
182
209
 
183
- const array = []
210
+ const arrayOfColumnKeyValue = []
211
+ const arrayOfSeedColumnKeyValue = []
184
212
  for (let i = 0; i < tableList.length; i++) {
185
213
  const tableName = tableList[i]
186
214
  if (
187
- isTableNameExistInModel(tableName) ||
215
+ this.isTableNameExistInModel(params.model, tableName) ||
188
216
  tableName.search("_view") >= 0
189
217
  ) {
190
218
  const columnList = await this.getColumnFullList(tableName)
191
219
 
192
- let singleInterfaceContent = ""
220
+ let stringOfRawColumnKeyAndValue = ""
193
221
  for (const cRow of columnList) {
194
- const columnChecker = isColumnExistInModel(
222
+ // console.log("cRow", cRow.COLUMN_TYPE)
223
+ const isColumnExistInModel = this.isColumnExistInModel(
224
+ params.model,
195
225
  tableName,
196
226
  cRow.COLUMN_NAME
197
227
  )
198
- if (columnChecker || tableName.search("_view") >= 0) {
199
- const possibleValue = getPossibleColumnValue(
228
+ if (isColumnExistInModel || tableName.search("_view") >= 0) {
229
+ const possibleValue = this.getPossibleColumnValue(
230
+ params.model,
200
231
  cRow.TABLE_NAME,
201
- cRow.COLUMN_NAME
232
+ cRow.COLUMN_NAME,
233
+ cRow.COLUMN_TYPE
202
234
  )
203
235
  if (possibleValue) {
204
- singleInterfaceContent = `${singleInterfaceContent} \n ${
236
+ stringOfRawColumnKeyAndValue = `${stringOfRawColumnKeyAndValue} \n ${
205
237
  cRow.COLUMN_NAME
206
238
  }: ${possibleValue.map((row) => `"${row}"`).join(" | ")};`
207
239
  } else {
208
- singleInterfaceContent = `${singleInterfaceContent} \n ${
240
+ stringOfRawColumnKeyAndValue = `${stringOfRawColumnKeyAndValue} \n ${
209
241
  cRow.COLUMN_NAME
210
242
  }: ${convertDataType(cRow.DATA_TYPE)};`
211
243
  }
@@ -214,20 +246,35 @@ export default class ModelGenerator {
214
246
  }
215
247
  }
216
248
 
217
- const str = ` ${tableName} : { ${singleInterfaceContent} }`
218
- array.push(str)
219
- // console.log(`generate interface from ${tableName}`)
249
+ // normal app model code
250
+ const str = ` ${tableName} : { ${stringOfRawColumnKeyAndValue} }`
251
+ arrayOfColumnKeyValue.push(str)
252
+ // only seed model code
253
+ const tempSeedModel = params.model.find(
254
+ (row) => row.tableName === tableName && row.isSeed
255
+ )
256
+ if (tempSeedModel) arrayOfSeedColumnKeyValue.push(str)
220
257
  }
221
258
  }
259
+ // normal app model code
222
260
  const fileName = `${params.appName}_INF.ts`
261
+ const seedFileName = `seed_INF.ts`
223
262
  const code = `export default interface ${
224
263
  params.appName
225
- }_INF { ${array.join("\n")} }`
264
+ }_INF { ${arrayOfColumnKeyValue.join("\n")} }`
265
+ const seedCode = `export default interface seed_INF { ${arrayOfSeedColumnKeyValue.join(
266
+ "\n"
267
+ )} }`
226
268
 
227
269
  for (let row of params.outputDirectory) {
270
+ // normal app model code
228
271
  const serverFilePath = path.join(row, fileName)
229
272
  await fs.writeFileSync(serverFilePath, code)
230
- console.log("save to ", serverFilePath)
273
+ console.log("save app model to ", serverFilePath)
274
+ // only seed model code
275
+ const seedServerFilePath = path.join(row, seedFileName)
276
+ await fs.writeFileSync(seedServerFilePath, seedCode)
277
+ console.log("save seed model to ", seedServerFilePath)
231
278
  }
232
279
 
233
280
  console.log(
@@ -259,17 +306,17 @@ export default class ModelGenerator {
259
306
  }
260
307
  return false
261
308
  }
262
- const getPossibleColumnValue = (tableName: string, columnName: string) => {
263
- const foundTable = params.model.find((row) => row.tableName === tableName)
264
- if (foundTable) {
265
- const foundColumn = foundTable["columns"].find(
266
- (row) => row.COLUMN_NAME === columnName
267
- )
268
- if (foundColumn) {
269
- if (foundColumn.POSSIBLE_VALUE) return foundColumn.POSSIBLE_VALUE
270
- } else return false
271
- }
272
- }
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
+ // }
273
320
  const tableList = [
274
321
  ...(await this.getTableNameList()),
275
322
  ...(await this.getViewNameList()),
@@ -291,9 +338,11 @@ export default class ModelGenerator {
291
338
  cRow.COLUMN_NAME
292
339
  )
293
340
  if (columnChecker || tableName.search("_view") >= 0) {
294
- const possibleValue = getPossibleColumnValue(
341
+ const possibleValue = this.getPossibleColumnValue(
342
+ params.model,
295
343
  cRow.TABLE_NAME,
296
- cRow.COLUMN_NAME
344
+ cRow.COLUMN_NAME,
345
+ cRow.COLUMN_TYPE
297
346
  )
298
347
  if (possibleValue) {
299
348
  value = `${value} \n ${cRow.COLUMN_NAME}: [${possibleValue
@@ -319,7 +368,7 @@ export default class ModelGenerator {
319
368
  }
320
369
  const fileName = `${params.appName}_model_const.ts`
321
370
  const code = `
322
- export const ${params.appName}_model_const = { ${array.join(
371
+ export const ${params.appName}_model_const = { ${array.join(
323
372
  ","
324
373
  )} } as const`
325
374
 
@@ -0,0 +1,33 @@
1
+ // import { MyModel } from "../myModel"
2
+
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
+ // }
@@ -1,10 +1,10 @@
1
1
  import chalk from "chalk"
2
2
  import fs from "fs"
3
3
  import path from "path"
4
- import ModelGenerator from "./ModelGenerator"
5
- import { columnType, MyModel } from "./myModel"
4
+ import ModelGenerator from "../ModelGenerator/ModelGenerator"
5
+ import { columnType, MyModel, MyViewModel } from "../myModel"
6
6
 
7
- export default class MyDBMigration<Main> {
7
+ export default class MyDBMigrator {
8
8
  MyDB: ModelGenerator
9
9
  onetimeLoadColumnContent: {
10
10
  COLUMN_NAME: string
@@ -35,13 +35,12 @@ export default class MyDBMigration<Main> {
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
- if (
39
- (await this.checkIsPrimaryKey(
40
- this.MyDB.dbInfo.database,
41
- row.tableName,
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 MyDBMigration<Main> {
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 MyDBMigration<Main> {
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 MyDBMigration<Main> {
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/index.ts CHANGED
@@ -1,4 +1,4 @@
1
1
  export { MyModel, columnContent, columnType } from "./myModel"
2
- export { DatabaseConfigInterface } from "./ModelGenerator"
3
- export { default as ModelGenerator } from "./ModelGenerator"
4
- export { default as GGMySQLConnector } from "./GGMySQLConnector"
2
+ export { DatabaseConfigInterface } from "./ModelGenerator/ModelGenerator"
3
+ export { default as ModelGenerator } from "./ModelGenerator/ModelGenerator"
4
+ export { default as GGMySQLConnector } from "./GGMySQLConnector/GGMySQLConnector"
package/src/myModel.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { SQLStatement } from "sql-template-strings"
1
2
 
2
3
  export type columnType =
3
4
  | "int"
@@ -21,18 +22,17 @@ export interface columnContent {
21
22
  DATA_TYPE: columnType
22
23
  COLUMN_DEFAULT?: string | number | null
23
24
  AUTO_INCREMENT?: boolean
24
- POSSIBLE_VALUE?: any[]
25
+ POSSIBLE_VALUE?: string[]
25
26
  IS_UNIQUE?: boolean
26
27
  IS_INDEX?: boolean
27
28
  }
28
29
  export interface MyModel {
30
+ isSeed: boolean
29
31
  tableName: string
30
- columns: [
31
- ...columnContent[]
32
- ]
32
+ columns: [...columnContent[]]
33
33
  meaning?: string
34
34
  }
35
- // export interface MyModelView {
36
- // viewName: string
37
- // relatedTableName: (keyof hotel_INF)[]
38
- // }
35
+ export interface MyViewModel {
36
+ viewName: string
37
+ sqlStatement: string | SQLStatement
38
+ }