gg-mysql-connector 1.0.94 → 1.0.96

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();
@@ -10,6 +10,7 @@ export default class MyDBMigrator {
10
10
  COLUMN_KEY: string;
11
11
  COLUMN_TYPE: string;
12
12
  }[];
13
+ foreignKeyStatementList: string[];
13
14
  constructor(connection: ModelGenerator);
14
15
  migrateTable(model: MyModel[]): Promise<void>;
15
16
  private isConstraintNameExist;
@@ -8,6 +8,7 @@ class MyDBMigrator {
8
8
  constructor(connection) {
9
9
  this.MyDB = connection;
10
10
  this.onetimeLoadColumnContent = [];
11
+ this.foreignKeyStatementList = [];
11
12
  }
12
13
  async migrateTable(model) {
13
14
  let data = model.filter((row) => row.tableName !== "sessions" && row.tableName.search("prisma") < 0);
@@ -30,6 +31,11 @@ class MyDBMigrator {
30
31
  }
31
32
  await this.alterColumnCombination(row.tableName, row.compoisteColumn);
32
33
  }
34
+ // execute foreignKeyStatement
35
+ console.log("execute foreignKeyStatement");
36
+ for (let row of this.foreignKeyStatementList) {
37
+ await this.MyDB.query(row);
38
+ }
33
39
  console.log("migrate table done");
34
40
  }
35
41
  async isConstraintNameExist(constraintName) {
@@ -43,19 +49,25 @@ class MyDBMigrator {
43
49
  }
44
50
  getCompositeName(compositeUniqueColumn) {
45
51
  if ((compositeUniqueColumn === null || compositeUniqueColumn === void 0 ? void 0 : compositeUniqueColumn.type) === "INDEX")
46
- return `index_${compositeUniqueColumn === null || compositeUniqueColumn === void 0 ? void 0 : compositeUniqueColumn.columnName.join("_")}`;
52
+ return `my_index_${compositeUniqueColumn === null || compositeUniqueColumn === void 0 ? void 0 : compositeUniqueColumn.columnName.join("_")}`;
47
53
  if ((compositeUniqueColumn === null || compositeUniqueColumn === void 0 ? void 0 : compositeUniqueColumn.type) === "UNIQUE")
48
- return `unique_${compositeUniqueColumn === null || compositeUniqueColumn === void 0 ? void 0 : compositeUniqueColumn.columnName.join("_")}`;
54
+ return `my_unique_${compositeUniqueColumn === null || compositeUniqueColumn === void 0 ? void 0 : compositeUniqueColumn.columnName.join("_")}`;
49
55
  }
50
56
  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}"`;
57
+ const compositeName = this.getCompositeName(compositeColumn);
58
+ let Non_unique;
59
+ if ((compositeColumn === null || compositeColumn === void 0 ? void 0 : compositeColumn.type) === "INDEX")
60
+ Non_unique = 1;
61
+ else if ((compositeColumn === null || compositeColumn === void 0 ? void 0 : compositeColumn.type) === "UNIQUE")
62
+ Non_unique = 0;
63
+ const sql = `SHOW INDEX FROM ${tableName} WHERE Non_unique = ${Non_unique} AND Key_name like '${compositeName}'`;
53
64
  const data = (await this.MyDB.query(sql));
54
65
  // console.log("sql", sql)
55
66
  // console.log("isCompositeUniqueColumnExist", data.length, data)
56
67
  return data.length ? true : false;
57
68
  }
