gg-mysql-connector 1.0.94 → 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();
@@ -43,19 +43,25 @@ class MyDBMigrator {
43
43
  }
44
44
  getCompositeName(compositeUniqueColumn) {
45
45
  if ((compositeUniqueColumn === null || compositeUniqueColumn === void 0 ? void 0 : compositeUniqueColumn.type) === "INDEX")
46
- return `index_${compositeUniqueColumn === null || compositeUniqueColumn === void 0 ? void 0 : compositeUniqueColumn.columnName.join("_")}`;
46
+ return `my_index_${compositeUniqueColumn === null || compositeUniqueColumn === void 0 ? void 0 : compositeUniqueColumn.columnName.join("_")}`;
47
47
  if ((compositeUniqueColumn === null || compositeUniqueColumn === void 0 ? void 0 : compositeUniqueColumn.type) === "UNIQUE")
48
- return `unique_${compositeUniqueColumn === null || compositeUniqueColumn === void 0 ? void 0 : compositeUniqueColumn.columnName.join("_")}`;
48
+ return `my_unique_${compositeUniqueColumn === null || compositeUniqueColumn === void 0 ? void 0 : compositeUniqueColumn.columnName.join("_")}`;
49
49
  }
50
50
  async isCompositeUniqueColumnExist(tableName, compositeColumn) {
51
- const compositeUniqueName = this.getCompositeName(compositeColumn);
52
- const sql = `SHOW INDEX FROM ${tableName} WHERE Non_unique = 0 AND Key_name like "${compositeUniqueName}"`;
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}'`;
53
58
  const data = (await this.MyDB.query(sql));
54
59
  // console.log("sql", sql)
55
60
  // console.log("isCompositeUniqueColumnExist", data.length, data)
56
61
  return data.length ? true : false;
57
62
  }
