|
| 1 | +# Database Documentation |
| 2 | + |
| 3 | +## Overview |
| 4 | +The application uses **MongoDB** as its primary data store. Due to the document-oriented nature of MongoDB, we do not have rigid tables, but we enforce schemas using **Pydantic Models** in the application layer. |
| 5 | + |
| 6 | +## Entity Relationship Diagram (ERD) |
| 7 | + |
| 8 | +Although MongoDB is NoSQL, the entities have implicit relationships. |
| 9 | + |
| 10 | +```mermaid |
| 11 | +erDiagram |
| 12 | + USERS ||--|{ TASKS : "owns" |
| 13 | + |
| 14 | + USERS { |
| 15 | + ObjectId _id PK |
| 16 | + string email UK |
| 17 | + string hashed_password |
| 18 | + string name |
| 19 | + enum role "USER | ADMIN" |
| 20 | + list permissions |
| 21 | + datetime created_at |
| 22 | + } |
| 23 | +
|
| 24 | + TASKS { |
| 25 | + ObjectId _id PK |
| 26 | + string title |
| 27 | + string description |
| 28 | + string owner_id FK "Reference to USERS._id" |
| 29 | + datetime created_at |
| 30 | + } |
| 31 | +``` |
| 32 | + |
| 33 | +## Collection Details |
| 34 | + |
| 35 | +### 1. Users Collection (`users`) |
| 36 | +Stores all registered user information. |
| 37 | +- **_id**: Automatically generated MongoDB ObjectId. |
| 38 | +- **email**: Unique index ensures no duplicate registrations. |
| 39 | +- **role**: Determines access level. Defaults to `USER`. |
| 40 | +- **permissions**: List of granular permission strings (extensible). |
| 41 | + |
| 42 | +### 2. Tasks Collection (`tasks`) |
| 43 | +Stores to-do items for users. |
| 44 | +- **_id**: Automatically generated MongoDB ObjectId. |
| 45 | +- **owner_id**: A string representation of the User's ObjectId. This links the task to a specific user. |
| 46 | +- **Index**: Typically indexed on `owner_id` for fast retrieval of a user's tasks. |
| 47 | + |
| 48 | +## Implementation Details |
| 49 | + |
| 50 | +### Model Validation |
| 51 | +We use Pydantic models to validate data before it enters the database. |
| 52 | +- **Location**: `backend/app/models/` |
| 53 | +- **Files**: `user.py`, `task.py` |
| 54 | + |
| 55 | +### ID Handling |
| 56 | +MongoDB uses `_id` (ObjectId), but API clients typically expect `id` (string). |
| 57 | +- Our Pydantic models use `Field(alias="_id")` to map the database `_id` to the API `id` field automatically. |
0 commit comments