gg-mysql-connector 1.0.30 → 1.0.32

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.
@@ -66,7 +66,7 @@ export default class GGMySQLConnector<Main> implements ClassDBInterface<Main> {
66
66
  loadColumnListToCache(): Promise<void>;
67
67
  getColumnList(tableName: string): Promise<string[]>;
68
68
  getColumnFullList(tableName: string): Promise<mysql.RowDataPacket[]>;
69
- waitUntilInitSuccess(interval: number): Promise<unknown>;
69
+ waitUntilInitSuccess(interval: number): true | Promise<unknown>;
70
70
  query<T>(statment: string, parameter?: any[], isPrint?: boolean): Promise<T | QueryResult>;
71
71
  private printResultLength;
72
72
  }
@@ -245,6 +245,8 @@ class GGMySQLConnector {
245
245
  return result;
246
246
  }
247
247
  waitUntilInitSuccess(interval) {
248
+ if (this.isConnected === true)
249
+ return true;
248
250
  return new Promise((resolve, reject) => {
249
251
  setTimeout(() => {
250
252
  if (this.isConnected === false) {
@@ -252,7 +254,6 @@ class GGMySQLConnector {
252
254
  this.waitUntilInitSuccess(interval);
253
255
  }
254
256
  else {
255
- console.log(`${this.DBInfo.database} > database connectd`);
256
257
  return resolve(true);
257
258
  }
258
259
  }, interval);
@@ -27,4 +27,9 @@ export default class ModelGenerator {
27
27
  model: MyModel[];
28
28
  outputDirectory: string[];
29
29
  }): Promise<string>;
30
+ generateModelConstStructure(params: {
31
+ appName: string;
32
+ model: MyModel[];
33
+ outputDirectory: string[];
34
+ }): Promise<string>;
30
35
  }
@@ -56,7 +56,6 @@ class ModelGenerator {
56
56
  this.waitUntilInitSuccess(interval);
57
57
  }
58
58
  else {
59
- // console.log(`${this.dbInfo.database} > database connectd`)
60
59
  return resolve(true);
61
60
  }
62
61
  }, interval);
@@ -182,6 +181,81 @@ class ModelGenerator {
182
181
  console.log(`generate interface ${params.appName} ${chalk_1.default.bgGreen(" SUCCESS ")}`);
183
182
  return code;
184
183
  }
184
+ async generateModelConstStructure(params) {
185
+ const isTableNameExistInModel = (tableName) => {
186
+ if (tableName === "sessions")
187
+ return true;
188
+ const table = params.model.find((row) => row.tableName === tableName);
189
+ if (table)
190
+ return true;
191
+ return false;
192
+ };
193
+ const isColumnExistInModel = (tableName, columnName) => {
194
+ if (tableName === "sessions")
195
+ return true;
196
+ const table = params.model.find((row) => row.tableName === tableName);
197
+ if (table) {
198
+ const column = table.columns.find((row) => row.COLUMN_NAME === columnName);
199
+ if (column)
200
+ return true;
201
+ }
202
+ return false;
203
+ };
204
+ const getPossibleColumnValue = (tableName, columnName) => {
205
+ const foundTable = params.model.find((row) => row.tableName === tableName);
206
+ if (foundTable) {
207
+ const foundColumn = foundTable["columns"].find((row) => row.COLUMN_NAME === columnName);
208
+ if (foundColumn) {
209
+ if (foundColumn.POSSIBLE_VALUE)
210
+ return foundColumn.POSSIBLE_VALUE;
211
+ }
212
+ else
213
+ return false;
214
+ }
215
+ };
216
+ const tableList = [
217
+ ...(await this.getTableNameList()),
218
+ ...(await this.getViewNameList()),
219
+ ];
220
+ const array = [];
221
+ for (let i = 0; i < tableList.length; i++) {
222
+ const tableName = tableList[i];
223
+ if (isTableNameExistInModel(tableName) ||
224
+ tableName.search("_view") >= 0) {
225
+ const columnList = await this.getColumnFullList(tableName);
226
+ let value = "";
227
+ for (const cRow of columnList) {
228
+ const columnChecker = isColumnExistInModel(tableName, cRow.COLUMN_NAME);
229
+ if (columnChecker || tableName.search("_view") >= 0) {
230
+ const possibleValue = getPossibleColumnValue(cRow.TABLE_NAME, cRow.COLUMN_NAME);
231
+ if (possibleValue) {
232
+ value = `${value} \n ${cRow.COLUMN_NAME}: [${possibleValue
233
+ .map((row) => `"${row}"`)
234
+ .join(" , ")}],`;
235
+ }
236
+ else {
237
+ value = `${value} \n ${cRow.COLUMN_NAME}: "${convertDataType(cRow.DATA_TYPE)}",`;
238
+ }
239
+ }
240
+ else {
241
+ console.log(`${tableName}-${cRow.COLUMN_NAME} not exist in model `);
242
+ }
243
+ }
244
+ const str = ` ${tableName} : { ${value} }`;
245
+ array.push(str);
246
+ // console.log(`generate interface from ${tableName}`)
247
+ }
248
+ }
249
+ const fileName = `${params.appName}_model_const.ts`;
250
+ const code = `export const ${params.appName}_const = { ${array.join(",")} }`;
251
+ for (let row of params.outputDirectory) {
252
+ const serverFilePath = path_1.default.join(row, fileName);
253
+ await fs_1.default.writeFileSync(serverFilePath, code);
254
+ console.log("save to ", serverFilePath);
255
+ }
256
+ console.log(`generate interface ${params.appName} ${chalk_1.default.bgGreen(" SUCCESS ")}`);
257
+ return code;
258
+ }
185
259
  }
186
260
  exports.default = ModelGenerator;
