gg-mysql-connector 1.0.129 → 1.0.132

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.
@@ -35,6 +35,14 @@ class ModelGenerator {
35
35
  });
36
36
  this.isConnected = true;
37
37
  await this.createDatabaseIfNotExist();
38
+ await this.connection.end();
39
+ this.connection = await promise_1.default.createPool({
40
+ host: this.dbInfo.host,
41
+ user: this.dbInfo.user,
42
+ password: this.dbInfo.password,
43
+ port: this.dbInfo.port,
44
+ database: this.dbInfo.database,
45
+ });
38
46
  }
39
47
  async createDatabaseIfNotExist() {
40
48
  const databaseName = this.dbInfo.database;
@@ -16,13 +16,13 @@ class MyDBMigrator {
16
16
  async migrateTable(model) {
17
17
  let data = model.filter((row) => row.tableName !== "sessions" && row.tableName.search("prisma") < 0);
18
18
  let index = 1;
19
- this.onetimeLoadColumnContent = [];
20
- await this.loadColumnContentIfEmpty();
21
19
  for (let row of data) {
22
20
  console.log(chalk_1.default.bgBlue(`${index}/${data.length} ${row.tableName}`));
23
21
  await this.createTableIfNotExist(row.tableName);
24
22
  index++;
25
23
  const indexList = (await this.MyDB.query(`SHOW INDEX FROM ${row.tableName}`));
24
+ this.onetimeLoadColumnContent = [];
25
+ await this.loadColumnContentIfEmpty();
26
26
  for (let col of row.columns) {
27
27
  console.log(`${row.tableName} (${col.DATA_TYPE}) - ${col.COLUMN_NAME} `);
28
28
  await this.alterPrimaryKey(row.tableName, col);
@@ -129,9 +129,8 @@ class MyDBMigrator {
129
129
  async alterPrimaryKey(tableName, col) {
130
130
  // console.log("alterPrimaryKey()")
131
131
  if (col.COLUMN_NAME === "id" && col.AUTO_INCREMENT === true) {
132
- const isPrimaryKey = await this.checkIsPrimaryKey(tableName, col.COLUMN_NAME);
133
- // recheck only id
134
- if (isPrimaryKey === false && col.COLUMN_NAME === "id")
132
+ const isPrimaryKey = await this.checkIsPrimaryKey(this.MyDB.dbInfo.database, tableName, col.COLUMN_NAME);
133
+ if (isPrimaryKey === false)
135
134
  await this.MyDB.query(`ALTER TABLE ${tableName} ADD PRIMARY KEY(id);`);
136
135
  }
137
136
  }
@@ -274,15 +273,18 @@ class MyDBMigrator {
274
273
  loopCount++;
275
274
  }
276
275
  }
277
- async checkIsPrimaryKey(tableName, columnName) {
278
- // let result: any = await this.MyDB.query(`SELECT *
279
- // FROM INFORMATION_SCHEMA.COLUMNS
280
- // WHERE TABLE_SCHEMA='${databaseName}'
281
- // AND TABLE_NAME='${tableName}'
282
- // AND COLUMN_NAME='${columnName}'`)
283
- const result2 = this.onetimeLoadColumnContent.find((row) => row.TABLE_NAME === tableName && columnName === columnName);
284
- if (result2) {
285
- if (result2.COLUMN_KEY === "PRI")
276
+ async checkIsPrimaryKey(databaseName, tableName, columnName) {
277
+ let result = (await this.MyDB.query(`SELECT *
278
+ FROM INFORMATION_SCHEMA.COLUMNS
279
+ WHERE TABLE_SCHEMA='${databaseName}'
280
+ AND TABLE_NAME='${tableName}'
281
+ AND COLUMN_NAME='${columnName}'`));
282
+ // console.log("checkIsPrimaryKey", result)
283
+ // const result2 = this.onetimeLoadColumnContent.find(
284
+ // (row) => row.TABLE_NAME === tableName && columnName === columnName
285
+ // )
286
+ if (result.length) {
287
+ if (result[0].COLUMN_KEY === "PRI")
286
288
  return true;
287
289
  else
288
290
  return false;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "gg-mysql-connector",
3
- "version": "1.0.129",
3
+ "version": "1.0.132",
4
4
  "description": "",
5
5
  "main": "dist/src/index.js",
6
6
  "scripts": {
@@ -36,6 +36,14 @@ export default class ModelGenerator {
36
36
  })
37
37
  this.isConnected = true
38
38
  await this.createDatabaseIfNotExist()
39
+ await this.connection.end()
40
+ this.connection = await mysql.createPool({
41
+ host: this.dbInfo.host,
42
+ user: this.dbInfo.user,
43
+ password: this.dbInfo.password,
44
+ port: this.dbInfo.port,
45
+ database: this.dbInfo.database,
46
+ })
39
47
  }
40
48
  private async createDatabaseIfNotExist() {
41
49
  const databaseName = this.dbInfo.database
@@ -44,6 +52,7 @@ export default class ModelGenerator {
44
52
  }
45
53
  async pushModelToDB() {
46
54
  const migrator = new MyDBMigrator(this)
55
+
47
56
  await migrator.migrateTable(this.model)
48
57
  }
49
58
  async pushViewToDB_v2(viewModel: MyViewModel[]) {
@@ -32,8 +32,7 @@ export default class MyDBMigrator {
32
32
  row.tableName !== "sessions" && row.tableName.search("prisma") < 0
33
33
  )
34
34
  let index = 1
35
- this.onetimeLoadColumnContent = []
36
- await this.loadColumnContentIfEmpty()
35
+
37
36
  for (let row of data) {
38
37
  console.log(chalk.bgBlue(`${index}/${data.length} ${row.tableName}`))
39
38
  await this.createTableIfNotExist(row.tableName)
@@ -44,6 +43,9 @@ export default class MyDBMigrator {
44
43
  `SHOW INDEX FROM ${row.tableName}`
45
44
  )) as { Key_name: string }[]
46
45
 
46
+ this.onetimeLoadColumnContent = []
47
+ await this.loadColumnContentIfEmpty()
48
+
47
49
  for (let col of row.columns) {
48
50
  console.log(`${row.tableName} (${col.DATA_TYPE}) - ${col.COLUMN_NAME} `)
49
51
  await this.alterPrimaryKey(row.tableName, col)
@@ -207,11 +209,11 @@ export default class MyDBMigrator {
207
209
  // console.log("alterPrimaryKey()")
208
210
  if (col.COLUMN_NAME === "id" && col.AUTO_INCREMENT === true) {
209
211
  const isPrimaryKey = await this.checkIsPrimaryKey(
212
+ this.MyDB.dbInfo.database,
210
213
  tableName,
211
214
  col.COLUMN_NAME
212
215
  )
213
- // recheck only id
214
- if (isPrimaryKey === false && col.COLUMN_NAME === "id")
216
+ if (isPrimaryKey === false)
215
217
  await this.MyDB.query(`ALTER TABLE ${tableName} ADD PRIMARY KEY(id);`)
216
218
  }
217
219
  }
@@ -413,19 +415,23 @@ export default class MyDBMigrator {
413
415
  }
414
416
  }
415
417
 
416
- private async checkIsPrimaryKey(tableName: string, columnName: string) {
417
- // let result: any = await this.MyDB.query(`SELECT *
418
- // FROM INFORMATION_SCHEMA.COLUMNS
419
- // WHERE TABLE_SCHEMA='${databaseName}'
420
- // AND TABLE_NAME='${tableName}'
421
- // AND COLUMN_NAME='${columnName}'`)
422
-
423
- const result2 = this.onetimeLoadColumnContent.find(
424
- (row) => row.TABLE_NAME === tableName && columnName === columnName
425
- )
418
+ private async checkIsPrimaryKey(
419
+ databaseName: string,
420
+ tableName: string,
421
+ columnName: string
422
+ ) {
423
+ let result = (await this.MyDB.query(`SELECT *
424
+ FROM INFORMATION_SCHEMA.COLUMNS
425
+ WHERE TABLE_SCHEMA='${databaseName}'
426
+ AND TABLE_NAME='${tableName}'
427
+ AND COLUMN_NAME='${columnName}'`)) as { COLUMN_KEY: string }[]
428
+ // console.log("checkIsPrimaryKey", result)
429
+ // const result2 = this.onetimeLoadColumnContent.find(
430
+ // (row) => row.TABLE_NAME === tableName && columnName === columnName
431
+ // )
426
432
 
427
- if (result2) {
428
- if (result2.COLUMN_KEY === "PRI") return true
433
+ if (result.length) {
434
+ if (result[0].COLUMN_KEY === "PRI") return true
429
435
  else return false
430
436
  } else return false
431
437
  }