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:
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
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:
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:
The application uses document models to ensure data consistency:
UserDocument: Represents user dataDocument: Base class for document modelsDatabase configuration is managed through config-server.json:
{
"database": {
"username": "your_username",
"password": "your_password",
"host": "database_host",
"name": "your_database"
}
}
username in Users and sender_id recipient_id in Messages for efficient querying.