gg-mysql-connector 1.0.93 → 1.0.95

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.
@@ -5,6 +5,7 @@ export interface DatabaseConfigInterface {
5
5
  user: string;
6
6
  password: string;
7
7
  database: string;
8
+ port: number;
8
9
  }
9
10
  export default class ModelGenerator {
10
11
  model: MyModel[];
@@ -31,6 +31,7 @@ class ModelGenerator {
31
31
  host: this.dbInfo.host,
32
32
  user: this.dbInfo.user,
33
33
  password: this.dbInfo.password,
34
+ port: this.dbInfo.port,
34
35
  });
35
36
  this.isConnected = true;
36
37
  await this.createDatabaseIfNotExist();
@@ -13,10 +13,10 @@ export default class MyDBMigrator {
13
13
  constructor(connection: ModelGenerator);
14
14
  migrateTable(model: MyModel[]): Promise<void>;
15
15
  private isConstraintNameExist;
16
- private getCompositeUniqueName;
16
+ private getCompositeName;
17
17
  private isCompositeUniqueColumnExist;
18
18
  private alterForeignKey;
19
- private alterUniqueColumnCombination;
19
+ private alterColumnCombination;
20
20
  private alterDataType;
21
21
  private alterPrimaryKey;
22
22
  private createTableIfNotExist;
@@ -28,7 +28,7 @@ class MyDBMigrator {
28
28
  await this.alterPossibleEnum(row.tableName, col.COLUMN_NAME, col.POSSIBLE_VALUE, col.IS_NOT_NULL);
29
29
  await this.alterForeignKey(row.tableName, col);
30
30
  }
31
- await this.alterUniqueColumnCombination(row.tableName, row.compoisteUniqueColumn);
31
+ await this.alterColumnCombination(row.tableName, row.compoisteColumn);
32
32
  }
33
33
  console.log("migrate table done");
34
34
  }
@@ -41,18 +41,27 @@ class MyDBMigrator {
41
41
  const data = (await this.MyDB.query(sql));
42
42
  return data.length ? true : false;
43
43
  }
