forked from Deen-Bridge/dnb-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.js
More file actions
113 lines (97 loc) · 3.44 KB
/
Copy pathdb.js
File metadata and controls
113 lines (97 loc) · 3.44 KB
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
import mongoose from "mongoose";
import logger from "./logger.js";
const SLOW_QUERY_MS = parseInt(process.env.SLOW_QUERY_MS || "200", 10);
mongoose.plugin(function slowQueryPlugin(schema) {
function logIfSlow(op) {
return function (result) {
const elapsed = Date.now() - (this._startTime || Date.now());
if (elapsed > SLOW_QUERY_MS) {
const filter = this.getQuery ? sanitizeFilter(this.getQuery()) : undefined;
logger.warn(
{ collection: schema.name, op, durationMs: elapsed, filter },
`Slow query: ${op} on ${schema.name} took ${elapsed}ms`
);
}
};
}
schema.pre("find", function () { this._startTime = Date.now(); });
schema.pre("findOne", function () { this._startTime = Date.now(); });
schema.pre("countDocuments", function () { this._startTime = Date.now(); });
schema.pre("updateOne", function () { this._startTime = Date.now(); });
schema.pre("deleteOne", function () { this._startTime = Date.now(); });
schema.post("find", logIfSlow("find"));
schema.post("findOne", logIfSlow("findOne"));
schema.post("countDocuments", logIfSlow("countDocuments"));
schema.post("updateOne", logIfSlow("updateOne"));
schema.post("deleteOne", logIfSlow("deleteOne"));
});
function sanitizeFilter(filter) {
if (!filter) return undefined;
const sanitized = { ...filter };
Object.keys(sanitized).forEach((key) => {
if (typeof sanitized[key] === "object" && sanitized[key] !== null) {
sanitized[key] = sanitizeFilter(sanitized[key]);
}
});
return sanitized;
}
const connectDB = async () => {
const maxRetries = 5;
let retryCount = 0;
const options = {
maxPoolSize: 10,
minPoolSize: 5,
serverSelectionTimeoutMS: 5000,
socketTimeoutMS: 45000,
family: 4,
retryWrites: true,
w: "majority",
};
const connectWithRetry = async () => {
try {
await mongoose.connect(process.env.MONGO_URI, options);
logger.info("MongoDB connected successfully");
logger.info(`Database: ${mongoose.connection.name}`);
logger.info(`Host: ${mongoose.connection.host}`);
mongoose.connection.on("error", (err) => {
logger.error(err, "MongoDB connection error");
});
mongoose.connection.on("disconnected", () => {
logger.warn("MongoDB disconnected. Attempting to reconnect...");
if (retryCount < maxRetries) {
connectWithRetry();
}
});
mongoose.connection.on("reconnected", () => {
logger.info("MongoDB reconnected successfully");
});
mongoose.connection.on("connected", () => {
logger.info("MongoDB connection established");
});
process.on("SIGINT", async () => {
try {
await mongoose.connection.close();
logger.info("MongoDB connection closed through app termination");
process.exit(0);
} catch (err) {
logger.error(err, "Error during MongoDB disconnection");
process.exit(1);
}
});
} catch (err) {
retryCount++;
logger.error(
`MongoDB connection error (Attempt ${retryCount}/${maxRetries}): ${err.message}`
);
if (retryCount < maxRetries) {
logger.info("Retrying connection in 5 seconds...");
setTimeout(connectWithRetry, 5000);
} else {
logger.error("Max retries reached. Could not connect to MongoDB.");
process.exit(1);
}
}
};
await connectWithRetry();
};
export default connectDB;