gg-mysql-connector 1.0.78 → 1.0.81

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 CHANGED
@@ -8,6 +8,10 @@ export default interface app_INF { item : {
8
8
  user : {
9
9
  id: number;
10
10
  name: string; }
11
+ item__each_user : {
12
+ id: number;
13
+ itemId: number;
14
+ userId: number; }
11
15
  test_view : {
12
16
  id: number;
13
17
  name: string;
@@ -7,7 +7,10 @@
7
7
  amount: "number",
8
8
  userId: "number", }, user : {
9
9
  id: "number",
10
- name: "string", }, test_view : {
10
+ name: "string", }, item__each_user : {
11
+ id: "number",
12
+ itemId: "number",
13
+ userId: "number", }, test_view : {
11
14
  id: "number",
12
15
  name: "string",
13
16
  itemName: ["pen" , "ruler" , "tiger"] as string[], }, item_view : {
package/dist/app_INF.d.ts CHANGED
@@ -11,6 +11,11 @@ export default interface app_INF {
11
11
  id: number;
12
12
  name: string;
13
13
  };
14
+ item__each_user: {
15
+ id: number;
16
+ itemId: number;
17
+ userId: number;
18
+ };
14
19
  test_view: {
15
20
  id: number;
16
21
  name: string;
@@ -11,6 +11,11 @@ export declare const app_model_const: {
11
11
  readonly id: "number";
12
12
  readonly name: "string";
13
13
  };
14
+ readonly item__each_user: {
15
+ readonly id: "number";
16
+ readonly itemId: "number";
17
+ readonly userId: "number";
18
+ };
14
19
  readonly test_view: {
15
20
  readonly id: "number";
16
21
  readonly name: "string";
@@ -11,6 +11,10 @@ exports.app_model_const = { item: {
11
11
  }, user: {
12
12
  id: "number",
13
13
  name: "string",
14
+ }, item__each_user: {
15
+ id: "number",
16
+ itemId: "number",
17
+ userId: "number",
14
18
  }, test_view: {
15
19
  id: "number",
16
20
  name: "string",
@@ -9,6 +9,7 @@ const fs_1 = __importDefault(require("fs"));
9
9
  const promise_1 = __importDefault(require("mysql2/promise"));
10
10
  const path_1 = __importDefault(require("path"));
11
11
  const MyDBMigrator_1 = __importDefault(require("../MyDBMigrator/MyDBMigrator"));
12
+ const convertDataType_1 = require("./convertDataType");
12
13
  class ModelGenerator {
13
14
  constructor(dbInfo, model) {
14
15
  this.printResultLength = (results, executionTime, queryResult) => {
@@ -42,10 +43,6 @@ class ModelGenerator {
42
43
  const migrator = new MyDBMigrator_1.default(this);
43
44
  await migrator.migrateTable(this.model);
44
45
  }
45
- // async pushViewToDB(viewDirectory: string) {
46
- // const migrator = new MyDBMigrator(this)
47
- // await migrator.migrateView(viewDirectory)
48
- // }
49
46
  async pushViewToDB_v2(viewModel) {
50
47
  const migrator = new MyDBMigrator_1.default(this);
51
48
  await migrator.migrateView_v2(viewModel);
@@ -178,7 +175,7 @@ class ModelGenerator {
178
175
  stringOfRawColumnKeyAndValue = `${stringOfRawColumnKeyAndValue} \n ${cRow.COLUMN_NAME}: ${possibleValue.map((row) => `"${row}"`).join(" | ")};`;
179
176
  }
180
177
  else {
181
- stringOfRawColumnKeyAndValue = `${stringOfRawColumnKeyAndValue} \n ${cRow.COLUMN_NAME}: ${convertDataType(cRow.DATA_TYPE)};`;
178
+ stringOfRawColumnKeyAndValue = `${stringOfRawColumnKeyAndValue} \n ${cRow.COLUMN_NAME}: ${(0, convertDataType_1.convertDataType)(cRow.DATA_TYPE)};`;
182
179
  }
183
180
  }
184
181
  else {
@@ -272,7 +269,7 @@ class ModelGenerator {
272
269
  .join(" , ")}] as ${typeof possibleValue[0]}[],`;
273
270
  }
274
271
  else {
275
- value = `${value} \n ${cRow.COLUMN_NAME}: "${convertDataType(cRow.DATA_TYPE)}",`;
272
+ value = `${value} \n ${cRow.COLUMN_NAME}: "${(0, convertDataType_1.convertDataType)(cRow.DATA_TYPE)}",`;
276
273
  }
277
274
  }
278
275
  else {
@@ -297,25 +294,3 @@ class ModelGenerator {
297
294
  }
298
295
  }
299
296
  exports.default = ModelGenerator;
300
- function convertDataType(inputDataType) {
301
- let result = "string";
302
- if (inputDataType === "int") {
303
- result = "number";
304
- }
305
- else if (inputDataType === "tinyint") {
306
- result = "number";
307
- }
308
- else if (inputDataType === "bigint") {
309
- result = "number";
310
- }
311
- else if (inputDataType === "date") {
312
- result = "string";
313
- }
314
- else if (inputDataType === "float") {
315
- result = "number";
316
- }
317
- else if (inputDataType === "double") {
318
- result = "number";
319
- }
320
- return result;
321
- }
@@ -0,0 +1 @@
1
+ export declare function convertDataType(inputDataType: string): "string" | "number";
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.convertDataType = convertDataType;
4
+ function convertDataType(inputDataType) {
5
+ let result = "string";
6
+ if (inputDataType === "int") {
7
+ result = "number";
8
+ }
9
+ else if (inputDataType === "tinyint") {
10
+ result = "number";
11
+ }
12
+ else if (inputDataType === "bigint") {
13
+ result = "number";
14
+ }
15
+ else if (inputDataType === "date") {
16
+ result = "string";
17
+ }
18
+ else if (inputDataType === "float") {
19
+ result = "number";
20
+ }
21
+ else if (inputDataType === "double") {
22
+ result = "number";
23
+ }
24
+ return result;
25
+ }
@@ -13,16 +13,19 @@ export default class MyDBMigrator {
13
13
  constructor(connection: ModelGenerator);
14
14
  migrateTable(model: MyModel[]): Promise<void>;
15
15
  private isConstraintNameExist;
16
+ private getCompositeUniqueName;
17
+ private isCompositeUniqueColumnExist;
16
18
  private alterForeignKey;
19
+ private alterUniqueColumnCombination;
17
20
  private alterDataType;
18
21
  private alterPrimaryKey;
19
22
  private createTableIfNotExist;
20
23
  private columnInTableExist;
21
24
  private loadColumnContentIfEmpty;
22
25
  private checkIsColumnDataTypeChange;
23
- alterUniqueKey(tableName: string, columnName: string, IS_UNIQUE: boolean | undefined): Promise<void>;
24
- alterIndex(tableName: string, columnName: string, IS_INDEX: boolean | undefined): Promise<void>;
25
- alterPossibleEnum(tableName: string, columnName: string, possibleValue?: string[]): Promise<null | undefined>;
26
+ private alterUniqueKey;
27
+ private alterIndex;
28
+ private alterPossibleEnum;
26
29
  migrateView_v2(viewModel: MyViewModel[]): Promise<void>;
27
- checkIsPrimaryKey(databaseName: string, tableName: string, columnName: string): Promise<boolean>;
30
+ private checkIsPrimaryKey;
28
31
  }
@@ -14,6 +14,7 @@ class MyDBMigrator {
14
14
  let index = 1;
15
15
  for (let row of data) {
16
16
  console.log(chalk_1.default.bgBlue(`${index}/${data.length} ${row.tableName}`));
17
+ console.log(row);
17
18
  await this.createTableIfNotExist(row.tableName);
18
19
  this.onetimeLoadColumnContent = [];
19
20
  await this.loadColumnContentIfEmpty();
@@ -26,6 +27,7 @@ class MyDBMigrator {
26
27
  await this.alterIndex(row.tableName, col.COLUMN_NAME, col.IS_INDEX);
27
28
  await this.alterPossibleEnum(row.tableName, col.COLUMN_NAME, col.POSSIBLE_VALUE);
28
29
  await this.alterForeignKey(row.tableName, col);
30
+ await this.alterUniqueColumnCombination(row.tableName, row.compoisteUniqueColumn);
29
31
  }
30
32
  }
31
33
  console.log("migrate table done");
@@ -39,13 +41,22 @@ class MyDBMigrator {
39
41
  const data = (await this.MyDB.query(sql));
40
42
  return data.length ? true : false;
41
43
  }
44
+ getCompositeUniqueName(compositeUniqueColumn) {
45
+ return `unique_${compositeUniqueColumn === null || compositeUniqueColumn === void 0 ? void 0 : compositeUniqueColumn.columnName.join("_")}`;
46
+ }
47
+ async isCompositeUniqueColumnExist(tableName, compositeUniqueColumn) {
48
+ const compositeUniqueName = this.getCompositeUniqueName(compositeUniqueColumn);
49
+ const sql = `SHOW INDEX FROM ${tableName} WHERE Non_unique = 0 AND Key_name like '${compositeUniqueName}'`;
50
+ const data = (await this.MyDB.query(sql));
51
+ return data.length ? true : false;
52
+ }
42
53
  async alterForeignKey(tableName, col) {
43
54
  if (col.FOREIGN_KEY) {
44
55
  for (const row of col.FOREIGN_KEY) {
45
56
  const CONSTRAINT = `fk_${tableName}_${col.COLUMN_NAME}_${row.tableName}_${row.columnName}`;
46
57
  const constraintExist = await this.isConstraintNameExist(CONSTRAINT);
47
58
  if (constraintExist) {
48
- this.MyDB.query(`ALTER TABLE ${tableName} DROP FOREIGN KEY ${CONSTRAINT}`);
59
+ await this.MyDB.query(`ALTER TABLE ${tableName} DROP FOREIGN KEY ${CONSTRAINT}`);
49
60
  }
50
61
  await this.MyDB
51
62
  .query(`ALTER TABLE ${tableName} ADD CONSTRAINT ${CONSTRAINT}
@@ -56,6 +67,20 @@ class MyDBMigrator {
56
67
  }
57
68
  }
58
69
  }
70
+ async alterUniqueColumnCombination(tableName, uniqueColumnCombination) {
71
+ if (uniqueColumnCombination === null || uniqueColumnCombination === void 0 ? void 0 : uniqueColumnCombination.length) {
72
+ uniqueColumnCombination.forEach(async (row) => {
73
+ const isCompositeUniqueNameExist = await this.isCompositeUniqueColumnExist(tableName, row);
74
+ // check is exist, then drop it first
75
+ if (isCompositeUniqueNameExist) {
76
+ await this.MyDB.query(`ALTER TABLE ${tableName} DROP INDEX ${this.getCompositeUniqueName(row)}`);
77
+ }
78
+ const uniqueKeyName = `unique_${row.columnName.join("_")}`;
79
+ const temp = row.columnName.join(",");
80
+ await this.MyDB.query(`ALTER TABLE ${tableName} ADD UNIQUE KEY ${uniqueKeyName} (${temp})`);
81
+ });
82
+ }
83
+ }
59
84
  async alterDataType(tableName, col) {
60
85
  const AutoIncrement = col.AUTO_INCREMENT ? "AUTO_INCREMENT" : "";
61
86
  const columnDefault = col.COLUMN_DEFAULT
@@ -15,11 +15,15 @@ export interface columnContent {
15
15
  IS_UNIQUE?: boolean;
16
16
  IS_INDEX?: boolean;
17
17
  }
18
+ export interface compoisteUniqueColumn {
19
+ columnName: string[];
20
+ }
18
21
  export interface MyModel {
19
22
  isSeed?: boolean;
20
23
  tableName: string;
21
24
  columns: [...columnContent[]];
22
25
  meaning?: string;
26
+ compoisteUniqueColumn?: compoisteUniqueColumn[];
23
27
  }
24
28
  export interface MyViewModel {
25
29
  viewName: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "gg-mysql-connector",
3
- "version": "1.0.78",
3
+ "version": "1.0.81",
4
4
  "description": "",
5
5
  "main": "dist/src/index.js",
6
6
  "scripts": {
@@ -5,6 +5,7 @@ import mysql from "mysql2/promise"
5
5
  import path from "path"
6
6
  import MyDBMigrator from "../MyDBMigrator/MyDBMigrator"
7
7
  import { MyModel, MyViewModel } from "../myModel"
8
+ import { convertDataType } from "./convertDataType"
8
9
  export interface DatabaseConfigInterface {
9
10
  host: string
10
11
  user: string
@@ -37,15 +38,10 @@ export default class ModelGenerator {
37
38
  await this.connection.query(`CREATE DATABASE IF NOT EXISTS ${databaseName}`)
38
39
  await this.connection.query(`USE ${databaseName}`)
39
40
  }
40
-
41
41
  async pushModelToDB() {
42
42
  const migrator = new MyDBMigrator(this)
43
43
  await migrator.migrateTable(this.model)
44
44
  }
45
- // async pushViewToDB(viewDirectory: string) {
46
- // const migrator = new MyDBMigrator(this)
47
- // await migrator.migrateView(viewDirectory)
48
- // }
49
45
  async pushViewToDB_v2(viewModel: MyViewModel[]) {
50
46
  const migrator = new MyDBMigrator(this)
51
47
  await migrator.migrateView_v2(viewModel)
@@ -388,21 +384,3 @@ export default class ModelGenerator {
388
384
  return code
389
385
  }
390
386
  }
391
-
392
- function convertDataType(inputDataType: string): "string" | "number" {
393
- let result: "string" | "number" = "string"
394
- if (inputDataType === "int") {
395
- result = "number"
396
- } else if (inputDataType === "tinyint") {
397
- result = "number"
398
- } else if (inputDataType === "bigint") {
399
- result = "number"
400
- } else if (inputDataType === "date") {
401
- result = "string"
402
- } else if (inputDataType === "float") {
403
- result = "number"
404
- } else if (inputDataType === "double") {
405
- result = "number"
406
- }
407
- return result
408
- }
@@ -0,0 +1,17 @@
1
+ export function convertDataType(inputDataType: string): "string" | "number" {
2
+ let result: "string" | "number" = "string"
3
+ if (inputDataType === "int") {
4
+ result = "number"
5
+ } else if (inputDataType === "tinyint") {
6
+ result = "number"
7
+ } else if (inputDataType === "bigint") {
8
+ result = "number"
9
+ } else if (inputDataType === "date") {
10
+ result = "string"
11
+ } else if (inputDataType === "float") {
12
+ result = "number"
13
+ } else if (inputDataType === "double") {
14
+ result = "number"
15
+ }
16
+ return result
17
+ }
@@ -26,6 +26,7 @@ export default class MyDBMigrator {
26
26
  let index = 1
27
27
  for (let row of data) {
28
28
  console.log(chalk.bgBlue(`${index}/${data.length} ${row.tableName}`))
29
+ console.log(row)
29
30
  await this.createTableIfNotExist(row.tableName)
30
31
  this.onetimeLoadColumnContent = []
31
32
  await this.loadColumnContentIfEmpty()
@@ -43,6 +44,10 @@ export default class MyDBMigrator {
43
44
  col.POSSIBLE_VALUE
44
45
  )
45
46
  await this.alterForeignKey(row.tableName, col)
47
+ await this.alterUniqueColumnCombination(
48
+ row.tableName,
49
+ row.compoisteUniqueColumn
50
+ )
46
51
  }
47
52
  }
48
53
  console.log("migrate table done")
@@ -59,6 +64,23 @@ export default class MyDBMigrator {
59
64
  return data.length ? true : false
60
65
  }
61
66
 
67
+ private getCompositeUniqueName(
68
+ compositeUniqueColumn: Unarray<MyModel["compoisteUniqueColumn"]>
69
+ ) {
70
+ return `unique_${compositeUniqueColumn?.columnName.join("_")}`
71
+ }
72
+ private async isCompositeUniqueColumnExist(
73
+ tableName: string,
74
+ compositeUniqueColumn: Unarray<MyModel["compoisteUniqueColumn"]>
75
+ ) {
76
+ const compositeUniqueName = this.getCompositeUniqueName(
77
+ compositeUniqueColumn
78
+ )
79
+ const sql = `SHOW INDEX FROM ${tableName} WHERE Non_unique = 0 AND Key_name like '${compositeUniqueName}'`
80
+ const data = (await this.MyDB.query(sql)) as any[]
81
+ return data.length ? true : false
82
+ }
83
+
62
84
  private async alterForeignKey(
63
85
  tableName: string,
64
86
  col: Unarray<MyModel["columns"]>
@@ -69,7 +91,7 @@ export default class MyDBMigrator {
69
91
 
70
92
  const constraintExist = await this.isConstraintNameExist(CONSTRAINT)
71
93
  if (constraintExist) {
72
- this.MyDB.query(
94
+ await this.MyDB.query(
73
95
  `ALTER TABLE ${tableName} DROP FOREIGN KEY ${CONSTRAINT}`
74
96
  )
75
97
  }
@@ -82,6 +104,30 @@ export default class MyDBMigrator {
82
104
  }
83
105
  }
84
106
  }
107
+ private async alterUniqueColumnCombination(
108
+ tableName: string,
109
+ uniqueColumnCombination: MyModel["compoisteUniqueColumn"]
110
+ ) {
111
+ if (uniqueColumnCombination?.length) {
112
+ uniqueColumnCombination.forEach(async (row) => {
113
+ const isCompositeUniqueNameExist =
114
+ await this.isCompositeUniqueColumnExist(tableName, row)
115
+ // check is exist, then drop it first
116
+ if (isCompositeUniqueNameExist) {
117
+ await this.MyDB.query(
118
+ `ALTER TABLE ${tableName} DROP INDEX ${this.getCompositeUniqueName(
119
+ row
120
+ )}`
121
+ )
122
+ }
123
+ const uniqueKeyName = `unique_${row.columnName.join("_")}`
124
+ const temp = row.columnName.join(",")
125
+ await this.MyDB.query(
126
+ `ALTER TABLE ${tableName} ADD UNIQUE KEY ${uniqueKeyName} (${temp})`
127
+ )
128
+ })
129
+ }
130
+ }
85
131
 
86
132
  private async alterDataType(
87
133
  tableName: string,
@@ -184,7 +230,7 @@ export default class MyDBMigrator {
184
230
  return true
185
231
  }
186
232
  }
187
- async alterUniqueKey(
233
+ private async alterUniqueKey(
188
234
  tableName: string,
189
235
  columnName: string,
190
236
  IS_UNIQUE: boolean | undefined
@@ -210,7 +256,7 @@ export default class MyDBMigrator {
210
256
  }
211
257
  }
212
258
 
213
- async alterIndex(
259
+ private async alterIndex(
214
260
  tableName: string,
215
261
  columnName: string,
216
262
  IS_INDEX: boolean | undefined
@@ -233,7 +279,7 @@ export default class MyDBMigrator {
233
279
  }
234
280
  }
235
281
 
236
- async alterPossibleEnum(
282
+ private async alterPossibleEnum(
237
283
  tableName: string,
238
284
  columnName: string,
239
285
  possibleValue?: string[]
@@ -273,7 +319,7 @@ export default class MyDBMigrator {
273
319
  }
274
320
  }
275
321
 
276
- async migrateView_v2(viewModel: MyViewModel[]) {
322
+ public async migrateView_v2(viewModel: MyViewModel[]) {
277
323
  let unSuccessUpdateViewList: MyViewModel[] = viewModel
278
324
  let loopCount = 0
279
325
  while (unSuccessUpdateViewList.length > 0) {
@@ -306,7 +352,7 @@ export default class MyDBMigrator {
306
352
  }
307
353
  }
308
354
 
309
- async checkIsPrimaryKey(
355
+ private async checkIsPrimaryKey(
310
356
  databaseName: string,
311
357
  tableName: string,
312
358
  columnName: string
package/src/myModel.ts CHANGED
@@ -32,11 +32,16 @@ export interface columnContent {
32
32
  IS_UNIQUE?: boolean
33
33
  IS_INDEX?: boolean
34
34
  }
35
+
36
+ export interface compoisteUniqueColumn {
37
+ columnName: string[]
38
+ }
35
39
  export interface MyModel {
36
40
  isSeed?: boolean
37
41
  tableName: string
38
42
  columns: [...columnContent[]]
39
43
  meaning?: string
44
+ compoisteUniqueColumn?: compoisteUniqueColumn[]
40
45
  }
41
46
  export interface MyViewModel {
42
47
  viewName: string