extract-mysql-schema 0.8.0 → 1.0.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/index.js +124 -53
- package/package.json +2 -2
package/index.js
CHANGED
|
@@ -29,59 +29,130 @@ const extractSchemas = async function (connection, options) {
|
|
|
29
29
|
const schemaName = connection.database;
|
|
30
30
|
options = options || {};
|
|
31
31
|
|
|
32
|
+
|
|
32
33
|
let c = await mysql.createConnection(connection);
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
let
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
34
|
+
|
|
35
|
+
let queryProcedures = [];
|
|
36
|
+
let queryParameters = [];
|
|
37
|
+
let queryColumns = [];
|
|
38
|
+
let queryFkey = [];
|
|
39
|
+
let queryIndexes = [];
|
|
40
|
+
let [tablenames] = await c.query('SHOW TABLES');
|
|
41
|
+
let [proceduresResult] = await c.query(`SHOW PROCEDURE STATUS`);
|
|
42
|
+
|
|
43
|
+
const tableKey = Object.keys(tablenames[0])[0];
|
|
44
|
+
|
|
45
|
+
for (const row of tablenames) {
|
|
46
|
+
const tableName = row[tableKey];
|
|
47
|
+
const [columnsResult] = await c.query( `SHOW COLUMNS FROM \`${tableName}\`` );
|
|
48
|
+
const [indexResult] = await c.query( `SHOW INDEX FROM \`${tableName}\`` );
|
|
49
|
+
const [keysResult] = await c.query( `SHOW KEYS FROM \`${tableName}\`` );
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
for(let col in columnsResult) {
|
|
53
|
+
let column = {
|
|
54
|
+
"TABLE_TYPE": "BASE TABLE",
|
|
55
|
+
"TABLE_CATALOG": "def",
|
|
56
|
+
"TABLE_SCHEMA": schemaName,
|
|
57
|
+
"TABLE_NAME": tableName,
|
|
58
|
+
"COLUMN_NAME": columnsResult[col]['Field'],
|
|
59
|
+
"ORDINAL_POSITION": col,
|
|
60
|
+
"COLUMN_KEY": columnsResult[col]['Key'],
|
|
61
|
+
"EXTRA": columnsResult[col]['Extra'],
|
|
62
|
+
"IS_NULLABLE": columnsResult[col]['Null']==='YES' ? 'YES' : 'NO',
|
|
63
|
+
"COLUMN_DEFAULT": columnsResult[col]['Default'],
|
|
64
|
+
"COLUMN_TYPE": columnsResult[col]['Type'],
|
|
65
|
+
"DATA_TYPE": columnsResult[col]['Type'].split('(')[0],
|
|
66
|
+
"GENERATION_EXPRESSION": columnsResult[col]['Extra'].indexOf('GENERATED')>=0 ? columnsResult[col]['Default'] || '' : '',
|
|
67
|
+
"CHARACTER_MAXIMUM_LENGTH": columnsResult[col]['Type'].split('(')[1] ? parseInt(columnsResult[col]['Type'].split('(')[1]) : 0,
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
for(let idx in indexResult) {
|
|
71
|
+
if(indexResult[idx]['Column_name']===column['COLUMN_NAME']) {
|
|
72
|
+
column['IS_COMPOUND_KEY'] = indexResult[idx]['Key_name'] !== 'PRIMARY' && indexResult[idx]['Seq_in_index'] > 1 ? 'YES' : 'NO';
|
|
73
|
+
column["IS_PRIMARY_KEY"] = indexResult[idx]['Key_name'] === 'PRIMARY' ? 'YES' : 'NO';
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
queryColumns.push(column)
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
for(let key in keysResult) {
|
|
80
|
+
if(keysResult[key]['Key_name']!=='PRIMARY' && keysResult[key]['Referenced_table_name']) {
|
|
81
|
+
let fkey = {
|
|
82
|
+
"ID": schemaName + '/' + keysResult[key]['Key_name'],
|
|
83
|
+
"FOR_NAME": schemaName + '/' + tableName,
|
|
84
|
+
"FOR_COL_NAME": keysResult[key]['Column_name'],
|
|
85
|
+
"REF_NAME": schemaName + '/' + keysResult[key]['Referenced_table_name'],
|
|
86
|
+
"REF_COL_NAME": keysResult[key]['Referenced_column_name']
|
|
87
|
+
};
|
|
88
|
+
queryFkey.push(fkey);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
queryIndexes=[]
|
|
93
|
+
for(let idx in indexResult) {
|
|
94
|
+
let isUnique = indexResult[idx]['Non_unique'] === 0 ? 'YES' : 'NO';
|
|
95
|
+
let isPrimary = indexResult[idx]['Key_name'] === 'PRIMARY' ? 'YES' : 'NO';
|
|
96
|
+
let isFK = 'NO';
|
|
97
|
+
for(let key in keysResult) {
|
|
98
|
+
if(keysResult[key]['Key_name']===indexResult[idx]['Key_name'] && keysResult[key]['Referenced_table_name']) {
|
|
99
|
+
isFK = 'YES';
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
queryIndexes.push({
|
|
103
|
+
"TABLE_SCHEMA": schemaName,
|
|
104
|
+
"TABLE_NAME": tableName,
|
|
105
|
+
"INDEX_NAME": indexResult[idx]['Key_name'],
|
|
106
|
+
"FIELD_TYPE": indexResult[idx]['Index_type'],
|
|
107
|
+
"POS": indexResult[idx]['Seq_in_index'] - 1,
|
|
108
|
+
"TYPE": indexResult[idx]['Index_type'],
|
|
109
|
+
"IS_UNIQUE": isUnique,
|
|
110
|
+
"IS_PRIMARY": isPrimary,
|
|
111
|
+
"IS_FK": isFK,
|
|
112
|
+
"IS_AUTONUMBER": 'NO',
|
|
113
|
+
"FIELD_NAME": indexResult[idx]['Column_name']
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
for(let proc of proceduresResult) {
|
|
119
|
+
if(proc['Db']!==schemaName) continue;
|
|
120
|
+
const [procDefResult] = await c.query( `SHOW CREATE PROCEDURE \`${proc['Name']}\`` );
|
|
121
|
+
let procedure = {
|
|
122
|
+
"SPECIFIC_NAME": proc['Name'],
|
|
123
|
+
"ROUTINE_DEFINITION": procDefResult[0]['Create Procedure']
|
|
124
|
+
};
|
|
125
|
+
queryProcedures.push(procedure);
|
|
126
|
+
|
|
127
|
+
const match = procDefResult[0]['Create Procedure'].match(/\(([\s\S]*?)\)/);
|
|
128
|
+
if (!match) { console.log('No parameters found'); return []; }
|
|
129
|
+
const paramString = match[1].trim();
|
|
130
|
+
const params = paramString .split(',') .map(p => p.trim()) .filter(p => p.length > 0);
|
|
131
|
+
|
|
132
|
+
for (let i = 0; i < params.length; i++) {
|
|
133
|
+
const paramParts = params[i].split(/\s+/);
|
|
134
|
+
let paramMode = 'IN';
|
|
135
|
+
let paramName, paramType;
|
|
136
|
+
if (paramParts.length === 2) {
|
|
137
|
+
paramName = paramParts[0];
|
|
138
|
+
paramType = paramParts[1];
|
|
139
|
+
} else if (paramParts.length === 3) {
|
|
140
|
+
paramMode = paramParts[0];
|
|
141
|
+
paramName = paramParts[1];
|
|
142
|
+
paramType = paramParts[2];
|
|
143
|
+
}
|
|
144
|
+
let parameter = {
|
|
145
|
+
"SPECIFIC_NAME": proc['Name'],
|
|
146
|
+
"PARAMETER_NAME": paramName,
|
|
147
|
+
"ORDINAL_POSITION": i + 1,
|
|
148
|
+
"PARAMETER_MODE": paramMode,
|
|
149
|
+
"DATA_TYPE": paramType.split('(')[0],
|
|
150
|
+
"DTD_IDENTIFIER": paramType,
|
|
151
|
+
"CHARACTER_MAXIMUM_LENGTH": paramType.split('(')[1] ? parseInt(paramType.split('(')[1]) : 0,
|
|
152
|
+
};
|
|
153
|
+
queryParameters.push(parameter);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
85
156
|
|
|
86
157
|
c.end();
|
|
87
158
|
|
|
@@ -181,7 +252,7 @@ const extractSchemas = async function (connection, options) {
|
|
|
181
252
|
else def=`('${def}')`;
|
|
182
253
|
}
|
|
183
254
|
}
|
|
184
|
-
if(wrapper.partition
|
|
255
|
+
if(wrapper.partition!==undefined && queryColumns[i]['PARTITION_METHOD']!==null) wrapper.partition=`PARTITION BY ${queryColumns[i]['PARTITION_METHOD']}(${queryColumns[i]['PARTITION_EXPRESSION']})`;
|
|
185
256
|
if(def) def=`DEFAULT ${def}`;
|
|
186
257
|
if(column.generated==="STORED") def = `${column.expression} STORED`;
|
|
187
258
|
let notNull = column.isNullable?"NULL":"NOT NULL";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "extract-mysql-schema",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "1.0.0",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"bin": "./run.js",
|
|
@@ -18,6 +18,6 @@
|
|
|
18
18
|
"author": "Chris Doty",
|
|
19
19
|
"license": "MIT",
|
|
20
20
|
"dependencies": {
|
|
21
|
-
"mysql2": "^3.
|
|
21
|
+
"mysql2": "^3.16.0"
|
|
22
22
|
}
|
|
23
23
|
}
|