187
261
  function convertDataType(inputDataType) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "gg-mysql-connector",
3
- "version": "1.0.30",
3
+ "version": "1.0.32",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -405,6 +405,7 @@ export default class GGMySQLConnector<Main> implements ClassDBInterface<Main> {
405
405
  return result
406
406
  }
407
407
  waitUntilInitSuccess(interval: number) {
408
+ if (this.isConnected === true) return true
408
409
  return new Promise((resolve, reject) => {
409
410
  setTimeout(() => {
410
411
  if (this.isConnected === false) {
@@ -413,13 +414,11 @@ export default class GGMySQLConnector<Main> implements ClassDBInterface<Main> {
413
414
  )
414
415
  this.waitUntilInitSuccess(interval)
415
416
  } else {
416
- console.log(`${this.DBInfo.database} > database connectd`)
417
417
  return resolve(true)
418
418
  }
419
419
  }, interval)
420
420
  })
421
421
  }
422
-
423
422
  async query<T>(
424
423
  statment: string,
425
424
  parameter?: any[],
@@ -56,7 +56,6 @@ export default class ModelGenerator {
56
56
  )
57
57
  this.waitUntilInitSuccess(interval)
58
58
  } else {
59
- // console.log(`${this.dbInfo.database} > database connectd`)
60
59
  return resolve(true)
61
60
  }
62
61
  }, interval)
@@ -152,12 +151,10 @@ export default class ModelGenerator {
152
151
  if (tableName === "sessions") return true
153
152
  const table = params.model.find((row) => row.tableName === tableName)
154
153
  if (table) return true
155
-
156
154
  return false
157
155
  }
158
156
  const isColumnExistInModel = (tableName: string, columnName: string) => {
159
157
  if (tableName === "sessions") return true
160
-
161
158
  const table = params.model.find((row) => row.tableName === tableName)
162
159
  if (table) {
163
160
  const column = table.columns.find(
@@ -239,6 +236,99 @@ export default class ModelGenerator {
239
236
 
240
237
  return code
241
238
  }
239
+
240
+ async generateModelConstStructure(params: {
241
+ appName: string
242
+ model: MyModel[]
243
+ outputDirectory: string[]
244
+ }) {
245
+ const isTableNameExistInModel = (tableName: string) => {
246
+ if (tableName === "sessions") return true
247
+ const table = params.model.find((row) => row.tableName === tableName)
248
+ if (table) return true
249
+ return false
250
+ }
251
+ const isColumnExistInModel = (tableName: string, columnName: string) => {
252
+ if (tableName === "sessions") return true
253
+ const table = params.model.find((row) => row.tableName === tableName)
254
+ if (table) {
255
+ const column = table.columns.find(
256
+ (row) => row.COLUMN_NAME === columnName
257
+ )
258
+ if (column) return true
259
+ }
260
+ return false
261
+ }
262
+ const getPossibleColumnValue = (tableName: string, columnName: string) => {
263
+ const foundTable = params.model.find((row) => row.tableName === tableName)
264
+ if (foundTable) {
265
+ const foundColumn = foundTable["columns"].find(
266
+ (row) => row.COLUMN_NAME === columnName
267
+ )
268
+ if (foundColumn) {
269
+ if (foundColumn.POSSIBLE_VALUE) return foundColumn.POSSIBLE_VALUE
270
+ } else return false
271
+ }
272
+ }
273
+ const tableList = [
274
+ ...(await this.getTableNameList()),
275
+ ...(await this.getViewNameList()),
276
+ ]
277
+
278
+ const array = []
279
+ for (let i = 0; i < tableList.length; i++) {
280
+ const tableName = tableList[i]
281
+ if (
282
+ isTableNameExistInModel(tableName) ||
283
+ tableName.search("_view") >= 0
284
+ ) {
285
+ const columnList = await this.getColumnFullList(tableName)
286
+
287
+ let value = ""
288
+ for (const cRow of columnList) {
289
+ const columnChecker = isColumnExistInModel(
290
+ tableName,
291
+ cRow.COLUMN_NAME
292
+ )
293
+ if (columnChecker || tableName.search("_view") >= 0) {
294
+ const possibleValue = getPossibleColumnValue(
295
+ cRow.TABLE_NAME,
296
+ cRow.COLUMN_NAME
297
+ )
298
+ if (possibleValue) {
299
+ value = `${value} \n ${cRow.COLUMN_NAME}: [${possibleValue
300
+ .map((row) => `"${row}"`)
301
+ .join(" , ")}],`
302
+ } else {
303
+ value = `${value} \n ${cRow.COLUMN_NAME}: "${convertDataType(
304
+ cRow.DATA_TYPE
305
+ )}",`
306
+ }
307
+ } else {
308
+ console.log(`${tableName}-${cRow.COLUMN_NAME} not exist in model `)
309
+ }
310
+ }
311
+
312
+ const str = ` ${tableName} : { ${value} }`
313
+ array.push(str)
314
+ // console.log(`generate interface from ${tableName}`)
315
+ }
316
+ }
317
+ const fileName = `${params.appName}_model_const.ts`
318
+ const code = `export const ${params.appName}_const = { ${array.join(",")} }`
319
+
320
+ for (let row of params.outputDirectory) {
321
+ const serverFilePath = path.join(row, fileName)
322
+ await fs.writeFileSync(serverFilePath, code)
323
+ console.log("save to ", serverFilePath)
324
+ }
325
+
326
+ console.log(
327
+ `generate interface ${params.appName} ${chalk.bgGreen(" SUCCESS ")}`
328
+ )
329
+
330
+ return code
331
+ }
242
332
  }
243
333
 
244
334
  function convertDataType(inputDataType: string): "string" | "number" {