-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
119 lines (102 loc) · 4.96 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
// Import necessary packages and modules
const express = require("express"); // Express framework for creating the server
const home = require("./routes/home"); // Custom route module
const { Database } = require("arangojs"); // ArangoDB driver for interacting with the database
const port = 3001; // Port number for the server to listen on
// Set up middlewares and create an Express application
const app = express();
app.use(express.json()); // Middleware to parse incoming JSON requests
// Set up ArangoDB connection
const db = new Database({
url: "https://92ac4680fb78.arangodb.cloud:18529", // ArangoDB server URL
auth: { username: "root", password: "yMfTVSAyJhqzjII2jtwB" }, // Authentication credentials
agentOptions: {
ca: Buffer.from(
"LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSURHRENDQWdDZ0F3SUJBZ0lRWGxzQzZwU0laUlRBbmpGak1XM2R6ekFOQmdrcWhraUc5dzBCQVFzRkFEQW0KTVJFd0R3WURWUVFLRXdoQmNtRnVaMjlFUWpFUk1BOEdBMVVFQXhNSVFYSmhibWR2UkVJd0hoY05Nak13TmpFMApNVEExTURJMFdoY05Namd3TmpFeU1UQTFNREkwV2pBbU1SRXdEd1lEVlFRS0V3aEJjbUZ1WjI5RVFqRVJNQThHCkExVUVBeE1JUVhKaGJtZHZSRUl3Z2dFaU1BMEdDU3FHU0liM0RRRUJBUVVBQTRJQkR3QXdnZ0VLQW9JQkFRQ3oKMDgybGd4ZUpqcGNFbVU1WnltMmZpWS9DaWU1YkRLUG83TTlXbk5KOGxXTnlXR1NPQUFhRmlHMU1wRytTTWw2bg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});
// Test the connection with ArangoDB server
db.version()
.then((version) => console.log("Connected to ArangoDB version:", version))
.catch((error) => console.error("Failed to connect to ArangoDB:", error));
// Define routes and their respective handlers
app.get("/get", async (req, res) => {
try {
// Retrieve data from ArangoDB
const collection = await db.collection("firstCollection");
const cursor = await collection.all();
// Extract necessary information from the documents
const documents = [];
await cursor.forEach((document) => {
documents.push({
key: document._key,
value: document.value,
});
});
res.send(`${JSON.stringify(documents)}`);
} catch (error) {
console.error("Error:", error);
res.status(500).send("Internal Server Error");
}
});
app.get("/create", async (req, res) => {
try {
// Create a new document in ArangoDB collection
const collection = await db.collection("firstCollection");
const document = {
ProcessStep: "Step 5",
MeasuredValue: "22",
};
await collection.save(document);
res.send("Document created successfully!");
} catch (error) {
console.error("Error:", error);
res.status(500).send("Internal Server Error");
}
});
app.get("/update", async (req, res) => {
try {
// Update an existing document in ArangoDB collection
const collection = await db.collection("firstCollection");
// 8012470 is the key from ArangoDB.
await collection.update("8012470", { ProcessStep: "Step 10" });
res.send("Document updated successfully!");
} catch (error) {
console.error("Error:", error);
res.status(500).send("Internal Server Error");
}
});
app.get("/delete", async (req, res) => {
try {
// Delete a document from ArangoDB collection
const collection = await db.collection("firstCollection");
await collection.remove("firstDocument");
res.send("Document deleted successfully");
} catch (error) {
res.status(500).send("Internal Server Error");
}
});
app.get("/documents", async (req, res) => {
try {
// Retrieve all documents from ArangoDB collection
const collection = db.collection("firstCollection");
const cursor = await collection.all();
// Extract necessary information from the documents
const documents = [];
await cursor.forEach((document) => {
documents.push({
document,
});
});
res.send(`${JSON.stringify(documents)}`);
} catch (error) {
console.error("Error:", error);
res.status(500).send("Internal Server Error");
}
});
// Start the server and listen on specified port
app.listen(port, () => {
console.log(`Server listening at http://localhost:${port}`);
});