-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
447 lines (399 loc) · 15.7 KB
/
index.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
const express = require("express");
const mysql = require("mysql2");
const bodyParser = require("body-parser");
const cors = require("cors");
const { v4: uuidv4 } = require('uuid');
require("dotenv").config();
const app = express();
const corsOptions = {
origin: true, // Allow all origins in development
methods: ['GET', 'POST', 'DELETE', 'PUT', 'PATCH', 'OPTIONS'],
allowedHeaders: [
'Content-Type',
'Authorization',
'X-Requested-With',
'Accept',
'Origin',
'Access-Control-Allow-Origin',
'Access-Control-Allow-Headers'
],
credentials: true,
preflightContinue: false,
optionsSuccessStatus: 200
};
app.use(cors(corsOptions));
app.use((req, res, next) => {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Accept, Origin, X-Requested-With');
if (req.method === 'OPTIONS') {
return res.sendStatus(200);
}
next();
});
app.use(bodyParser.json());
app.use((req, res, next) => {
console.log('\n=== REQUEST LOG ===');
console.log(`🚀 ${req.method} ${req.url}`);
console.log('📋 Headers:', JSON.stringify(req.headers, null, 2));
console.log('📦 Body:', JSON.stringify(req.body, null, 2));
console.log('==================\n');
next();
});
// MySQL Connection
let db;
const connectToDatabase = () => {
db = mysql.createConnection({
host: process.env.DB_HOST || 'localhost', // Make sure this is correct
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME,
port: process.env.DB_PORT || 3306,
connectTimeout: 10000,
ssl: {
rejectUnauthorized: false // Add this for Railway.app MySQL connections
}
});
db.connect((err) => {
if (err) {
console.error("❌ Database connection failed:", err.message);
setTimeout(connectToDatabase, 2000); // Retry connection after 2 seconds
} else {
console.log("✅ Connected to MySQL database.");
}
});
db.on('error', (err) => {
console.error('Database error:', err);
if (err.code === 'PROTOCOL_CONNECTION_LOST') {
console.log('Attempting to reconnect to database...');
connectToDatabase();
}
});
};
connectToDatabase();
const queryAsync = (query, values) => {
return new Promise((resolve, reject) => {
db.query(query, values, (err, results) => {
if (err) return reject(err);
resolve(results);
});
});
};
// Ensure the database connection is open before executing queries
const ensureConnection = async () => {
if (!db || db.state === 'disconnected') {
connectToDatabase();
await new Promise(resolve => setTimeout(resolve, 2000)); // Wait for 2 seconds to reconnect
}
};
// Initialize MySQL Database
app.post("/api/init-db", async (req, res) => {
try {
await ensureConnection();
// Add response headers
res.setHeader('Content-Type', 'application/json');
res.setHeader('Access-Control-Allow-Origin', '*');
// Test the connection first
await queryAsync('SELECT 1');
await queryAsync(`
CREATE TABLE IF NOT EXISTS nodes (
id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255) NOT NULL,
description TEXT,
images TEXT,
audioFiles TEXT,
documents TEXT,
videoLinks TEXT,
coordinates VARCHAR(255),
type VARCHAR(50) NOT NULL,
parent_id INT,
position VARCHAR(255),
superNodeId INT,
FOREIGN KEY (parent_id) REFERENCES nodes(id)
);
`);
await queryAsync(`
CREATE TABLE IF NOT EXISTS connections (
source_id INT,
target_id INT,
PRIMARY KEY (source_id, target_id),
FOREIGN KEY (source_id) REFERENCES nodes(id),
FOREIGN KEY (target_id) REFERENCES nodes(id)
);
`);
await queryAsync(`
CREATE TABLE IF NOT EXISTS node_graphs (
node_id INT,
graph_id VARCHAR(255),
PRIMARY KEY (node_id, graph_id),
FOREIGN KEY (node_id) REFERENCES nodes(id)
);
`);
res.status(200).json({ message: "Database initialized successfully" });
} catch (err) {
console.error("Failed to initialize database:", err.message);
res.status(500).json({ error: `Failed to initialize database: ${err.message}` });
}
});
// Drop All Tables
app.post("/api/drop-tables", async (req, res) => {
try {
await ensureConnection();
// Add response headers
res.setHeader('Content-Type', 'application/json');
res.setHeader('Access-Control-Allow-Origin', '*');
// Drop the tables
await queryAsync('DROP TABLE IF EXISTS connections');
await queryAsync('DROP TABLE IF EXISTS node_graphs');
await queryAsync('DROP TABLE IF EXISTS nodes');
res.status(200).json({ message: "All tables dropped successfully" });
} catch (err) {
console.error("Failed to drop tables:", err.message);
res.status(500).json({ error: `Failed to drop tables: ${err.message}` });
}
});
// Save Node
app.post("/api/save-node", async (req, res) => {
try {
await ensureConnection();
console.log('Received save node request:', req.body);
let {
id,
title,
description,
images,
audioFiles,
documents,
videoLinks,
coordinates,
type,
parent_id,
position,
connections,
graph_id,
superNodeId
} = req.body;
if (!title) {
return res.status(400).json({ error: "Missing required field (title)" });
}
// Ensure position is correctly formatted as a JSON string
if (typeof position === 'object') {
position = JSON.stringify(position);
}
// Filter out null values from connections
connections = connections ? connections.filter(conn => conn !== null) : [];
// First ensure the node exists or create it
const query = `
INSERT INTO nodes (id, title, description, images, audioFiles, documents, videoLinks, coordinates, type, parent_id, position, superNodeId)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
ON DUPLICATE KEY UPDATE
title = VALUES(title),
description = VALUES(description),
images = VALUES(images),
audioFiles = VALUES(audioFiles),
documents = VALUES(documents),
videoLinks = VALUES(videoLinks),
coordinates = VALUES(coordinates),
type = VALUES(type),
parent_id = VALUES(parent_id),
position = VALUES(position),
superNodeId = VALUES(superNodeId)
`;
const values = [
id || null,
title,
description || null,
images ? JSON.stringify(images) : '[]',
audioFiles ? JSON.stringify(audioFiles) : '[]',
documents ? JSON.stringify(documents) : '[]',
videoLinks ? JSON.stringify(videoLinks) : '[]',
coordinates ? JSON.stringify(coordinates) : null,
type || 'normal',
parent_id || null,
position || JSON.stringify({"dx":0,"dy":0}),
superNodeId || null
];
const result = await queryAsync(query, values);
if (!id) {
id = result.insertId; // Get the auto-incremented ID
}
// Handle connections if they exist
if (connections && Array.isArray(connections)) {
await queryAsync("DELETE FROM connections WHERE source_id = ?", [id]);
if (connections.length > 0) {
const connectionValues = connections.map(targetId => [id, targetId]);
await queryAsync(
"INSERT INTO connections (source_id, target_id) VALUES ?",
[connectionValues]
);
}
}
// Handle graph association
if (graph_id) {
await queryAsync(`
INSERT INTO node_graphs (node_id, graph_id)
VALUES (?, ?)
ON DUPLICATE KEY UPDATE graph_id = VALUES(graph_id)
`, [id, graph_id]);
}
res.status(200).json({
message: "Node saved successfully",
nodeId: id
});
} catch (err) {
console.error("Failed to save node:", err);
res.status(500).json({
error: "Failed to save node",
details: err.message
});
}
});
app.get("/api/load-nodes", async (req, res) => {
try {
await ensureConnection();
const query = `
SELECT nodes.*,
GROUP_CONCAT(DISTINCT connections.target_id) AS connections,
GROUP_CONCAT(DISTINCT node_graphs.graph_id) AS graphs
FROM nodes
LEFT JOIN connections ON nodes.id = connections.source_id
LEFT JOIN node_graphs ON nodes.id = node_graphs.node_id
GROUP BY nodes.id
`;
const results = await queryAsync(query);
const formattedResults = results.map(row => ({
id: row.id,
title: row.title,
description: row.description,
images: row.images ? JSON.parse(row.images) : [],
audioFiles: row.audioFiles ? JSON.parse(row.audioFiles) : [],
documents: row.documents ? JSON.parse(row.documents) : [],
videoLinks: row.videoLinks ? JSON.parse(row.videoLinks) : [],
coordinates: row.coordinates ? JSON.parse(row.coordinates) : null,
type: row.type,
parent_id: row.parent_id,
position: row.position ? JSON.parse(row.position) : {"dx":0,"dy":0},
connections: row.connections ? row.connections.split(',').filter(Boolean) : [],
graphs: row.graphs ? row.graphs.split(',').filter(Boolean) : [],
superNodeId: row.superNodeId
}));
res.json(formattedResults);
} catch (err) {
console.error("Failed to load nodes:", err.message);
res.status(500).send("Failed to load nodes");
}
});
// Delete Node
app.delete("/api/delete-node/:id", async (req, res) => {
const { id } = req.params;
try {
await ensureConnection();
if (!id) {
return res.status(400).json({ error: "Node ID is required" });
}
console.log(`Deleting node with ID: ${id}`); // Log the node ID being deleted
// Ensure the node exists before attempting to delete
const nodeExists = await queryAsync("SELECT id FROM nodes WHERE id = ?", [id]);
if (nodeExists.length === 0) {
console.log(`Node with ID ${id} not found`); // Log if the node is not found
return res.status(404).json({ error: "Node not found" });
}
// Delete connections and node_graphs associated with the node
await queryAsync("DELETE FROM connections WHERE source_id = ? OR target_id = ?", [id, id]);
await queryAsync("DELETE FROM node_graphs WHERE node_id = ?", [id]);
const deleteNode = await queryAsync("DELETE FROM nodes WHERE id = ?", [id]);
console.log(`Delete node result:`, deleteNode); // Log the result of the delete query
res.setHeader('Content-Type', 'application/json');
res.status(200).json({ message: "Node deleted successfully" });
} catch (err) {
console.error("Failed to delete node:", err.message);
res.status(500).json({ error: "Failed to delete node" });
}
});
// Load Nodes
app.get("/api/load-nodes", async (req, res) => {
try {
await ensureConnection();
const query = `
SELECT nodes.*,
GROUP_CONCAT(DISTINCT connections.target_id) AS connections,
GROUP_CONCAT(DISTINCT node_graphs.graph_id) AS graphs
FROM nodes
LEFT JOIN connections ON nodes.id = connections.source_id
LEFT JOIN node_graphs ON nodes.id = node_graphs.node_id
GROUP BY nodes.id
`;
const results = await queryAsync(query);
const formattedResults = results.map(row => ({
id: row.id,
title: row.title,
description: row.description,
images: row.images ? JSON.parse(row.images) : [],
audioFiles: row.audioFiles ? JSON.parse(row.audioFiles) : [],
documents: row.documents ? JSON.parse(row.documents) : [],
videoLinks: row.videoLinks ? JSON.parse(row.videoLinks) : [],
coordinates: row.coordinates ? JSON.parse(row.coordinates) : null,
type: row.type,
parent_id: row.parent_id,
position: row.position ? JSON.parse(row.position) : {"dx":0,"dy":0},
connections: row.connections ? row.connections.split(',').filter(Boolean) : [],
graphs: row.graphs ? row.graphs.split(',').filter(Boolean) : [],
superNodeId: row.superNodeId
}));
res.json(formattedResults);
} catch (err) {
console.error("Failed to load nodes:", err.message);
res.status(500).send("Failed to load nodes");
}
});
// Fetch Node by ID
app.get("/api/node/:id", async (req, res) => {
const { id } = req.params;
try {
await ensureConnection();
const query = `
SELECT nodes.*,
GROUP_CONCAT(DISTINCT connections.target_id) AS connections,
GROUP_CONCAT(DISTINCT node_graphs.graph_id) AS graphs
FROM nodes
LEFT JOIN connections ON nodes.id = connections.source_id
LEFT JOIN node_graphs ON nodes.id = node_graphs.node_id
WHERE nodes.id = ?
GROUP BY nodes.id
`;
const results = await queryAsync(query, [id]);
if (results.length === 0) {
return res.status(404).json({ error: "Node not found" });
}
const node = results[0];
res.json({
id: node.id,
title: node.title,
description: node.description,
images: node.images ? JSON.parse(node.images) : [],
audioFiles: node.audioFiles ? JSON.parse(node.audioFiles) : [],
documents: node.documents ? JSON.parse(node.documents) : [],
videoLinks: node.videoLinks ? JSON.parse(node.videoLinks) : [],
coordinates: node.coordinates ? JSON.parse(node.coordinates) : null,
type: node.type,
parent_id: node.parent_id,
position: node.position ? JSON.parse(node.position) : {"dx":0,"dy":0},
connections: node.connections ? node.connections.split(',').filter(Boolean) : [],
graphs: node.graphs ? node.graphs.split(',').filter(Boolean) : [],
superNodeId: node.superNodeId
});
} catch (err) {
console.error("Failed to fetch node:", err.message);
res.status(500).json({ error: "Failed to fetch node" });
}
});
// Add error handling middleware
app.use((err, req, res, next) => {
console.error('Server error:', err);
res.status(err.status || 500).json({
error: err.message || 'Internal server error'
});
});
// Start Server
const PORT = process.env.SERVER_PORT || 3000;
app.listen(PORT, () => console.log(`🚀 Server running on port ${PORT}`));