-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
218 lines (189 loc) · 7.29 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
const express = require('express');
const mysql = require('mysql');
const cors = require('cors');
const app = express();
const port = 5000;
/////////////////////////////////////////////////////////////////////////////////////
// MIDDLEWARE
/////////////////////////////////////////////////////////////////////////////////////
app.use(cors());
app.use(express.json());
/////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////
app.get('/', (req, res) => {
res.send('Hello World!');
});
/////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////
app.post('/Connect', (req, res) => {
const { host, username, password, database } = req.body;
console.log(host , username, password ,database);
const connection = mysql.createConnection({
host: host,
user: username,
password: password,
database: database
});
connection.connect((err) => {
if (err) {
res.status(500).send('Failed to connect to the database.');
return;
}
res.status(200).send('Connection successful');
});
connection.end();
});
/////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////
app.post('/Table', (req, res) => {
const { host, username, password, database } = req.body;
// Create a connection to the MySQL database
const connection = mysql.createConnection({
host: host,
user: username,
password: password,
database: database
});
// Connect to the MySQL database
connection.connect((err) => {
if (err) {
res.status(500).send('Failed to connect to the database.');
return;
}
// Retrieve list of databases
connection.query('SHOW DATABASES', (err, results) => {
if (err) {
res.status(500).send('Failed to retrieve databases from the server.');
return;
}
// List of databases to ignore
const ignoreDatabases = ['information_schema', 'mysql', 'performance_schema', 'phpmyadmin'];
// Filter out ignored databases
const filteredDatabases = results.filter((db) => !ignoreDatabases.includes(db.Database));
// Retrieve tables for each filtered database
const databaseTablesPromises = filteredDatabases.map((db) => {
return new Promise((resolve, reject) => {
connection.query(`SHOW TABLES IN ${db.Database}`, (err, tables) => {
if (err) {
reject(`Failed to retrieve tables for database ${db.Database}`);
return;
}
resolve({ database: db.Database, tables: tables.map((table) => table[`Tables_in_${db.Database}`]) });
});
});
});
// Wait for all database tables requests to complete
Promise.all(databaseTablesPromises)
.then((databaseTables) => {
// Send the filtered databases along with their tables as a response
res.status(200).send(databaseTables);
})
.catch((error) => {
res.status(500).send(error);
})
.finally(() => {
// Close the database connection
connection.end();
});
});
});
});
/////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////
app.post('/TableData', (req, res) => {
const { host, username, password, database, table } = req.body;
// Create a connection to the MySQL database
const connection = mysql.createConnection({
host: host,
user: username,
password: password,
database: database
});
// Connect to the MySQL database
connection.connect((err) => {
if (err) {
res.status(500).send('Failed to connect to the database.');
return;
}
// Retrieve data from the specified table
connection.query(`SELECT * FROM \`${table}\``, (err, results) => {
if (err) {
res.status(500).send(`Failed to retrieve data from table ${table}`);
return;
}
// Send the table data as a response
res.status(200).send(results);
});
});
});
/////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////
app.post('/Query', (req, res) => {
const { host, username, password, database, query } = req.body;
const connection = mysql.createConnection({
host: host,
user: username,
password: password,
database: database
});
connection.connect((err) => {
if (err) {
console.error('Failed to connect to the database:', err);
res.status(500).send('Failed to connect to the database.');
return;
}
// RUN QUERY
connection.query(query, (err, results, fields) => {
if (err) {
console.error('Failed to execute query:', err);
res.status(400).send(err.sqlMessage);
return;
}
const DATA = {
results,
fields
}
// Send the table data as a response with a status code of 200
res.status(200).send(DATA);
});
// Close the connection
connection.end((err) => {
if (err) {
res.status(400).send(err)
return;
}
});
});
});
/////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////
app.post('/Fields', (req, res) => {
const { host, username, password, database, table } = req.body;
const connection = mysql.createConnection({
host: host,
user: username,
password: password,
database: database
});
connection.connect((err) => {
if (err) {
res.status(500).send('Failed to connect to the database.');
return;
}
// RUN QUERY TO GET FIELDS AND THEIR DATA TYPES
const query = `SELECT COLUMN_NAME, DATA_TYPE FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = '${database}' AND TABLE_NAME = '${table}'`;
connection.query(query, (err, results, fields) => {
if (err) {
res.status(500).send(`Failed to retrieve fields from the table.`);
return;
}
// Send the field data as a response
res.status(200).send(results);
});
});
});
/////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////
app.listen(port, () => {
console.log(`listening on port ${port}`)
})