Download src/config/database.ts from simhaa-hat/ai-learning-dashboard-api: direct link, hf CLI and curl.
- Browser
- Download file 1.39 kB
-
https://huggingface.co/spaces/simhaa-hat/ai-learning-dashboard-api/resolve/main/src/config/database.ts
- Command line
-
hf download hf://spaces/simhaa-hat/ai-learning-dashboard-api/src/config/database.ts
-
curl -L -o database.ts https://huggingface.co/spaces/simhaa-hat/ai-learning-dashboard-api/resolve/main/src/config/database.ts
1.39 kB
| import mongoose from 'mongoose'; | |
| const connectDB = async (retries: number = 5, delay: number = 3000): Promise<void> => { | |
| const mongoURI = process.env.MONGODB_URI || 'mongodb://localhost:27017/ai-learning-dashboard'; | |
| for (let attempt = 1; attempt <= retries; attempt++) { | |
| try { | |
| const conn = await mongoose.connect(mongoURI); | |
| console.log(`✅ MongoDB Connected: ${conn.connection.host}`); | |
| // Handle connection events after initial connect | |
| mongoose.connection.on('error', (err) => { | |
| console.error('❌ MongoDB connection error:', err); | |
| }); | |
| mongoose.connection.on('disconnected', () => { | |
| console.warn('⚠️ MongoDB disconnected. Attempting reconnect...'); | |
| }); | |
| return; | |
| } catch (error) { | |
| console.error(`❌ MongoDB connection attempt ${attempt}/${retries} failed:`, error instanceof Error ? error.message : error); | |
| if (attempt < retries) { | |
| const waitTime = delay * Math.pow(2, attempt - 1); | |
| console.log(`⏳ Retrying in ${waitTime / 1000}s...`); | |
| await new Promise(resolve => setTimeout(resolve, waitTime)); | |
| } | |
| } | |
| } | |
| console.error('❌ All MongoDB connection attempts failed. Exiting...'); | |
| process.exit(1); | |
| }; | |
| export default connectDB; | |