58
63
  async alterForeignKey(tableName, col) {
64
+ console.log("alterForeignKey()");
59
65
  if (col.FOREIGN_KEY) {
60
66
  for (const row of col.FOREIGN_KEY) {
61
67
  const CONSTRAINT = `fk_${tableName}_${col.COLUMN_NAME}_${row.tableName}_${row.columnName}`.slice(0, 64);
@@ -73,21 +79,24 @@ class MyDBMigrator {
73
79
  }
74
80
  }
75
81
  async alterColumnCombination(tableName, uniqueColumnCombination) {
82
+ console.log("alterColumnCombination()");
76
83
  if (uniqueColumnCombination === null || uniqueColumnCombination === void 0 ? void 0 : uniqueColumnCombination.length) {
77
84
  for (let row of uniqueColumnCombination) {
78
85
  const isCompositeUniqueNameExist = await this.isCompositeUniqueColumnExist(tableName, row);
79
86
  // check is exist, then drop it first
87
+ const compositeName = this.getCompositeName(row);
80
88
  if (isCompositeUniqueNameExist) {
81
- await this.MyDB.query(`ALTER TABLE ${tableName} DROP INDEX ${this.getCompositeName(row)}`);
89
+ const stmt = `ALTER TABLE ${tableName} DROP INDEX ${compositeName}`;
90
+ await this.MyDB.query(stmt);
82
91
  }
83
92
  const type = row.type === "UNIQUE" ? `${row.type} KEY` : "INDEX";
84
- const compositeUniqueName = this.getCompositeName(row);
85
93
  const temp = row.columnName.join(",");
86
- await this.MyDB.query(`ALTER TABLE ${tableName} ADD ${type} ${compositeUniqueName} (${temp})`);
94
+ await this.MyDB.query(`ALTER TABLE ${tableName} ADD ${type} ${compositeName} (${temp})`);
87
95
  }
88
96
  }
89
97
  }
90
98
  async alterDataType(tableName, col) {
99
+ console.log("alterDataType()");
91
100
  const AutoIncrement = col.AUTO_INCREMENT ? "AUTO_INCREMENT" : "";
92
101
  const columnDefault = col.COLUMN_DEFAULT
93
102
  ? `DEFAULT ${col.COLUMN_DEFAULT}`
@@ -105,6 +114,7 @@ class MyDBMigrator {
105
114
  }
106
115
  }
107
116
  async alterPrimaryKey(tableName, col) {
117
+ console.log("alterPrimaryKey()");
108
118
  if (col.COLUMN_NAME === "id" && col.AUTO_INCREMENT === true) {
109
119
  const isPrimaryKey = await this.checkIsPrimaryKey(this.MyDB.dbInfo.database, tableName, col.COLUMN_NAME);
110
120
  if (isPrimaryKey === false)
@@ -151,6 +161,7 @@ class MyDBMigrator {
151
161
  }
152
162
  }
153
163
  async alterUniqueKey(tableName, columnName, IS_UNIQUE) {
164
+ console.log("alterUniqueKey()");
154
165
  if (IS_UNIQUE === true) {
155
166
  // ต้องโหลดใหม่ เพราะ กรณี ที่เพิ่ม column ใหม่ที่เป็น unique index แล้ว โค้ดก่อนหน้าจะเพิ่ม column เข้าเข้าไม่ก่อน
156
167
  // onetimeLoadColumnContent จะยังไม่มี ข้อมูล column ใหม่ ทำให้ไม่ยอม add unique key index ให้
@@ -171,6 +182,7 @@ class MyDBMigrator {
171
182
  }
172
183
  }
173
184
  async alterIndex(tableName, columnName, IS_INDEX) {
185
+ console.log("alterIndex()");
174
186
  const data = this.onetimeLoadColumnContent.find((row) => row.COLUMN_NAME === columnName && row.TABLE_NAME === tableName);
175
187
  if (data) {
176
188
  if ((data.COLUMN_KEY === "MUL" && !IS_INDEX) ||
@@ -184,13 +196,14 @@ class MyDBMigrator {
184
196
  }
185
197
  }
186
198
  async alterPossibleEnum(tableName, columnName, possibleValue, isNotNull) {
199
+ console.log("alterPossibleEnum()");
187
200
  if (!possibleValue)
188
201
  return null;
189
202
  const data = this.onetimeLoadColumnContent.find((row) => row.COLUMN_NAME === columnName && row.TABLE_NAME === tableName);
190
203
  const notNull = isNotNull ? "NOT NULL" : "";
191
204
  if (data) {
192
205
  let possibleValueString = possibleValue
193
- .map((row) => `"${row}"`)
206
+ .map((row) => `'${row}'`)
194
207
  .join(", ");
195
208
  try {
196
209
  await this.MyDB.query(`ALTER TABLE ${tableName} MODIFY COLUMN ${columnName} ENUM(${possibleValueString}) ${notNull} ;`);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "gg-mysql-connector",
3
- "version": "1.0.94",
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()
@@ -66,16 +66,20 @@ export default class MyDBMigrator {
66
66
  compositeUniqueColumn: Unarray<MyModel["compoisteColumn"]>
67
67
  ) {
68
68
  if (compositeUniqueColumn?.type === "INDEX")
69
- return `index_${compositeUniqueColumn?.columnName.join("_")}`
69
+ return `my_index_${compositeUniqueColumn?.columnName.join("_")}`
70
70
  if (compositeUniqueColumn?.type === "UNIQUE")
71
- return `unique_${compositeUniqueColumn?.columnName.join("_")}`
71
+ return `my_unique_${compositeUniqueColumn?.columnName.join("_")}`
72
72
  }
73
73
  private async isCompositeUniqueColumnExist(
74
74
  tableName: string,
75
75
  compositeColumn: Unarray<MyModel["compoisteColumn"]>
76
76
  ) {
77
- const compositeUniqueName = this.getCompositeName(compositeColumn)
78
- 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
+
79
83
  const data = (await this.MyDB.query(sql)) as any[]
80
84
  // console.log("sql", sql)
81
85
  // console.log("isCompositeUniqueColumnExist", data.length, data)
@@ -86,6 +90,7 @@ export default class MyDBMigrator {
86
90
  tableName: string,
87
91
  col: Unarray<MyModel["columns"]>
88
92
  ) {
93
+ console.log("alterForeignKey()")
89
94
  if (col.FOREIGN_KEY) {
90
95
  for (const row of col.FOREIGN_KEY) {
91
96
  const CONSTRAINT =
@@ -113,21 +118,21 @@ export default class MyDBMigrator {
113
118
  tableName: string,
114
119
  uniqueColumnCombination: MyModel["compoisteColumn"]
115
120
  ) {
121
+ console.log("alterColumnCombination()")
116
122
  if (uniqueColumnCombination?.length) {
117
123
  for (let row of uniqueColumnCombination) {
118
124
  const isCompositeUniqueNameExist =
119
125
  await this.isCompositeUniqueColumnExist(tableName, row)
120
126
  // check is exist, then drop it first
127
+ const compositeName = this.getCompositeName(row)
121
128
  if (isCompositeUniqueNameExist) {
122
- await this.MyDB.query(
123
- `ALTER TABLE ${tableName} DROP INDEX ${this.getCompositeName(row)}`
124
- )
129
+ const stmt = `ALTER TABLE ${tableName} DROP INDEX ${compositeName}`
130
+ await this.MyDB.query(stmt)
125
131
  }
126
132
  const type = row.type === "UNIQUE" ? `${row.type} KEY` : "INDEX"
127
- const compositeUniqueName = this.getCompositeName(row)
128
133
  const temp = row.columnName.join(",")
129
134
  await this.MyDB.query(
130
- `ALTER TABLE ${tableName} ADD ${type} ${compositeUniqueName} (${temp})`
135
+ `ALTER TABLE ${tableName} ADD ${type} ${compositeName} (${temp})`
131
136
  )
132
137
  }
133
138
  }
@@ -137,6 +142,7 @@ export default class MyDBMigrator {
137
142
  tableName: string,
138
143
  col: Unarray<MyModel["columns"]>
139
144
  ) {
145
+ console.log("alterDataType()")
140
146
  const AutoIncrement = col.AUTO_INCREMENT ? "AUTO_INCREMENT" : ""
141
147
  const columnDefault = col.COLUMN_DEFAULT
142
148
  ? `DEFAULT ${col.COLUMN_DEFAULT}`
@@ -169,6 +175,7 @@ export default class MyDBMigrator {
169
175
  tableName: string,
170
176
  col: Unarray<MyModel["columns"]>
171
177
  ) {
178
+ console.log("alterPrimaryKey()")
172
179
  if (col.COLUMN_NAME === "id" && col.AUTO_INCREMENT === true) {
173
180
  const isPrimaryKey = await this.checkIsPrimaryKey(
174
181
  this.MyDB.dbInfo.database,
@@ -240,6 +247,7 @@ export default class MyDBMigrator {
240
247
  columnName: string,
241
248
  IS_UNIQUE: boolean | undefined
242
249
  ) {
250
+ console.log("alterUniqueKey()")
243
251
  if (IS_UNIQUE === true) {
244
252
  // ต้องโหลดใหม่ เพราะ กรณี ที่เพิ่ม column ใหม่ที่เป็น unique index แล้ว โค้ดก่อนหน้าจะเพิ่ม column เข้าเข้าไม่ก่อน
245
253
  // onetimeLoadColumnContent จะยังไม่มี ข้อมูล column ใหม่ ทำให้ไม่ยอม add unique key index ให้
@@ -272,6 +280,7 @@ export default class MyDBMigrator {
272
280
  columnName: string,
273
281
  IS_INDEX: boolean | undefined
274
282
  ) {
283
+ console.log("alterIndex()")
275
284
  const data = this.onetimeLoadColumnContent.find(
276
285
  (row) => row.COLUMN_NAME === columnName && row.TABLE_NAME === tableName
277
286
  )
@@ -296,6 +305,7 @@ export default class MyDBMigrator {
296
305
  possibleValue?: string[],
297
306
  isNotNull?: boolean
298
307
  ) {
308
+ console.log("alterPossibleEnum()")
299
309
  if (!possibleValue) return null
300
310
  const data = this.onetimeLoadColumnContent.find(
301
311
  (row) => row.COLUMN_NAME === columnName && row.TABLE_NAME === tableName
@@ -303,7 +313,7 @@ export default class MyDBMigrator {
303
313
  const notNull = isNotNull ? "NOT NULL" : ""
304
314
  if (data) {
305
315
  let possibleValueString = possibleValue
306
- .map((row) => `"${row}"`)
316
+ .map((row) => `'${row}'`)
307
317
  .join(", ")
308
318
 
309
319
  try {