extract-mysql-schema 0.0.7 → 0.0.9
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 +10 -0
- package/index.js +29 -2
- package/package.json +1 -1
- package/run.js +44 -10
package/example.js
ADDED
package/index.js
CHANGED
|
@@ -19,6 +19,14 @@ const extractSchemas = async function (connection) {
|
|
|
19
19
|
const schemaName = connection.database;
|
|
20
20
|
|
|
21
21
|
let adapter = await getAdapter(connection);
|
|
22
|
+
|
|
23
|
+
let fkeys = await adapter.query(`
|
|
24
|
+
SELECT iif.*, iifc.FOR_COL_NAME, iifc.REF_COL_NAME
|
|
25
|
+
FROM INFORMATION_SCHEMA.INNODB_FOREIGN as iif
|
|
26
|
+
JOIN INFORMATION_SCHEMA.INNODB_FOREIGN_COLS as iifc on iifc.ID=iif.ID
|
|
27
|
+
`);
|
|
28
|
+
fkeys=fkeys[0];
|
|
29
|
+
|
|
22
30
|
let columns = await adapter.query(`
|
|
23
31
|
SELECT *
|
|
24
32
|
FROM INFORMATION_SCHEMA.COLUMNS
|
|
@@ -28,6 +36,20 @@ const extractSchemas = async function (connection) {
|
|
|
28
36
|
|
|
29
37
|
columns = columns[0];
|
|
30
38
|
|
|
39
|
+
const foreign = {};
|
|
40
|
+
for(let i=0;i<fkeys.length;i++) {
|
|
41
|
+
const tableName = fkeys[i]['FOR_NAME'].substring(schemaName.length+1);
|
|
42
|
+
const keyName = fkeys[i]['ID'].substring(schemaName.length+1);
|
|
43
|
+
foreign[tableName+"_"+fkeys[i]['FOR_COL_NAME']] = {
|
|
44
|
+
"schemaName": schemaName,
|
|
45
|
+
"tableName": fkeys[i]['REF_NAME'].substring(schemaName.length+1),
|
|
46
|
+
"columnName": fkeys[i]['FOR_COL_NAME'],
|
|
47
|
+
"onUpdate": "CASCADE",
|
|
48
|
+
"onDelete": "RESTRICT",
|
|
49
|
+
"name": keyName
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
|
|
31
53
|
let schema = {};
|
|
32
54
|
let tables = [];
|
|
33
55
|
for (let i = 0; i < columns.length; i++) {
|
|
@@ -63,9 +85,14 @@ const extractSchemas = async function (connection) {
|
|
|
63
85
|
generated: columns[i]['EXTRA'].indexOf('DEFAULT_GENERATED') >= 0 ? (columns[i]['EXTRA'].indexOf('on update') > 0 ? "ALWAYS" : "BY DEFAULT") : "NEVER",
|
|
64
86
|
isUpdatable: columns[i]['EXTRA'].indexOf('DEFAULT_GENERATED') < 0,
|
|
65
87
|
type: columns[i]['DATA_TYPE'],
|
|
66
|
-
defaultValue: columns[i]['COLUMN_DEFAULT'] || ""
|
|
88
|
+
defaultValue: columns[i]['COLUMN_DEFAULT'] || "",
|
|
89
|
+
references:[]
|
|
67
90
|
};
|
|
68
91
|
|
|
92
|
+
if(foreign[tableName+"_"+name]!==undefined) {
|
|
93
|
+
column.references.push(foreign[tableName+"_"+name]);
|
|
94
|
+
}
|
|
95
|
+
|
|
69
96
|
column = Object.fromEntries(Object.entries(column).filter(([_, v]) => v != null));
|
|
70
97
|
table.push(column);
|
|
71
98
|
}
|
|
@@ -81,4 +108,4 @@ const extractSchemas = async function (connection) {
|
|
|
81
108
|
|
|
82
109
|
module.exports = {
|
|
83
110
|
extractSchemas
|
|
84
|
-
};
|
|
111
|
+
};
|
package/package.json
CHANGED
package/run.js
CHANGED
|
@@ -1,16 +1,50 @@
|
|
|
1
|
+
const fs = require("fs");
|
|
1
2
|
const { extractSchemas } = require('./index.js');
|
|
2
3
|
|
|
3
|
-
async function
|
|
4
|
-
const
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
4
|
+
async function main(options) {
|
|
5
|
+
const config = require(options.configFile);
|
|
6
|
+
const result = await extractSchemas(config.connection);
|
|
7
|
+
if(options.outputFile) {
|
|
8
|
+
fs.writeFileSync(options.outputFile, JSON.stringify(result,null,2) ,"utf8")
|
|
9
|
+
} else console.log(JSON.stringify(result,null,2));
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
let options = {
|
|
13
|
+
configFile:"",
|
|
14
|
+
outputFile:"",
|
|
15
|
+
}
|
|
10
16
|
|
|
11
|
-
|
|
17
|
+
if (process.argv.length === 2) {
|
|
18
|
+
console.error('Expected at least one argument!');
|
|
19
|
+
process.exit(1);
|
|
20
|
+
} else {
|
|
21
|
+
let argv = process.argv;
|
|
12
22
|
|
|
13
|
-
|
|
23
|
+
for(let i=2;i<argv.length;i++) {
|
|
24
|
+
if(argv[i].substring(0,2)==="--") {
|
|
25
|
+
let name = argv[i].substring(2);
|
|
26
|
+
if(options[name]!==undefined) {
|
|
27
|
+
options[name] = argv[i+1];
|
|
28
|
+
i++;
|
|
29
|
+
} else {
|
|
30
|
+
console.error('Expected a known option');
|
|
31
|
+
process.exit(1);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
14
35
|
}
|
|
15
36
|
|
|
16
|
-
|
|
37
|
+
(async () => {
|
|
38
|
+
try {
|
|
39
|
+
console.log('\x1b[32m%s\x1b[0m',"Running with options:\n",JSON.stringify(options,null,2));
|
|
40
|
+
main(options).then(()=>{
|
|
41
|
+
if(options.debug) console.log("DONE");
|
|
42
|
+
process.exit(0);
|
|
43
|
+
},()=>{
|
|
44
|
+
if(options.debug) console.log("ERROR");
|
|
45
|
+
process.exit(1);
|
|
46
|
+
})
|
|
47
|
+
} catch (e) {
|
|
48
|
+
process.exit(1);
|
|
49
|
+
}
|
|
50
|
+
})();
|