-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnect2.js
43 lines (37 loc) · 989 Bytes
/
connect2.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
const mysql = require('mysql2/promise');
const fs = require('fs/promises');
async function getNodes() {
const pool = await mysql.createPool({
host: 'localhost',
user: 'root',
password: '',
database: 'alaa',
waitForConnections: true,
});
try {
const [rows, fields] = await pool.execute('SELECT * FROM nodes');
return rows;
} catch (err) {
console.error(err);
} finally {
await pool.end();
}
}
async function checkAndUpdateJson() {
try {
const rows = await getNodes();
const jsonData = JSON.stringify(rows, null, 2);
// Check if the JSON data has changed
const currentData = await fs.readFile('nodes.json', 'utf8');
if (currentData !== jsonData) {
// Write the new data to the file
await fs.writeFile('nodes.json', jsonData);
console.log('Updated nodes.json');
} else {
console.log('nodes.json is up-to-date');
}
} catch (err) {
console.error(err);
}
}
checkAndUpdateJson();