220 lines
4.3 KiB
Markdown
220 lines
4.3 KiB
Markdown
# MongoDB Setup Guide for Grateful Journal
|
|
|
|
## Prerequisites
|
|
|
|
- MongoDB installed on your system
|
|
- Python 3.9+
|
|
- pip package manager
|
|
|
|
## Installation Steps
|
|
|
|
### 1. Install MongoDB
|
|
|
|
#### macOS (using Homebrew)
|
|
|
|
```bash
|
|
brew tap mongodb/brew
|
|
brew install mongodb-community
|
|
```
|
|
|
|
#### Linux (Ubuntu/Debian)
|
|
|
|
```bash
|
|
curl -fsSL https://www.mongodb.org/static/pgp/server-6.0.asc | sudo apt-key add -
|
|
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/6.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-6.0.list
|
|
sudo apt-get update
|
|
sudo apt-get install -y mongodb-org
|
|
```
|
|
|
|
#### Windows
|
|
|
|
Download and run the installer from [MongoDB Community Download](https://www.mongodb.com/try/download/community)
|
|
|
|
### 2. Start MongoDB Server
|
|
|
|
#### macOS / Linux
|
|
|
|
```bash
|
|
# Start as a service (recommended)
|
|
brew services start mongodb-community
|
|
|
|
# Or run directly
|
|
mongod --config /usr/local/etc/mongod.conf
|
|
```
|
|
|
|
#### Windows
|
|
|
|
MongoDB should run as a service. If not:
|
|
|
|
```bash
|
|
net start MongoDB
|
|
```
|
|
|
|
### 3. Verify MongoDB is Running
|
|
|
|
```bash
|
|
mongosh
|
|
```
|
|
|
|
If you see a connection prompt, MongoDB is running successfully.
|
|
|
|
### 4. Set up Python Backend Environment
|
|
|
|
Navigate to the backend directory:
|
|
|
|
```bash
|
|
cd backend
|
|
```
|
|
|
|
Create a virtual environment:
|
|
|
|
```bash
|
|
python3 -m venv venv
|
|
source venv/bin/activate # macOS/Linux
|
|
# or
|
|
venv\Scripts\activate # Windows
|
|
```
|
|
|
|
Install dependencies:
|
|
|
|
```bash
|
|
pip install -r requirements.txt
|
|
```
|
|
|
|
### 5. Configure Environment Variables
|
|
|
|
Copy the example env file:
|
|
|
|
```bash
|
|
cp .env.example .env
|
|
```
|
|
|
|
Edit `.env` with your settings (defaults work for local development):
|
|
|
|
```env
|
|
MONGODB_URI=mongodb://localhost:27017
|
|
MONGODB_DB_NAME=grateful_journal
|
|
API_PORT=8001
|
|
ENVIRONMENT=development
|
|
FRONTEND_URL=http://localhost:8000
|
|
```
|
|
|
|
### 6. Run the FastAPI Server
|
|
|
|
```bash
|
|
python main.py
|
|
```
|
|
|
|
You should see:
|
|
|
|
```
|
|
✓ Connected to MongoDB: grateful_journal
|
|
INFO: Uvicorn running on http://0.0.0.0:8001
|
|
```
|
|
|
|
### 7. Access API Documentation
|
|
|
|
Open your browser and go to:
|
|
|
|
- **Swagger UI**: http://localhost:8001/docs
|
|
- **ReDoc**: http://localhost:8001/redoc
|
|
- **Health Check**: http://localhost:8001/health
|
|
|
|
## MongoDB Collections Overview
|
|
|
|
The following collections will be created automatically on first write:
|
|
|
|
### `users`
|
|
|
|
Stores user profiles after Firebase Google Auth.
|
|
|
|
```json
|
|
{
|
|
"_id": ObjectId,
|
|
"email": "user@example.com",
|
|
"displayName": "John Doe",
|
|
"photoURL": "https://...",
|
|
"theme": "light",
|
|
"createdAt": ISODate,
|
|
"updatedAt": ISODate
|
|
}
|
|
```
|
|
|
|
### `entries`
|
|
|
|
Stores journal entries.
|
|
|
|
```json
|
|
{
|
|
"_id": ObjectId,
|
|
"userId": "user_id_string",
|
|
"title": "Today's thoughts",
|
|
"content": "Long-form journal content...",
|
|
"mood": "grateful",
|
|
"tags": ["reflection", "gratitude"],
|
|
"isPublic": false,
|
|
"createdAt": ISODate,
|
|
"updatedAt": ISODate
|
|
}
|
|
```
|
|
|
|
### `settings`
|
|
|
|
Stores user preferences and settings.
|
|
|
|
```json
|
|
{
|
|
"_id": ObjectId,
|
|
"userId": "user_id_string",
|
|
"notifications": true,
|
|
"emailNotifications": false,
|
|
"theme": "light",
|
|
"language": "en",
|
|
"updatedAt": ISODate
|
|
}
|
|
```
|
|
|
|
## API Endpoints
|
|
|
|
### Users
|
|
|
|
- `POST /api/users/register` — Register user after Firebase auth
|
|
- `GET /api/users/by-email/{email}` — Get user profile by email
|
|
- `PUT /api/users/update/{user_id}` — Update user profile
|
|
- `DELETE /api/users/{user_id}` — Delete user and associated data
|
|
|
|
### Entries
|
|
|
|
- `POST /api/entries/{user_id}` — Create new entry
|
|
- `GET /api/entries/{user_id}` — Get all entries (paginated)
|
|
- `GET /api/entries/{user_id}/{entry_id}` — Get specific entry
|
|
- `PUT /api/entries/{user_id}/{entry_id}` — Update entry
|
|
- `DELETE /api/entries/{user_id}/{entry_id}` — Delete entry
|
|
- `GET /api/entries/{user_id}/date/{date_str}` — Get entries by date (YYYY-MM-DD)
|
|
|
|
## Troubleshooting
|
|
|
|
**MongoDB connection refused**
|
|
|
|
- Check that MongoDB service is running: `brew services list` (macOS)
|
|
- Verify port 27017 is not blocked
|
|
|
|
**ModuleNotFoundError: pymongo**
|
|
|
|
- Ensure virtual environment is activated
|
|
- Run `pip install -r requirements.txt` again
|
|
|
|
**CORS errors in frontend**
|
|
|
|
- Check `FRONTEND_URL` in `.env` matches your frontend URL
|
|
- Default allows http://localhost:8000
|
|
|
|
## Next Steps
|
|
|
|
Once MongoDB and FastAPI are running:
|
|
|
|
1. Frontend calls Firebase Google Auth
|
|
2. Frontend sends auth token to `/api/users/register`
|
|
3. Backend creates user in MongoDB
|
|
4. Frontend can now call `/api/entries/*` endpoints with user_id
|