gg-mysql-connector 1.0.31 → 1.0.33

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.
@@ -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
  }
@@ -181,6 +181,81 @@ class ModelGenerator {
181
181
  console.log(`generate interface ${params.appName} ${chalk_1.default.bgGreen(" SUCCESS ")}`);
182
182
  return code;
183
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}_model_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
+ }
184
259
  }
185
260
  exports.default = ModelGenerator;
186
261
  function convertDataType(inputDataType) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "gg-mysql-connector",
3
- "version": "1.0.31",
3
+ "version": "1.0.33",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -151,12 +151,10 @@ export default class ModelGenerator {
151
151
  if (tableName === "sessions") return true
152
152
  const table = params.model.find((row) => row.tableName === tableName)
153
153
  if (table) return true
154
-
155
154
  return false
156
155
  }
157
156
  const isColumnExistInModel = (tableName: string, columnName: string) => {
158
157
  if (tableName === "sessions") return true
159
-
160
158
  const table = params.model.find((row) => row.tableName === tableName)
161
159
  if (table) {
162
160
  const column = table.columns.find(
@@ -238,6 +236,101 @@ export default class ModelGenerator {
238
236
 
239
237
  return code
240
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}_model_const = { ${array.join(
319
+ ","
320
+ )} }`
321
+
322
+ for (let row of params.outputDirectory) {
323
+ const serverFilePath = path.join(row, fileName)
324
+ await fs.writeFileSync(serverFilePath, code)
325
+ console.log("save to ", serverFilePath)
326
+ }
327
+
328
+ console.log(
329
+ `generate interface ${params.appName} ${chalk.bgGreen(" SUCCESS ")}`
330
+ )
331
+
332
+ return code
333
+ }
241
334
  }
242
335
 
243
336
  function convertDataType(inputDataType: string): "string" | "number" {