extract-mysql-schema 0.0.6 → 0.0.8
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/index.js +29 -2
- package/package.json +1 -1
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
|
+
};
|