44
- getCompositeUniqueName(compositeUniqueColumn) {
45
- return `unique_${compositeUniqueColumn === null || compositeUniqueColumn === void 0 ? void 0 : compositeUniqueColumn.columnName.join("_")}`;
44
+ getCompositeName(compositeUniqueColumn) {
45
+ if ((compositeUniqueColumn === null || compositeUniqueColumn === void 0 ? void 0 : compositeUniqueColumn.type) === "INDEX")
46
+ return `my_index_${compositeUniqueColumn === null || compositeUniqueColumn === void 0 ? void 0 : compositeUniqueColumn.columnName.join("_")}`;
47
+ if ((compositeUniqueColumn === null || compositeUniqueColumn === void 0 ? void 0 : compositeUniqueColumn.type) === "UNIQUE")
48
+ return `my_unique_${compositeUniqueColumn === null || compositeUniqueColumn === void 0 ? void 0 : compositeUniqueColumn.columnName.join("_")}`;
46
49
  }
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
+ async isCompositeUniqueColumnExist(tableName, compositeColumn) {
51
+ const compositeName = this.getCompositeName(compositeColumn);
52
+ let Non_unique;
53
+ if ((compositeColumn === null || compositeColumn === void 0 ? void 0 : compositeColumn.type) === "INDEX")
54
+ Non_unique = 1;
55
+ else if ((compositeColumn === null || compositeColumn === void 0 ? void 0 : compositeColumn.type) === "UNIQUE")
56
+ Non_unique = 0;
57
+ const sql = `SHOW INDEX FROM ${tableName} WHERE Non_unique = ${Non_unique} AND Key_name like '${compositeName}'`;
50
58
  const data = (await this.MyDB.query(sql));
51
59
  // console.log("sql", sql)
52
60
  // console.log("isCompositeUniqueColumnExist", data.length, data)
53
61
  return data.length ? true : false;
54
62
  }
55
63
  async alterForeignKey(tableName, col) {
64
+ console.log("alterForeignKey()");
56
65
  if (col.FOREIGN_KEY) {
57
66
  for (const row of col.FOREIGN_KEY) {
58
67
  const CONSTRAINT = `fk_${tableName}_${col.COLUMN_NAME}_${row.tableName}_${row.columnName}`.slice(0, 64);
@@ -69,21 +78,25 @@ class MyDBMigrator {
69
78
  }
70
79
  }
71
80
  }
72
- async alterUniqueColumnCombination(tableName, uniqueColumnCombination) {
81
+ async alterColumnCombination(tableName, uniqueColumnCombination) {
82
+ console.log("alterColumnCombination()");
73
83
  if (uniqueColumnCombination === null || uniqueColumnCombination === void 0 ? void 0 : uniqueColumnCombination.length) {
74
84
  for (let row of uniqueColumnCombination) {
75
85
  const isCompositeUniqueNameExist = await this.isCompositeUniqueColumnExist(tableName, row);
76
86
  // check is exist, then drop it first
87
+ const compositeName = this.getCompositeName(row);
77
88
  if (isCompositeUniqueNameExist) {
78
- await this.MyDB.query(`ALTER TABLE ${tableName} DROP INDEX ${this.getCompositeUniqueName(row)}`);
89
+ const stmt = `ALTER TABLE ${tableName} DROP INDEX ${compositeName}`;
90
+ await this.MyDB.query(stmt);
79
91
  }
80
- const compositeUniqueName = this.getCompositeUniqueName(row);
92
+ const type = row.type === "UNIQUE" ? `${row.type} KEY` : "INDEX";
81
93
  const temp = row.columnName.join(",");
82
- await this.MyDB.query(`ALTER TABLE ${tableName} ADD UNIQUE KEY ${compositeUniqueName} (${temp})`);
94
+ await this.MyDB.query(`ALTER TABLE ${tableName} ADD ${type} ${compositeName} (${temp})`);
83
95
  }
84
96
  }
85
97
  }
86
98
  async alterDataType(tableName, col) {
99
+ console.log("alterDataType()");
87
100
  const AutoIncrement = col.AUTO_INCREMENT ? "AUTO_INCREMENT" : "";
88
101
  const columnDefault = col.COLUMN_DEFAULT
89
102
  ? `DEFAULT ${col.COLUMN_DEFAULT}`
@@ -101,6 +114,7 @@ class MyDBMigrator {
101
114
  }
102
115
  }
103
116
  async alterPrimaryKey(tableName, col) {
117
+ console.log("alterPrimaryKey()");
104
118
  if (col.COLUMN_NAME === "id" && col.AUTO_INCREMENT === true) {
105
119
  const isPrimaryKey = await this.checkIsPrimaryKey(this.MyDB.dbInfo.database, tableName, col.COLUMN_NAME);
106
120
  if (isPrimaryKey === false)
@@ -147,6 +161,7 @@ class MyDBMigrator {
147
161
  }
148
162
  }
149
163
  async alterUniqueKey(tableName, columnName, IS_UNIQUE) {
164
+ console.log("alterUniqueKey()");
150
165
  if (IS_UNIQUE === true) {
151
166
  // ต้องโหลดใหม่ เพราะ กรณี ที่เพิ่ม column ใหม่ที่เป็น unique index แล้ว โค้ดก่อนหน้าจะเพิ่ม column เข้าเข้าไม่ก่อน
152
167
  // onetimeLoadColumnContent จะยังไม่มี ข้อมูล column ใหม่ ทำให้ไม่ยอม add unique key index ให้
@@ -167,6 +182,7 @@ class MyDBMigrator {
167
182
  }
168
183
  }
169
184
  async alterIndex(tableName, columnName, IS_INDEX) {
185
+ console.log("alterIndex()");
170
186
  const data = this.onetimeLoadColumnContent.find((row) => row.COLUMN_NAME === columnName && row.TABLE_NAME === tableName);
171
187
  if (data) {
172
188
  if ((data.COLUMN_KEY === "MUL" && !IS_INDEX) ||
@@ -180,13 +196,14 @@ class MyDBMigrator {
180
196
  }
181
197
  }
182
198
  async alterPossibleEnum(tableName, columnName, possibleValue, isNotNull) {
199
+ console.log("alterPossibleEnum()");
183
200
  if (!possibleValue)
184
201
  return null;
185
202
  const data = this.onetimeLoadColumnContent.find((row) => row.COLUMN_NAME === columnName && row.TABLE_NAME === tableName);
186
203
  const notNull = isNotNull ? "NOT NULL" : "";
187
204
  if (data) {
188
205
  let possibleValueString = possibleValue
189
- .map((row) => `"${row}"`)
206
+ .map((row) => `'${row}'`)
190
207
  .join(", ");
191
208
  try {
192
209
  await this.MyDB.query(`ALTER TABLE ${tableName} MODIFY COLUMN ${columnName} ENUM(${possibleValueString}) ${notNull} ;`);
@@ -16,7 +16,8 @@ export interface columnContent {
16
16
  IS_INDEX?: boolean;
17
17
  IS_NOT_NULL?: boolean;
18
18
  }
19
- export interface compoisteUniqueColumn {
19
+ export interface compoisteColumn {
20
+ type: "UNIQUE" | "INDEX";
20
21
  columnName: string[];
21
22
  }
22
23
  export interface MyModel {
@@ -24,7 +25,7 @@ export interface MyModel {
24
25
  tableName: string;
25
26
  columns: [...columnContent[]];
26
27
  meaning?: string;
27
- compoisteUniqueColumn?: compoisteUniqueColumn[];
28
+ compoisteColumn?: compoisteColumn[];
28
29
  }
29
30
  export interface MyViewModel {
30
31
  viewName: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "gg-mysql-connector",
3
- "version": "1.0.93",
3
+ "version": "1.0.95",
4
4
  "description": "",
5
5
  "main": "dist/src/index.js",
6
6
  "scripts": {
@@ -11,6 +11,7 @@ export interface DatabaseConfigInterface {
11
11
  user: string
12
12
  password: string
13
13
  database: string
14
+ port: number
14
15
  }
15
16
 
16
17
  export default class ModelGenerator {
@@ -31,6 +32,7 @@ export default class ModelGenerator {
31
32
  host: this.dbInfo.host,
32
33
  user: this.dbInfo.user,
33
34
  password: this.dbInfo.password,
35
+ port: this.dbInfo.port,
34
36
  })
35
37
  this.isConnected = true
36
38
  await this.createDatabaseIfNotExist()
@@ -46,10 +46,7 @@ export default class MyDBMigrator {
46
46
  )
47
47
  await this.alterForeignKey(row.tableName, col)
48
48
  }
49
- await this.alterUniqueColumnCombination(
50
- row.tableName,
51
- row.compoisteUniqueColumn
52
- )
49
+ await this.alterColumnCombination(row.tableName, row.compoisteColumn)
53
50
  }
54
51
  console.log("migrate table done")
55
52
  }
@@ -65,19 +62,24 @@ export default class MyDBMigrator {
65
62
  return data.length ? true : false
66
63
  }
67
64
 
68
- private getCompositeUniqueName(
69
- compositeUniqueColumn: Unarray<MyModel["compoisteUniqueColumn"]>
65
+ private getCompositeName(
66
+ compositeUniqueColumn: Unarray<MyModel["compoisteColumn"]>
70
67
  ) {
71
- return `unique_${compositeUniqueColumn?.columnName.join("_")}`
68
+ if (compositeUniqueColumn?.type === "INDEX")
69
+ return `my_index_${compositeUniqueColumn?.columnName.join("_")}`
70
+ if (compositeUniqueColumn?.type === "UNIQUE")
71
+ return `my_unique_${compositeUniqueColumn?.columnName.join("_")}`
72
72
  }
73
73
  private async isCompositeUniqueColumnExist(
74
74
  tableName: string,
75
- compositeUniqueColumn: Unarray<MyModel["compoisteUniqueColumn"]>
75
+ compositeColumn: Unarray<MyModel["compoisteColumn"]>
76
76
  ) {
77
- const compositeUniqueName = this.getCompositeUniqueName(
78
- compositeUniqueColumn
79
- )
80
- const sql = `SHOW INDEX FROM ${tableName} WHERE Non_unique = 0 AND Key_name like "${compositeUniqueName}"`
77
+ const compositeName = this.getCompositeName(compositeColumn)
78
+ let Non_unique
79
+ if (compositeColumn?.type === "INDEX") Non_unique = 1
80
+ else if (compositeColumn?.type === "UNIQUE") Non_unique = 0
81
+ const sql = `SHOW INDEX FROM ${tableName} WHERE Non_unique = ${Non_unique} AND Key_name like '${compositeName}'`
82
+
81
83
  const data = (await this.MyDB.query(sql)) as any[]
82
84
  // console.log("sql", sql)
83
85
  // console.log("isCompositeUniqueColumnExist", data.length, data)
@@ -88,6 +90,7 @@ export default class MyDBMigrator {
88
90
  tableName: string,
89
91
  col: Unarray<MyModel["columns"]>
90
92
  ) {
93
+ console.log("alterForeignKey()")
91
94
  if (col.FOREIGN_KEY) {
92
95
  for (const row of col.FOREIGN_KEY) {
93
96
  const CONSTRAINT =
@@ -111,26 +114,25 @@ export default class MyDBMigrator {
111
114
  }
112
115
  }
113
116
  }
114
- private async alterUniqueColumnCombination(
117
+ private async alterColumnCombination(
115
118
  tableName: string,
116
- uniqueColumnCombination: MyModel["compoisteUniqueColumn"]
119
+ uniqueColumnCombination: MyModel["compoisteColumn"]
117
120
  ) {
121
+ console.log("alterColumnCombination()")
118
122
  if (uniqueColumnCombination?.length) {
119
123
  for (let row of uniqueColumnCombination) {
120
124
  const isCompositeUniqueNameExist =
121
125
  await this.isCompositeUniqueColumnExist(tableName, row)
122
126
  // check is exist, then drop it first
127
+ const compositeName = this.getCompositeName(row)
123
128
  if (isCompositeUniqueNameExist) {
124
- await this.MyDB.query(
125
- `ALTER TABLE ${tableName} DROP INDEX ${this.getCompositeUniqueName(
126
- row
127
- )}`
128
- )
129
+ const stmt = `ALTER TABLE ${tableName} DROP INDEX ${compositeName}`
130
+ await this.MyDB.query(stmt)
129
131
  }
130
- const compositeUniqueName = this.getCompositeUniqueName(row)
132
+ const type = row.type === "UNIQUE" ? `${row.type} KEY` : "INDEX"
131
133
  const temp = row.columnName.join(",")
132
134
  await this.MyDB.query(
133
- `ALTER TABLE ${tableName} ADD UNIQUE KEY ${compositeUniqueName} (${temp})`
135
+ `ALTER TABLE ${tableName} ADD ${type} ${compositeName} (${temp})`
134
136
  )
135
137
  }
136
138
  }
@@ -140,6 +142,7 @@ export default class MyDBMigrator {
140
142
  tableName: string,
141
143
  col: Unarray<MyModel["columns"]>
142
144
  ) {
145
+ console.log("alterDataType()")
143
146
  const AutoIncrement = col.AUTO_INCREMENT ? "AUTO_INCREMENT" : ""
144
147
  const columnDefault = col.COLUMN_DEFAULT
145
148
  ? `DEFAULT ${col.COLUMN_DEFAULT}`
@@ -172,6 +175,7 @@ export default class MyDBMigrator {
172
175
  tableName: string,
173
176
  col: Unarray<MyModel["columns"]>
174
177
  ) {
178
+ console.log("alterPrimaryKey()")
175
179
  if (col.COLUMN_NAME === "id" && col.AUTO_INCREMENT === true) {
176
180
  const isPrimaryKey = await this.checkIsPrimaryKey(
177
181
  this.MyDB.dbInfo.database,
@@ -243,6 +247,7 @@ export default class MyDBMigrator {
243
247
  columnName: string,
244
248
  IS_UNIQUE: boolean | undefined
245
249
  ) {
250
+ console.log("alterUniqueKey()")
246
251
  if (IS_UNIQUE === true) {
247
252
  // ต้องโหลดใหม่ เพราะ กรณี ที่เพิ่ม column ใหม่ที่เป็น unique index แล้ว โค้ดก่อนหน้าจะเพิ่ม column เข้าเข้าไม่ก่อน
248
253
  // onetimeLoadColumnContent จะยังไม่มี ข้อมูล column ใหม่ ทำให้ไม่ยอม add unique key index ให้
@@ -275,6 +280,7 @@ export default class MyDBMigrator {
275
280
  columnName: string,
276
281
  IS_INDEX: boolean | undefined
277
282
  ) {
283
+ console.log("alterIndex()")
278
284
  const data = this.onetimeLoadColumnContent.find(
279
285
  (row) => row.COLUMN_NAME === columnName && row.TABLE_NAME === tableName
280
286
  )
@@ -299,6 +305,7 @@ export default class MyDBMigrator {
299
305
  possibleValue?: string[],
300
306
  isNotNull?: boolean
301
307
  ) {
308
+ console.log("alterPossibleEnum()")
302
309
  if (!possibleValue) return null
303
310
  const data = this.onetimeLoadColumnContent.find(
304
311
  (row) => row.COLUMN_NAME === columnName && row.TABLE_NAME === tableName
@@ -306,7 +313,7 @@ export default class MyDBMigrator {
306
313
  const notNull = isNotNull ? "NOT NULL" : ""
307
314
  if (data) {
308
315
  let possibleValueString = possibleValue
309
- .map((row) => `"${row}"`)
316
+ .map((row) => `'${row}'`)
310
317
  .join(", ")
311
318
 
312
319
  try {
package/src/myModel.ts CHANGED
@@ -34,15 +34,17 @@ export interface columnContent {
34
34
  IS_NOT_NULL?: boolean
35
35
  }
36
36
 
37
- export interface compoisteUniqueColumn {
37
+ export interface compoisteColumn {
38
+ type: "UNIQUE" | "INDEX"
38
39
  columnName: string[]
39
40
  }
41
+
40
42
  export interface MyModel {
41
43
  isSeed?: boolean
42
44
  tableName: string
43
45
  columns: [...columnContent[]]
44
46
  meaning?: string
45
- compoisteUniqueColumn?: compoisteUniqueColumn[]
47
+ compoisteColumn?: compoisteColumn[]
46
48
  }
47
49
  export interface MyViewModel {
48
50
  viewName: string