extract-mysql-schema 0.1.0 → 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.
Files changed (2) hide show
  1. package/index.js +57 -36
  2. package/package.json +4 -1
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, {
@@ -15,36 +16,15 @@ const getAdapter = async function (connection) {
15
16
  return adapter;
16
17
  }
17
18
 
18
- const lowerize = obj =>
19
- Object.keys(obj).reduce((acc, k) => {
20
- acc[k.toLowerCase()] = obj[k];
21
- return acc;
22
- }, {});
23
-
24
- const extractSchemas = async function (connection,options) {
19
+ const extractSchemas = async function (connection) {
25
20
  const schemaName = connection.database;
26
- options = options || {columnISV:false,tableISV:true};
27
21
 
28
22
  let adapter = await getAdapter(connection);
29
23
 
30
- let tableISV={};
31
- if(options.tableISV) {
32
- let queryTableISV = await adapter.query(`
33
- SELECT *
34
- FROM INFORMATION_SCHEMA.TABLES
35
- WHERE TABLE_SCHEMA = '${schemaName}'
36
- `);
37
- queryTableISV = queryTableISV[0];
38
- for(let i=0;i<queryTableISV.length;i++){
39
- tableISV[queryTableISV[i]["TABLE_NAME"]]=lowerize(queryTableISV[i]);
40
- }
41
- }
42
-
43
24
  let fkeys = await adapter.query(`
44
25
  SELECT iif.*, iifc.FOR_COL_NAME, iifc.REF_COL_NAME
45
26
  FROM INFORMATION_SCHEMA.INNODB_FOREIGN as iif
46
27
  JOIN INFORMATION_SCHEMA.INNODB_FOREIGN_COLS as iifc on iifc.ID=iif.ID
47
- WHERE iif.ID like '${schemaName}/%'
48
28
  `);
49
29
  fkeys=fkeys[0];
50
30
 
@@ -52,7 +32,6 @@ const extractSchemas = async function (connection,options) {
52
32
  SELECT *
53
33
  FROM INFORMATION_SCHEMA.COLUMNS
54
34
  WHERE TABLE_SCHEMA = '${schemaName}'
55
- ORDER BY TABLE_NAME,ORDINAL_POSITION
56
35
  `);
57
36
  await adapter.close();
58
37
 
@@ -74,6 +53,7 @@ const extractSchemas = async function (connection,options) {
74
53
 
75
54
  let schema = {};
76
55
  let tables = [];
56
+ let hasParent = [];
77
57
  for (let i = 0; i < columns.length; i++) {
78
58
  let name = columns[i]['COLUMN_NAME'];
79
59
 
@@ -82,19 +62,19 @@ const extractSchemas = async function (connection,options) {
82
62
  if (schema[tableName]) table = schema[tableName];
83
63
  else {
84
64
  schema[tableName] = table;
85
- let wrapper = {
65
+ tables.push({
86
66
  name: tableName,
87
67
  schemaName: schemaName,
88
68
  kind: "table",
89
- columns: table
90
- };
91
- if(options.tableISV) {
92
- let isv = tableISV[tableName];
93
- if(isv.table_type==='BASE TABLE') isv.table_type="BASE";
94
- isv.is_insertable_into=isv.is_insertable_into||'YES';
95
- wrapper.informationSchemaValue=isv;
96
- }
97
- tables.push(wrapper);
69
+ columns: table,
70
+ informationSchemaValue: {
71
+ is_insertable_into: 'YES',
72
+ table_type: 'BASE',
73
+ table_catalog: schemaName,
74
+ table_name: tableName,
75
+ table_schema: schemaName
76
+ }
77
+ });
98
78
  }
99
79
 
100
80
  let column = {
@@ -111,21 +91,62 @@ const extractSchemas = async function (connection,options) {
111
91
  references:[]
112
92
  };
113
93
 
114
- if(options.columnISV) {
115
- column.informationSchemaValue = lowerize(columns[i]);
116
- }
117
94
  if(foreign[tableName+"_"+name]!==undefined) {
118
95
  column.references.push(foreign[tableName+"_"+name]);
96
+ if(hasParent.indexOf(tableName)<0) {
97
+ hasParent.push(tableName);
98
+ }
119
99
  }
120
100
 
121
101
  column = Object.fromEntries(Object.entries(column).filter(([_, v]) => v != null));
122
102
  table.push(column);
123
103
  }
124
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
+
125
145
  let result = {};
126
146
  result[schemaName] = {
127
147
  name: schemaName,
128
148
  tables: tables,
149
+ tableOrder,
129
150
  views: []
130
151
  }
131
152
  return result;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "extract-mysql-schema",
3
- "version": "0.1.0",
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
  }