gg-mysql-connector 1.0.79 → 1.0.83

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
  }
@@ -27,6 +27,7 @@ class MyDBMigrator {
27
27
  await this.alterIndex(row.tableName, col.COLUMN_NAME, col.IS_INDEX);
28
28
  await this.alterPossibleEnum(row.tableName, col.COLUMN_NAME, col.POSSIBLE_VALUE);
29
29
  await this.alterForeignKey(row.tableName, col);
30
+ await this.alterUniqueColumnCombination(row.tableName, row.compoisteUniqueColumn);
30
31
  }
31
32
  }
32
33
  console.log("migrate table done");
@@ -40,13 +41,22 @@ class MyDBMigrator {
40
41
  const data = (await this.MyDB.query(sql));
41
42
  return data.length ? true : false;
42
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
+ }
43
53
  async alterForeignKey(tableName, col) {
44
54
  if (col.FOREIGN_KEY) {
45
55
  for (const row of col.FOREIGN_KEY) {
46
56
  const CONSTRAINT = `fk_${tableName}_${col.COLUMN_NAME}_${row.tableName}_${row.columnName}`;
47
57
  const constraintExist = await this.isConstraintNameExist(CONSTRAINT);
48
58
  if (constraintExist) {
49
- this.MyDB.query(`ALTER TABLE ${tableName} DROP FOREIGN KEY ${CONSTRAINT}`);
59
+ await this.MyDB.query(`ALTER TABLE ${tableName} DROP FOREIGN KEY ${CONSTRAINT}`);
50
60
  }
51
61
  await this.MyDB
52
62
  .query(`ALTER TABLE ${tableName} ADD CONSTRAINT ${CONSTRAINT}
@@ -57,6 +67,20 @@ class MyDBMigrator {
57
67
  }
58
68
  }
59
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 compositeUniqueName = this.getCompositeUniqueName(row);
79
+ const temp = row.columnName.join(",");
80
+ await this.MyDB.query(`ALTER TABLE ${tableName} ADD UNIQUE KEY ${compositeUniqueName} (${temp})`);
81
+ });
82
+ }
83
+ }
60
84
  async alterDataType(tableName, col) {
61
85
  const AutoIncrement = col.AUTO_INCREMENT ? "AUTO_INCREMENT" : "";
62
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.79",
3
+ "version": "1.0.83",
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
+ }
@@ -44,6 +44,10 @@ export default class MyDBMigrator {
44
44
  col.POSSIBLE_VALUE
45
45
  )
46
46
  await this.alterForeignKey(row.tableName, col)
47
+ await this.alterUniqueColumnCombination(
48
+ row.tableName,
49
+ row.compoisteUniqueColumn
50
+ )
47
51
  }
48
52
  }
49
53
  console.log("migrate table done")
@@ -60,6 +64,23 @@ export default class MyDBMigrator {
60
64
  return data.length ? true : false
61
65
  }
62
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
+
63
84
  private async alterForeignKey(
64
85
  tableName: string,
65
86
  col: Unarray<MyModel["columns"]>
@@ -70,7 +91,7 @@ export default class MyDBMigrator {
70
91
 
71
92
  const constraintExist = await this.isConstraintNameExist(CONSTRAINT)
72
93
  if (constraintExist) {
73
- this.MyDB.query(
94
+ await this.MyDB.query(
74
95
  `ALTER TABLE ${tableName} DROP FOREIGN KEY ${CONSTRAINT}`
75
96
  )
76
97
  }
@@ -83,6 +104,30 @@ export default class MyDBMigrator {
83
104
  }
84
105
  }
85
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 compositeUniqueName = this.getCompositeUniqueName(row)
124
+ const temp = row.columnName.join(",")
125
+ await this.MyDB.query(
126
+ `ALTER TABLE ${tableName} ADD UNIQUE KEY ${compositeUniqueName} (${temp})`
127
+ )
128
+ })
129
+ }
130
+ }
86
131
 
87
132
  private async alterDataType(
88
133
  tableName: string,
@@ -185,7 +230,7 @@ export default class MyDBMigrator {
185
230
  return true
186
231
  }
187
232
  }
188
- async alterUniqueKey(
233
+ private async alterUniqueKey(
189
234
  tableName: string,
190
235
  columnName: string,
191
236
  IS_UNIQUE: boolean | undefined
@@ -211,7 +256,7 @@ export default class MyDBMigrator {
211
256
  }
212
257
  }
213
258
 
214
- async alterIndex(
259
+ private async alterIndex(
215
260
  tableName: string,
216
261
  columnName: string,
217
262
  IS_INDEX: boolean | undefined
@@ -234,7 +279,7 @@ export default class MyDBMigrator {
234
279
  }
235
280
  }
236
281
 
237
- async alterPossibleEnum(
282
+ private async alterPossibleEnum(
238
283
  tableName: string,
239
284
  columnName: string,
240
285
  possibleValue?: string[]
@@ -274,7 +319,7 @@ export default class MyDBMigrator {
274
319
  }
275
320
  }
276
321
 
277
- async migrateView_v2(viewModel: MyViewModel[]) {
322
+ public async migrateView_v2(viewModel: MyViewModel[]) {
278
323
  let unSuccessUpdateViewList: MyViewModel[] = viewModel
279
324
  let loopCount = 0
280
325
  while (unSuccessUpdateViewList.length > 0) {
@@ -307,7 +352,7 @@ export default class MyDBMigrator {
307
352
  }
308
353
  }
309
354
 
310
- async checkIsPrimaryKey(
355
+ private async checkIsPrimaryKey(
311
356
  databaseName: string,
312
357
  tableName: string,
313
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