-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathdatabase.js
65 lines (52 loc) · 1.55 KB
/
database.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
import mongoose from "mongoose";
const {
MONGO_DBNAME = "",
MONGO_HOSTS = "",
MONGO_USERNAME = "",
MONGO_PASSWORD = "",
MONGO_REPLICASET,
MONGO_READ_PREFERENCE,
MONGO_PEM_PATH = "",
MONGO_SERVER_IDENTITY_CHECK = "true",
} = process.env;
const REQUIRED_CONFIG = [
"MONGO_DBNAME",
"MONGO_HOSTS",
"MONGO_USERNAME",
"MONGO_PASSWORD",
];
// REQUIRED_CONFIG.forEach((key) => {
// if (!process.env[key]) {
// console.error("[Error] Missing MongoDB Config:", key);
// return process.exit(1);
// }
// });
const CONFIG = {};
const CONNECTION_URI = `mongodb+srv://${MONGO_USERNAME}:${MONGO_PASSWORD}@${MONGO_HOSTS}/${MONGO_DBNAME}`;
//@cluster0.8ysl0ky.mongodb.net
const mongoConnect = async () => {
try {
await mongoose.connect(CONNECTION_URI, CONFIG.OPTIONS);
mongoose.connection.on("connected", () => {
console.log("[Connection] Successfully connected to MongoDB!");
});
mongoose.connection.on("error", (err) => {
console.error("[Connection] Error connecting to MongoDB:", err);
});
mongoose.connection.on("disconnected", () => {
console.log("[Connection] MongoDB connection disconnected.");
});
} catch (error) {
console.error("[Connection] MongoDB connection failed:", error);
process.exit(1);
}
};
mongoConnect()
.then(() => {
// console.log("[Info] Server Started Successfully! Listening on Port: 8080");
})
.catch((err) => {
console.error("[Error] Failed to start the server:", err);
});
const MONGO_CONFIG = { CONFIG, mongoConnect };
module.exports = MONGO_CONFIG;