extract-mysql-schema 0.0.11 → 0.2.0

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.
package/example.js CHANGED
@@ -4,7 +4,7 @@ module.exports = {
4
4
  host: '127.0.0.1',
5
5
  user: 'root',
6
6
  password: 'password',
7
- database: 'db',
7
+ database: 'litservices',
8
8
  charset: 'utf8'
9
9
  }
10
10
  };
package/index.js CHANGED
@@ -1,5 +1,6 @@
1
1
  "use strict";
2
2
  const SequelizeAdapter = require("sequelize");
3
+ const orderBy = require("lodash.orderby");
3
4
 
4
5
  const getAdapter = async function (connection) {
5
6
  let adapter = new SequelizeAdapter(connection.database, connection.user, connection.password, {
@@ -52,6 +53,7 @@ const extractSchemas = async function (connection) {
52
53
 
53
54
  let schema = {};
54
55
  let tables = [];
56
+ let hasParent = [];
55
57
  for (let i = 0; i < columns.length; i++) {
56
58
  let name = columns[i]['COLUMN_NAME'];
57
59
 
@@ -91,16 +93,60 @@ const extractSchemas = async function (connection) {
91
93
 
92
94
  if(foreign[tableName+"_"+name]!==undefined) {
93
95
  column.references.push(foreign[tableName+"_"+name]);
96
+ if(hasParent.indexOf(tableName)<0) {
97
+ hasParent.push(tableName);
98
+ }
94
99
  }
95
100
 
96
101
  column = Object.fromEntries(Object.entries(column).filter(([_, v]) => v != null));
97
102
  table.push(column);
98
103
  }
99
104
 
105
+ let noparent = [];
106
+ for (let i = 0; i < tables.length; i++) {
107
+ if(hasParent.indexOf(tables[i].name)<0) {
108
+ noparent.push(tables[i].name);
109
+ }
110
+ }
111
+ noparent.sort();
112
+
113
+ let byCounts = {};
114
+ for (let i = 0; i < hasParent.length; i++) {
115
+ let tableName = hasParent[i];
116
+ let table = schema[hasParent[i]];
117
+ for (let j = 0; j < table.length; j++) {
118
+ var column = table[j];
119
+ var references = column.references;
120
+ if(column.references.length==0) continue;
121
+
122
+ for (let k = 0; k < references.length; k++) {
123
+ var reference = references[k];
124
+ if(byCounts[reference.tableName]===undefined) byCounts[reference.tableName]={name:reference.tableName,count:0,children:[]};
125
+ byCounts[reference.tableName].count++;
126
+ byCounts[reference.tableName].children.push(tableName);
127
+ }
128
+ }
129
+ }
130
+ byCounts = orderBy(byCounts,['count']).reverse();
131
+ let tableOrder = noparent;
132
+ for(let i=0;i<byCounts.length;i++){
133
+ if(tableOrder.indexOf(byCounts[i].name)<0)
134
+ tableOrder.push(byCounts[i].name);
135
+
136
+ let children = byCounts[i].children;
137
+ for(let j=0;j<children.length;j++){
138
+ let child=children[j];
139
+ if(tableOrder.indexOf(child)<0 && byCounts.indexOf(child)<0) {
140
+ tableOrder.push(child);
141
+ }
142
+ }
143
+ }
144
+
100
145
  let result = {};
101
146
  result[schemaName] = {
102
147
  name: schemaName,
103
148
  tables: tables,
149
+ tableOrder,
104
150
  views: []
105
151
  }
106
152
  return result;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "extract-mysql-schema",
3
- "version": "0.0.11",
3
+ "version": "0.2.0",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "bin": "./run.js",
@@ -20,5 +20,8 @@
20
20
  "dependencies": {
21
21
  "mysql2": "^3.6.3",
22
22
  "sequelize": "^6.34.0"
23
+ },
24
+ "devDependencies": {
25
+ "lodash.orderby": "^4.6.0"
23
26
  }
24
27
  }
package/run.js CHANGED
@@ -4,7 +4,7 @@ const { extractSchemas } = require('./index.js');
4
4
 
5
5
  async function main(options) {
6
6
  const config = require(path.join(process.cwd(),options.configFile));
7
- const result = await extractSchemas(config.connection);
7
+ const result = await extractSchemas(config.connection,options);
8
8
  if(options.outputFile) {
9
9
  fs.writeFileSync(path.join(process.cwd(),options.outputFile), JSON.stringify(result,null,2) ,"utf8")
10
10
  } else console.log(JSON.stringify(result,null,2));
@@ -13,6 +13,8 @@ async function main(options) {
13
13
  let options = {
14
14
  configFile:"",
15
15
  outputFile:"",
16
+ columnISV:false,
17
+ tableISV:false
16
18
  }
17
19
 
18
20
  if (process.argv.length === 2) {
@@ -22,6 +24,9 @@ process.exit(1);
22
24
  let argv = process.argv;
23
25
 
24
26
  for(let i=2;i<argv.length;i++) {
27
+ if(argv[i]==="--columnISV") options.columnISV=true;
28
+ else if(argv[i]==="--tableISV") options.tableISV=true;
29
+ else
25
30
  if(argv[i].substring(0,2)==="--") {
26
31
  let name = argv[i].substring(2);
27
32
  if(options[name]!==undefined) {