Update database.js
Browse files- database.js +90 -10
database.js
CHANGED
@@ -7,19 +7,99 @@ if (!dbUri) {
|
|
7 |
throw new Error("Missing DB_URI environment variable");
|
8 |
}
|
9 |
|
10 |
-
const dbName = "AkenoXJs";
|
11 |
const client = new MongoClient(dbUri);
|
12 |
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
-
|
20 |
-
|
21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
}
|
23 |
};
|
24 |
|
25 |
-
module.exports =
|
|
|
7 |
throw new Error("Missing DB_URI environment variable");
|
8 |
}
|
9 |
|
|
|
10 |
const client = new MongoClient(dbUri);
|
11 |
|
12 |
+
class Database {
|
13 |
+
constructor(dbname, collectionName) {
|
14 |
+
this.dbname = dbname;
|
15 |
+
this.collectionName = collectionName;
|
16 |
+
}
|
17 |
+
|
18 |
+
async connect() {
|
19 |
+
try {
|
20 |
+
if (!client.isConnected) {
|
21 |
+
await client.connect();
|
22 |
+
console.log("Connected to MongoDB");
|
23 |
+
}
|
24 |
+
} catch (error) {
|
25 |
+
console.error("Error connecting to MongoDB:", error.message);
|
26 |
+
}
|
27 |
+
}
|
28 |
+
|
29 |
+
collection() {
|
30 |
+
try {
|
31 |
+
const db = client.db(this.dbname);
|
32 |
+
return db.collection(this.collectionName);
|
33 |
+
} catch (error) {
|
34 |
+
console.error("Error accessing collection:", error.message);
|
35 |
+
}
|
36 |
+
}
|
37 |
+
|
38 |
+
async close() {
|
39 |
+
try {
|
40 |
+
await client.close();
|
41 |
+
console.log("MongoDB connection closed");
|
42 |
+
} catch (error) {
|
43 |
+
console.error("Error closing MongoDB connection:", error.message);
|
44 |
+
}
|
45 |
+
}
|
46 |
+
|
47 |
+
async IPAddressAndUpdate(ip) {
|
48 |
+
try {
|
49 |
+
const collection = this.collection(); // Access collection
|
50 |
+
const filter = { ip: ip }; // Filter to find the IP
|
51 |
+
const update = { $set: { ip: ip } }; // Update IP field
|
52 |
+
|
53 |
+
const result = await collection.updateOne(filter, update, { upsert: true });
|
54 |
+
|
55 |
+
if (result.upsertedCount > 0) {
|
56 |
+
console.log("Inserted a new IP address:", ip);
|
57 |
+
} else {
|
58 |
+
console.log("Updated an existing IP address:", ip);
|
59 |
+
}
|
60 |
+
} catch (error) {
|
61 |
+
console.error("Error updating IP address:", error.message);
|
62 |
+
}
|
63 |
+
}
|
64 |
+
|
65 |
+
async AddIpisBlocked(ip) {
|
66 |
+
try {
|
67 |
+
const collection = this.collection();
|
68 |
+
const filter = { ip: ip };
|
69 |
+
const update = { $set: { blocked: true } };
|
70 |
+
|
71 |
+
const result = await collection.updateOne(filter, update, { upsert: true });
|
72 |
+
|
73 |
+
if (result.upsertedCount > 0) {
|
74 |
+
console.log("Inserted a new IP address:", ip);
|
75 |
+
} else {
|
76 |
+
console.log("Updated an existing IP address:", ip);
|
77 |
+
}
|
78 |
+
} catch (error) {
|
79 |
+
console.error("Error updating IP address:", error.message);
|
80 |
+
}
|
81 |
+
}
|
82 |
+
|
83 |
+
async CheckIsBlocked(ip) {
|
84 |
+
try {
|
85 |
+
const collection = this.collection();
|
86 |
+
const filter = { ip: ip };
|
87 |
+
const update = { $set: { blocked: true } };
|
88 |
+
|
89 |
+
const FindIp = await collection.findOne(filter);
|
90 |
|
91 |
+
if (FindIp) {
|
92 |
+
console.log("IP found in the database:", FindIp);
|
93 |
+
return FindIp;
|
94 |
+
} else {
|
95 |
+
console.log("IP not found in the database");
|
96 |
+
return null
|
97 |
+
}
|
98 |
+
} catch (error) {
|
99 |
+
console.error("Error checking IP:", error.message);
|
100 |
+
return null
|
101 |
+
}
|
102 |
}
|
103 |
};
|
104 |
|
105 |
+
module.exports = Database;
|