Database Configuration
Relevant source files
The following files were used as context for generating this wiki page:
Introduction
The database configuration system in the EICHackathon project provides a robust interface for connecting the Node.js backend to a MongoDB instance. It utilizes the Mongoose Object Data Modeling (ODM) library to manage data relationships, provide schema validation, and facilitate object-document mapping. The primary goal of this module is to ensure a stable, asynchronous connection to the database before the Express server begins handling requests.
The configuration relies on environment variables for sensitive connection details and follows a modular pattern, separating connection logic from data modeling. This structure supports secure authentication and efficient data persistence for features such as user management.
Sources: BACKEND/src/db/index.js, BACKEND/src/index.js, BACKEND/package.json:28
Connection Architecture
The database connection is established through a dedicated utility function that leverages environment variables and Mongoose. The system ensures that the application does not start listening for HTTP traffic until a successful database handshake is confirmed.
Initialization Workflow
The server entry point initializes the database connection by calling the connectDB function. Upon a successful promise resolution, the Express application starts. If the connection fails, the process is terminated with an exit code of 1 to prevent the application from running in a broken state.
The diagram shows the sequential dependency between database connectivity and server availability.
Sources: BACKEND/src/index.js:13-23, BACKEND/src/db/index.js:4-15
Configuration Parameters
The connection logic utilizes specific constants and environment variables to target the correct database instance and collection.
| Variable | Source | Purpose |
|---|---|---|
process.env.URL | .env | The MongoDB connection string (URI). |
DB_NAME | src/constants.js | The specific name of the database to use. |
process.env.PORT | .env | Port on which the server listens after DB connection. |
Sources: BACKEND/src/db/index.js:5-8, BACKEND/src/index.js:15
Data Modeling
The project uses Mongoose schemas to define the structure of documents within the MongoDB collections. Currently, the system implements a robust User model with built-in security features.
User Schema Implementation
The User model defines the attributes and behaviors for application users. It includes validation, indexing for performance, and middleware for security.
| Field | Type | Constraints | Description |
|---|---|---|---|
username | String | Unique, Required, Indexed, Lowercase | Unique identifier for user login. |
email | String | Unique, Required, Lowercase | User contact and auth email. |
fullName | String | Required, Indexed | The user's display name. |
password | String | Required | Bcrypt hashed password string. |
refreshToken | String | Optional | Token for session persistence. |
Sources: BACKEND/src/models/User.model.js:5-35
Security Logic and Middleware
The database configuration includes pre-save hooks and instance methods to handle encryption and authentication tokens.
- Pre-save Hook: Automatically hashes the user's password using
bcrypt(10 rounds) before saving to the database, provided the password field was modified. - Password Verification: An instance method
isPasswordCorrectprovides a secure way to compare plain-text inputs with stored hashes. - JWT Generation: Methods are provided to generate Access and Refresh tokens using
jsonwebtoken.
The sequence diagram illustrates the automated security processing that occurs during document persistence.
Sources: BACKEND/src/models/User.model.js:38-76
Implementation Details
The core connection logic is encapsulated in BACKEND/src/db/index.js.
const connectDB = async () => {
try {
const connectionInstance = await mongoose.connect(`${process.env.URL}`, {
dbName: DB_NAME,
});
console.log(
`\n MongoDB connected !! DB_HOST: ${connectionInstance.connection.host}`
);
} catch (error) {
console.log("MONGODB CONNECTION ERROR", error);
process.exit(1);
}
};
Sources: BACKEND/src/db/index.js:4-15
Summary
The Database Configuration module establishes a critical foundation for the EICHackathon backend. By enforcing a "Database-First" startup policy, it prevents runtime errors related to data persistence. Through the use of Mongoose schemas and bcrypt-integrated middleware, it ensures that data is not only structured but also secure by default.