extract-mysql-schema 0.0.10 → 0.1.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 +1 -1
- package/index.js +36 -11
- package/package.json +1 -1
- package/run.js +9 -3
- package/schema.json +18534 -0
package/example.js
CHANGED
package/index.js
CHANGED
|
@@ -15,15 +15,36 @@ const getAdapter = async function (connection) {
|
|
|
15
15
|
return adapter;
|
|
16
16
|
}
|
|
17
17
|
|
|
18
|
-
const
|
|
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
25
|
const schemaName = connection.database;
|
|
26
|
+
options = options || {columnISV:false,tableISV:true};
|
|
20
27
|
|
|
21
28
|
let adapter = await getAdapter(connection);
|
|
22
29
|
|
|
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
|
+
|
|
23
43
|
let fkeys = await adapter.query(`
|
|
24
44
|
SELECT iif.*, iifc.FOR_COL_NAME, iifc.REF_COL_NAME
|
|
25
45
|
FROM INFORMATION_SCHEMA.INNODB_FOREIGN as iif
|
|
26
46
|
JOIN INFORMATION_SCHEMA.INNODB_FOREIGN_COLS as iifc on iifc.ID=iif.ID
|
|
47
|
+
WHERE iif.ID like '${schemaName}/%'
|
|
27
48
|
`);
|
|
28
49
|
fkeys=fkeys[0];
|
|
29
50
|
|
|
@@ -31,6 +52,7 @@ const extractSchemas = async function (connection) {
|
|
|
31
52
|
SELECT *
|
|
32
53
|
FROM INFORMATION_SCHEMA.COLUMNS
|
|
33
54
|
WHERE TABLE_SCHEMA = '${schemaName}'
|
|
55
|
+
ORDER BY TABLE_NAME,ORDINAL_POSITION
|
|
34
56
|
`);
|
|
35
57
|
await adapter.close();
|
|
36
58
|
|
|
@@ -60,19 +82,19 @@ const extractSchemas = async function (connection) {
|
|
|
60
82
|
if (schema[tableName]) table = schema[tableName];
|
|
61
83
|
else {
|
|
62
84
|
schema[tableName] = table;
|
|
63
|
-
|
|
85
|
+
let wrapper = {
|
|
64
86
|
name: tableName,
|
|
65
87
|
schemaName: schemaName,
|
|
66
88
|
kind: "table",
|
|
67
|
-
columns: table
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
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);
|
|
76
98
|
}
|
|
77
99
|
|
|
78
100
|
let column = {
|
|
@@ -89,6 +111,9 @@ const extractSchemas = async function (connection) {
|
|
|
89
111
|
references:[]
|
|
90
112
|
};
|
|
91
113
|
|
|
114
|
+
if(options.columnISV) {
|
|
115
|
+
column.informationSchemaValue = lowerize(columns[i]);
|
|
116
|
+
}
|
|
92
117
|
if(foreign[tableName+"_"+name]!==undefined) {
|
|
93
118
|
column.references.push(foreign[tableName+"_"+name]);
|
|
94
119
|
}
|
package/package.json
CHANGED
package/run.js
CHANGED
|
@@ -1,17 +1,20 @@
|
|
|
1
1
|
const fs = require("fs");
|
|
2
|
+
const path = require("path");
|
|
2
3
|
const { extractSchemas } = require('./index.js');
|
|
3
4
|
|
|
4
5
|
async function main(options) {
|
|
5
|
-
const config = require(options.configFile);
|
|
6
|
-
const result = await extractSchemas(config.connection);
|
|
6
|
+
const config = require(path.join(process.cwd(),options.configFile));
|
|
7
|
+
const result = await extractSchemas(config.connection,options);
|
|
7
8
|
if(options.outputFile) {
|
|
8
|
-
fs.writeFileSync(options.outputFile, JSON.stringify(result,null,2) ,"utf8")
|
|
9
|
+
fs.writeFileSync(path.join(process.cwd(),options.outputFile), JSON.stringify(result,null,2) ,"utf8")
|
|
9
10
|
} else console.log(JSON.stringify(result,null,2));
|
|
10
11
|
}
|
|
11
12
|
|
|
12
13
|
let options = {
|
|
13
14
|
configFile:"",
|
|
14
15
|
outputFile:"",
|
|
16
|
+
columnISV:false,
|
|
17
|
+
tableISV:false
|
|
15
18
|
}
|
|
16
19
|
|
|
17
20
|
if (process.argv.length === 2) {
|
|
@@ -21,6 +24,9 @@ process.exit(1);
|
|
|
21
24
|
let argv = process.argv;
|
|
22
25
|
|
|
23
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
|
|
24
30
|
if(argv[i].substring(0,2)==="--") {
|
|
25
31
|
let name = argv[i].substring(2);
|
|
26
32
|
if(options[name]!==undefined) {
|