> ## Documentation Index
> Fetch the complete documentation index at: https://docs.pyqdeck.in/llms.txt
> Use this file to discover all available pages before exploring further.

# Database Infrastructure

> MongoDB Atlas, connection management, and testing strategies

## Overview

PyqDeck uses **MongoDB Atlas** as its production database provider and **Mongoose** as the Object Data Modeling (ODM) library for Node.js.

## Production Setup (MongoDB Atlas)

* **Cluster**: We use a multi-region cluster for high availability and low latency.
* **Connection**: Managed via the `MONGODB_URI` environment variable.
* **Security**:
  * IP Whitelisting: Only authorized application servers and CI/CD runners can connect.
  * Authentication: SCRAM-SHA-1 or X.509 certificates.
  * Encryption: Data is encrypted at rest (AES-256) and in transit (TLS/SSL).

## Connection Management

The database connection is initialized in `backend/src/database.js`.

* **Singleton Pattern**: We ensure only one connection instance exists throughout the application lifecycle.
* **Configuration**:
  ```javascript theme={null}
  const options = {
    autoIndex: true, // Enabled in dev, usually disabled in production for performance
    maxPoolSize: 10, // Maintain up to 10 socket connections
    serverSelectionTimeoutMS: 5000, // Keep trying to send operations for 5 seconds
    socketTimeoutMS: 45000, // Close sockets after 45 seconds of inactivity
  };
  ```

## Indexing Strategy

To ensure high performance as the dataset grows, we define indexes in our Mongoose models:

* **Unique Indexes**: On slugs and email addresses.
* **Compound Indexes**: For common query patterns (e.g., `universityId` + `branchId` + `semesterId`).
* **TTL Indexes**: For temporary data like OTPs (if stored in MongoDB).
* **Text Indexes**: For basic global search functionality across universities and subjects.

## Testing with mongodb-memory-server

For unit and integration tests, we do **not** use a live MongoDB instance. Instead, we use `mongodb-memory-server`.

### Benefits

* **Isolation**: Each test run starts with a clean, empty database.
* **Speed**: Operations are performed in memory, making tests significantly faster.
* **Reliability**: Tests don't fail due to network issues or shared state in a physical dev database.

### Setup (`backend/tests/setup.js`)

The test runner automatically:

1. Starts an in-memory MongoDB instance.
2. Sets the `MONGODB_URI` to the memory server's address.
3. Clears all collections between test cases to ensure idempotency.
4. Shuts down the server after all tests are complete.