58
69
  async alterForeignKey(tableName, col) {
70
+ console.log("alterForeignKey()");
59
71
  if (col.FOREIGN_KEY) {
60
72
  for (const row of col.FOREIGN_KEY) {
61
73
  const CONSTRAINT = `fk_${tableName}_${col.COLUMN_NAME}_${row.tableName}_${row.columnName}`.slice(0, 64);
@@ -63,31 +75,35 @@ class MyDBMigrator {
63
75
  if (constraintExist) {
64
76
  await this.MyDB.query(`ALTER TABLE ${tableName} DROP FOREIGN KEY ${CONSTRAINT}`);
65
77
  }
66
- await this.MyDB
67
- .query(`ALTER TABLE ${tableName} ADD CONSTRAINT ${CONSTRAINT}
78
+ const statement = `ALTER TABLE ${tableName} ADD CONSTRAINT ${CONSTRAINT}
68
79
  FOREIGN KEY (${col.COLUMN_NAME})
69
80
  REFERENCES ${row.tableName}(${row.columnName})
70
81
  ON DELETE ${row.ON_DELETE}
71
- ON UPDATE ${row.ON_UPDATE}`);
82
+ ON UPDATE ${row.ON_UPDATE}`;
83
+ // await this.MyDB.query(statement)
84
+ this.foreignKeyStatementList.push(statement);
72
85
  }
73
86
  }
74
87
  }
75
88
  async alterColumnCombination(tableName, uniqueColumnCombination) {
89
+ console.log("alterColumnCombination()");
76
90
  if (uniqueColumnCombination === null || uniqueColumnCombination === void 0 ? void 0 : uniqueColumnCombination.length) {
77
91
  for (let row of uniqueColumnCombination) {
78
92
  const isCompositeUniqueNameExist = await this.isCompositeUniqueColumnExist(tableName, row);
79
93
  // check is exist, then drop it first
94
+ const compositeName = this.getCompositeName(row);
80
95
  if (isCompositeUniqueNameExist) {
81
- await this.MyDB.query(`ALTER TABLE ${tableName} DROP INDEX ${this.getCompositeName(row)}`);
96
+ const stmt = `ALTER TABLE ${tableName} DROP INDEX ${compositeName}`;
97
+ await this.MyDB.query(stmt);
82
98
  }
83
99
  const type = row.type === "UNIQUE" ? `${row.type} KEY` : "INDEX";
84
- const compositeUniqueName = this.getCompositeName(row);
85
100
  const temp = row.columnName.join(",");
86
- await this.MyDB.query(`ALTER TABLE ${tableName} ADD ${type} ${compositeUniqueName} (${temp})`);
101
+ await this.MyDB.query(`ALTER TABLE ${tableName} ADD ${type} ${compositeName} (${temp})`);
87
102
  }
88
103
  }
89
104
  }
90
105
  async alterDataType(tableName, col) {
106
+ console.log("alterDataType()");
91
107
  const AutoIncrement = col.AUTO_INCREMENT ? "AUTO_INCREMENT" : "";
92
108
  const columnDefault = col.COLUMN_DEFAULT
93
109
  ? `DEFAULT ${col.COLUMN_DEFAULT}`
@@ -105,6 +121,7 @@ class MyDBMigrator {
105
121
  }
106
122
  }
107
123
  async alterPrimaryKey(tableName, col) {
124
+ console.log("alterPrimaryKey()");
108
125
  if (col.COLUMN_NAME === "id" && col.AUTO_INCREMENT === true) {
109
126
  const isPrimaryKey = await this.checkIsPrimaryKey(this.MyDB.dbInfo.database, tableName, col.COLUMN_NAME);
110
127
  if (isPrimaryKey === false)
@@ -151,6 +168,7 @@ class MyDBMigrator {
151
168
  }
152
169
  }
153
170
  async alterUniqueKey(tableName, columnName, IS_UNIQUE) {
171
+ console.log("alterUniqueKey()");
154
172
  if (IS_UNIQUE === true) {
155
173
  // ต้องโหลดใหม่ เพราะ กรณี ที่เพิ่ม column ใหม่ที่เป็น unique index แล้ว โค้ดก่อนหน้าจะเพิ่ม column เข้าเข้าไม่ก่อน
156
174
  // onetimeLoadColumnContent จะยังไม่มี ข้อมูล column ใหม่ ทำให้ไม่ยอม add unique key index ให้
@@ -171,6 +189,7 @@ class MyDBMigrator {
171
189
  }
172
190
  }
173
191
  async alterIndex(tableName, columnName, IS_INDEX) {
192
+ console.log("alterIndex()");
174
193
  const data = this.onetimeLoadColumnContent.find((row) => row.COLUMN_NAME === columnName && row.TABLE_NAME === tableName);
175
194
  if (data) {
176
195
  if ((data.COLUMN_KEY === "MUL" && !IS_INDEX) ||
@@ -184,13 +203,14 @@ class MyDBMigrator {
184
203
  }
185
204
  }
186
205
  async alterPossibleEnum(tableName, columnName, possibleValue, isNotNull) {
206
+ console.log("alterPossibleEnum()");
187
207
  if (!possibleValue)
188
208
  return null;
189
209
  const data = this.onetimeLoadColumnContent.find((row) => row.COLUMN_NAME === columnName && row.TABLE_NAME === tableName);
190
210
  const notNull = isNotNull ? "NOT NULL" : "";
191
211
  if (data) {
192
212
  let possibleValueString = possibleValue
193
- .map((row) => `"${row}"`)
213
+ .map((row) => `'${row}'`)
194
214
  .join(", ");
195
215
  try {
196
216
  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.96",
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()
@@ -13,9 +13,12 @@ export default class MyDBMigrator {
13
13
  COLUMN_KEY: string
14
14
  COLUMN_TYPE: string
15
15
  }[]
16
+ foreignKeyStatementList: string[]
17
+
16
18
  constructor(connection: ModelGenerator) {
17
19
  this.MyDB = connection
18
20
  this.onetimeLoadColumnContent = []
21
+ this.foreignKeyStatementList = []
19
22
  }
20
23
 
21
24
  async migrateTable(model: MyModel[]) {
@@ -48,6 +51,11 @@ export default class MyDBMigrator {
48
51
  }
49
52
  await this.alterColumnCombination(row.tableName, row.compoisteColumn)
50
53
  }
54
+ // execute foreignKeyStatement
55
+ console.log("execute foreignKeyStatement")
56
+ for (let row of this.foreignKeyStatementList) {
57
+ await this.MyDB.query(row)
58
+ }
51
59
  console.log("migrate table done")
52
60
  }
53
61
 
@@ -66,16 +74,20 @@ export default class MyDBMigrator {
66
74
  compositeUniqueColumn: Unarray<MyModel["compoisteColumn"]>
67
75
  ) {
68
76
  if (compositeUniqueColumn?.type === "INDEX")
69
- return `index_${compositeUniqueColumn?.columnName.join("_")}`
77
+ return `my_index_${compositeUniqueColumn?.columnName.join("_")}`
70
78
  if (compositeUniqueColumn?.type === "UNIQUE")
71
- return `unique_${compositeUniqueColumn?.columnName.join("_")}`
79
+ return `my_unique_${compositeUniqueColumn?.columnName.join("_")}`
72
80
  }
73
81
  private async isCompositeUniqueColumnExist(
74
82
  tableName: string,
75
83
  compositeColumn: Unarray<MyModel["compoisteColumn"]>
76
84
  ) {
77
- const compositeUniqueName = this.getCompositeName(compositeColumn)
78
- const sql = `SHOW INDEX FROM ${tableName} WHERE Non_unique = 0 AND Key_name like "${compositeUniqueName}"`
85
+ const compositeName = this.getCompositeName(compositeColumn)
86
+ let Non_unique
87
+ if (compositeColumn?.type === "INDEX") Non_unique = 1
88
+ else if (compositeColumn?.type === "UNIQUE") Non_unique = 0
89
+ const sql = `SHOW INDEX FROM ${tableName} WHERE Non_unique = ${Non_unique} AND Key_name like '${compositeName}'`
90
+
79
91
  const data = (await this.MyDB.query(sql)) as any[]
80
92
  // console.log("sql", sql)
81
93
  // console.log("isCompositeUniqueColumnExist", data.length, data)
@@ -86,6 +98,7 @@ export default class MyDBMigrator {
86
98
  tableName: string,
87
99
  col: Unarray<MyModel["columns"]>
88
100
  ) {
101
+ console.log("alterForeignKey()")
89
102
  if (col.FOREIGN_KEY) {
90
103
  for (const row of col.FOREIGN_KEY) {
91
104
  const CONSTRAINT =
@@ -100,12 +113,13 @@ export default class MyDBMigrator {
100
113
  `ALTER TABLE ${tableName} DROP FOREIGN KEY ${CONSTRAINT}`
101
114
  )
102
115
  }
103
- await this.MyDB
104
- .query(`ALTER TABLE ${tableName} ADD CONSTRAINT ${CONSTRAINT}
116
+ const statement = `ALTER TABLE ${tableName} ADD CONSTRAINT ${CONSTRAINT}
105
117
  FOREIGN KEY (${col.COLUMN_NAME})
106
118
  REFERENCES ${row.tableName}(${row.columnName})
107
119
  ON DELETE ${row.ON_DELETE}
108
- ON UPDATE ${row.ON_UPDATE}`)
120
+ ON UPDATE ${row.ON_UPDATE}`
121
+ // await this.MyDB.query(statement)
122
+ this.foreignKeyStatementList.push(statement)
109
123
  }
110
124
  }
111
125
  }
@@ -113,21 +127,21 @@ export default class MyDBMigrator {
113
127
  tableName: string,
114
128
  uniqueColumnCombination: MyModel["compoisteColumn"]
115
129
  ) {
130
+ console.log("alterColumnCombination()")
116
131
  if (uniqueColumnCombination?.length) {
117
132
  for (let row of uniqueColumnCombination) {
118
133
  const isCompositeUniqueNameExist =
119
134
  await this.isCompositeUniqueColumnExist(tableName, row)
120
135
  // check is exist, then drop it first
136
+ const compositeName = this.getCompositeName(row)
121
137
  if (isCompositeUniqueNameExist) {
122
- await this.MyDB.query(
123
- `ALTER TABLE ${tableName} DROP INDEX ${this.getCompositeName(row)}`
124
- )
138
+ const stmt = `ALTER TABLE ${tableName} DROP INDEX ${compositeName}`
139
+ await this.MyDB.query(stmt)
125
140
  }
126
141
  const type = row.type === "UNIQUE" ? `${row.type} KEY` : "INDEX"
127
- const compositeUniqueName = this.getCompositeName(row)
128
142
  const temp = row.columnName.join(",")
129
143
  await this.MyDB.query(
130
- `ALTER TABLE ${tableName} ADD ${type} ${compositeUniqueName} (${temp})`
144
+ `ALTER TABLE ${tableName} ADD ${type} ${compositeName} (${temp})`
131
145
  )
132
146
  }
133
147
  }
@@ -137,6 +151,7 @@ export default class MyDBMigrator {
137
151
  tableName: string,
138
152
  col: Unarray<MyModel["columns"]>
139
153
  ) {
154
+ console.log("alterDataType()")
140
155
  const AutoIncrement = col.AUTO_INCREMENT ? "AUTO_INCREMENT" : ""
141
156
  const columnDefault = col.COLUMN_DEFAULT
142
157
  ? `DEFAULT ${col.COLUMN_DEFAULT}`
@@ -169,6 +184,7 @@ export default class MyDBMigrator {
169
184
  tableName: string,
170
185
  col: Unarray<MyModel["columns"]>
171
186
  ) {
187
+ console.log("alterPrimaryKey()")
172
188
  if (col.COLUMN_NAME === "id" && col.AUTO_INCREMENT === true) {
173
189
  const isPrimaryKey = await this.checkIsPrimaryKey(
174
190
  this.MyDB.dbInfo.database,
@@ -240,6 +256,7 @@ export default class MyDBMigrator {
240
256
  columnName: string,
241
257
  IS_UNIQUE: boolean | undefined
242
258
  ) {
259
+ console.log("alterUniqueKey()")
243
260
  if (IS_UNIQUE === true) {
244
261
  // ต้องโหลดใหม่ เพราะ กรณี ที่เพิ่ม column ใหม่ที่เป็น unique index แล้ว โค้ดก่อนหน้าจะเพิ่ม column เข้าเข้าไม่ก่อน
245
262
  // onetimeLoadColumnContent จะยังไม่มี ข้อมูล column ใหม่ ทำให้ไม่ยอม add unique key index ให้
@@ -272,6 +289,7 @@ export default class MyDBMigrator {
272
289
  columnName: string,
273
290
  IS_INDEX: boolean | undefined
274
291
  ) {
292
+ console.log("alterIndex()")
275
293
  const data = this.onetimeLoadColumnContent.find(
276
294
  (row) => row.COLUMN_NAME === columnName && row.TABLE_NAME === tableName
277
295
  )
@@ -296,6 +314,7 @@ export default class MyDBMigrator {
296
314
  possibleValue?: string[],
297
315
  isNotNull?: boolean
298
316
  ) {
317
+ console.log("alterPossibleEnum()")
299
318
  if (!possibleValue) return null
300
319
  const data = this.onetimeLoadColumnContent.find(
301
320
  (row) => row.COLUMN_NAME === columnName && row.TABLE_NAME === tableName
@@ -303,7 +322,7 @@ export default class MyDBMigrator {
303
322
  const notNull = isNotNull ? "NOT NULL" : ""
304
323
  if (data) {
305
324
  let possibleValueString = possibleValue
306
- .map((row) => `"${row}"`)
325
+ .map((row) => `'${row}'`)
307
326
  .join(", ")
308
327
 
309
328
  try {