File size: 1,128 Bytes
14960b8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// 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 };