Spaces:
Building
Building
// src/database.js | |
const mongoose = require('mongoose'); | |
const logger = require('./logger'); | |
const config = require('./config'); | |
if (!config.mongodbUri) { | |
logger.fatal("FATAL: MONGODB_URI not found in environment variables/config. Check .env file."); | |
process.exit(1); | |
} | |
// Schema remains the same | |
const userSchema = new mongoose.Schema({ | |
remoteJid: { type: String, required: true, unique: true }, | |
name: { type: String }, | |
regNo: { type: String }, | |
semester: { type: String }, | |
isRegistered: { type: Boolean, default: false }, | |
registrationTimestamp: { type: Date } | |
}); | |
// Comment on Data Size | |
// Data per user is small (~<1KB). 2000 users (~2MB) is well within Atlas free tier limits. | |
const User = mongoose.model('User', userSchema); | |
async function connectDB() { | |
try { | |
await mongoose.connect(config.mongodbUri); | |
logger.info('Successfully connected to MongoDB!'); | |
} catch (error) { | |
logger.fatal({ err: error }, 'Error connecting to MongoDB'); | |
process.exit(1); | |
} | |
} | |
// Export model directly for handlers to import | |
module.exports = { connectDB, User }; | |