Overview

The project’s persistent storage utilizes MongoDB database, and we use MongoDB Atlas as the cloud database service for persistent storage, implementing a robust document-based storage system for the chat application. The database layer is structured using three main components:

Database Architecture

Connection Management

The DatabaseManager class implements a singleton pattern to maintain a single database connection throughout the application lifecycle. It handles:

mongodb+srv://<username>:<password>@<host>/?retryWrites=true&w=majority

Collections

Users Collection

The UsersCollection stores user account information with the following schema:

{
    "_id": "ObjectId",          // Unique identifier for each user
    "username": "string",       // Username (not unique)
    "email": "string",          // Unique email address
    "password_hash": "string",  // Hashed password
    "created_at": "Date",       // Account creation timestamp
    "last_login": "Date",       // Last login timestamp
}

Key operations:

Messages Collection

The MessagesCollectionstores messages sent between users with the following schema:

{
    "_id": "ObjectId",          // Unique identifier for each message
    "sender_id": "ObjectId",    // Reference to Users._id
    "recipient_id": "ObjectId", // Reference to Users._id
    "content": "string",        // Message content
    "timestamp": "Date",        // Time the message was sent
    "is_read": "boolean"        // Read (Delivered) status of the message
}

Key features:

Data Models

The application uses document models to ensure data consistency:

Configuration

Database configuration is managed through config-server.json:

{
    "database": {
        "username": "your_username",
        "password": "your_password",
        "host": "database_host",
        "name": "your_database"
    }
}

Security Features

Performance Considerations