-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.ts
More file actions
40 lines (30 loc) · 1.06 KB
/
db.ts
File metadata and controls
40 lines (30 loc) · 1.06 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
import "dotenv/config";
import mongoose from "mongoose";
const connectDB = async (): Promise<void> => {
try {
if (!process.env.MONGO_DB_URI) {
throw new Error("MONGO_DB_URI is not defined in environment variables");
}
await mongoose.connect(process.env.MONGO_DB_URI, {
// socketTimeoutMS: 45000,
// serverSelectionTimeoutMS: 5000
// useNewUrlParser: true,
// useUnifiedTopology: true,
});
console.log('Connected to database successfully');
mongoose.connection.on('error', (error) => {
console.error('MongoDB connection error:', error);
});
mongoose.connection.on('disconnected', () => {
console.log('MongoDB disconnected');
});
process.on('SIGINT', async () => {
await mongoose.connection.close();
process.exit(0);
});
} catch (error) {
console.error('Database connection error:', error);
process.exit(1);
}
};
export default connectDB;