-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjavascript_connectDB.js
More file actions
28 lines (22 loc) · 934 Bytes
/
Copy pathjavascript_connectDB.js
File metadata and controls
28 lines (22 loc) · 934 Bytes
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
export const javascript_connectDB = `// Import mongoose for MongoDB connection
import mongoose from "mongoose";
// Optional: specify a default DB name if not managed in the URI itself
const DB_NAME = "your-db-name";
// Asynchronous function to connect to MongoDB
const connectDB = async () => {
try {
// Build the full MongoDB URI with database name appended
// const mongoURI = \`\${process.env.MONGODB_URI}/\${DB_NAME}\`;
// Connect to MongoDB
// const connectionInstance = await mongoose.connect(mongoURI);
// console.log(\`✅ MongoDB connected at: \${connectionInstance.connection.host}\`);
} catch (error) {
// Handle initial connection errors
console.error("❌ MongoDB connection failed:", error);
// Exit process with failure
process.exit(1);
}
};
// Export the function to be used in other parts of the app
export default connectDB;
`