added encryption
This commit is contained in:
@@ -30,56 +30,56 @@ This refactoring addresses critical database issues and optimizes the MongoDB sc
|
||||
### Backend Core
|
||||
|
||||
1. **[models.py](./models.py)** — Updated Pydantic models
|
||||
- Changed `User.id: str` → now uses `_id` alias for ObjectId
|
||||
- Added `JournalEntry.entryDate: datetime`
|
||||
- Added `EncryptionMetadata` model for encryption support
|
||||
- Added pagination response models
|
||||
- Changed `User.id: str` → now uses `_id` alias for ObjectId
|
||||
- Added `JournalEntry.entryDate: datetime`
|
||||
- Added `EncryptionMetadata` model for encryption support
|
||||
- Added pagination response models
|
||||
|
||||
2. **[routers/users.py](./routers/users.py)** — Rewrote user logic
|
||||
- Changed user registration from `insert_one` → `update_one` with upsert
|
||||
- Prevents duplicate users (one per email)
|
||||
- Validates ObjectId conversions with error handling
|
||||
- Added `get_user_by_id` endpoint
|
||||
- Changed user registration from `insert_one` → `update_one` with upsert
|
||||
- Prevents duplicate users (one per email)
|
||||
- Validates ObjectId conversions with error handling
|
||||
- Added `get_user_by_id` endpoint
|
||||
|
||||
3. **[routers/entries.py](./routers/entries.py)** — Updated entry handling
|
||||
- Convert all `userId` from string → ObjectId
|
||||
- Enforce user existence check before entry creation
|
||||
- Added `entryDate` field support
|
||||
- Added `get_entries_by_month` for calendar queries
|
||||
- Improved pagination with `hasMore` flag
|
||||
- Better error messages for invalid ObjectIds
|
||||
- Convert all `userId` from string → ObjectId
|
||||
- Enforce user existence check before entry creation
|
||||
- Added `entryDate` field support
|
||||
- Added `get_entries_by_month` for calendar queries
|
||||
- Improved pagination with `hasMore` flag
|
||||
- Better error messages for invalid ObjectIds
|
||||
|
||||
### New Scripts
|
||||
|
||||
4. **[scripts/migrate_data.py](./scripts/migrate_data.py)** — Data migration
|
||||
- Deduplicates users by email (keeps oldest)
|
||||
- Converts `entries.userId` string → ObjectId
|
||||
- Adds `entryDate` field (defaults to createdAt)
|
||||
- Adds encryption metadata
|
||||
- Verifies data integrity post-migration
|
||||
- Deduplicates users by email (keeps oldest)
|
||||
- Converts `entries.userId` string → ObjectId
|
||||
- Adds `entryDate` field (defaults to createdAt)
|
||||
- Adds encryption metadata
|
||||
- Verifies data integrity post-migration
|
||||
|
||||
5. **[scripts/create_indexes.py](./scripts/create_indexes.py)** — Index creation
|
||||
- Creates unique index on `users.email`
|
||||
- Creates compound indexes:
|
||||
- `entries(userId, createdAt)` — for history/pagination
|
||||
- `entries(userId, entryDate)` — for calendar view
|
||||
- Creates supporting indexes for tags and dates
|
||||
- Creates unique index on `users.email`
|
||||
- Creates compound indexes:
|
||||
- `entries(userId, createdAt)` — for history/pagination
|
||||
- `entries(userId, entryDate)` — for calendar view
|
||||
- Creates supporting indexes for tags and dates
|
||||
|
||||
### Documentation
|
||||
|
||||
6. **[SCHEMA.md](./SCHEMA.md)** — Complete schema documentation
|
||||
- Full field descriptions and examples
|
||||
- Index rationale and usage
|
||||
- Query patterns with examples
|
||||
- Data type conversions
|
||||
- Security considerations
|
||||
- Full field descriptions and examples
|
||||
- Index rationale and usage
|
||||
- Query patterns with examples
|
||||
- Data type conversions
|
||||
- Security considerations
|
||||
|
||||
7. **[MIGRATION_GUIDE.md](./MIGRATION_GUIDE.md)** — Step-by-step migration
|
||||
- Pre-migration checklist
|
||||
- Backup instructions
|
||||
- Running migration and index scripts
|
||||
- Rollback procedure
|
||||
- Troubleshooting guide
|
||||
- Pre-migration checklist
|
||||
- Backup instructions
|
||||
- Running migration and index scripts
|
||||
- Rollback procedure
|
||||
- Troubleshooting guide
|
||||
|
||||
---
|
||||
|
||||
@@ -100,6 +100,7 @@ This refactoring addresses critical database issues and optimizes the MongoDB sc
|
||||
```
|
||||
|
||||
**Key Changes:**
|
||||
|
||||
- ✓ Unique email index
|
||||
- ✓ Settings embedded (theme field)
|
||||
- ✓ No separate settings collection
|
||||
@@ -115,11 +116,11 @@ This refactoring addresses critical database issues and optimizes the MongoDB sc
|
||||
mood: string | null,
|
||||
tags: string[],
|
||||
isPublic: boolean,
|
||||
|
||||
|
||||
entryDate: datetime, // ← NEW: Logical journal date
|
||||
createdAt: datetime,
|
||||
updatedAt: datetime,
|
||||
|
||||
|
||||
encryption: { // ← NEW: Encryption metadata
|
||||
encrypted: boolean,
|
||||
iv: string | null,
|
||||
@@ -129,6 +130,7 @@ This refactoring addresses critical database issues and optimizes the MongoDB sc
|
||||
```
|
||||
|
||||
**Key Changes:**
|
||||
|
||||
- ✓ `userId` is ObjectId
|
||||
- ✓ `entryDate` separates "when written" (createdAt) from "which day it's for" (entryDate)
|
||||
- ✓ Encryption metadata for future encrypted storage
|
||||
@@ -141,12 +143,14 @@ This refactoring addresses critical database issues and optimizes the MongoDB sc
|
||||
### User Registration (Upsert)
|
||||
|
||||
**Old:**
|
||||
|
||||
```python
|
||||
POST /api/users/register
|
||||
# Created new user every time (duplicates!)
|
||||
```
|
||||
|
||||
**New:**
|
||||
|
||||
```python
|
||||
POST /api/users/register
|
||||
# Idempotent: updates if exists, inserts if not
|
||||
@@ -156,6 +160,7 @@ POST /api/users/register
|
||||
### Get User by ID
|
||||
|
||||
**New Endpoint:**
|
||||
|
||||
```
|
||||
GET /api/users/{user_id}
|
||||
```
|
||||
@@ -165,6 +170,7 @@ Returns user by ObjectId instead of only by email.
|
||||
### Create Entry
|
||||
|
||||
**Old:**
|
||||
|
||||
```json
|
||||
POST /api/entries/{user_id}
|
||||
{
|
||||
@@ -174,6 +180,7 @@ POST /api/entries/{user_id}
|
||||
```
|
||||
|
||||
**New:**
|
||||
|
||||
```json
|
||||
POST /api/entries/{user_id}
|
||||
{
|
||||
@@ -191,6 +198,7 @@ POST /api/entries/{user_id}
|
||||
### Get Entries
|
||||
|
||||
**Improved Response:**
|
||||
|
||||
```json
|
||||
{
|
||||
"entries": [...],
|
||||
@@ -206,6 +214,7 @@ POST /api/entries/{user_id}
|
||||
### New Endpoint: Get Entries by Month
|
||||
|
||||
**For Calendar View:**
|
||||
|
||||
```
|
||||
GET /api/entries/{user_id}/by-month/{year}/{month}?limit=100
|
||||
```
|
||||
@@ -314,6 +323,7 @@ No breaking changes if using the API correctly. However:
|
||||
### Backup Created
|
||||
|
||||
✓ Before migration, create backup:
|
||||
|
||||
```bash
|
||||
mongodump --db grateful_journal --out ./backup-2026-03-05
|
||||
```
|
||||
@@ -321,6 +331,7 @@ mongodump --db grateful_journal --out ./backup-2026-03-05
|
||||
### Rollback Available
|
||||
|
||||
If issues occur:
|
||||
|
||||
```bash
|
||||
mongorestore --drop --db grateful_journal ./backup-2026-03-05
|
||||
```
|
||||
@@ -396,26 +407,28 @@ Based on this new schema, future features are now possible:
|
||||
|
||||
If you encounter issues during or after migration:
|
||||
|
||||
1. **Check logs:**
|
||||
```bash
|
||||
tail -f backend/logs/backend.log
|
||||
```
|
||||
1. **Check logs:**
|
||||
|
||||
```bash
|
||||
tail -f backend/logs/backend.log
|
||||
```
|
||||
|
||||
2. **Verify database:**
|
||||
```bash
|
||||
mongosh --db grateful_journal
|
||||
db.users.countDocuments({})
|
||||
db.entries.countDocuments({})
|
||||
```
|
||||
|
||||
```bash
|
||||
mongosh --db grateful_journal
|
||||
db.users.countDocuments({})
|
||||
db.entries.countDocuments({})
|
||||
```
|
||||
|
||||
3. **Review documents:**
|
||||
- [SCHEMA.md](./SCHEMA.md) — Schema reference
|
||||
- [MIGRATION_GUIDE.md](./MIGRATION_GUIDE.md) — Troubleshooting section
|
||||
- [models.py](./models.py) — Pydantic model definitions
|
||||
- [SCHEMA.md](./SCHEMA.md) — Schema reference
|
||||
- [MIGRATION_GUIDE.md](./MIGRATION_GUIDE.md) — Troubleshooting section
|
||||
- [models.py](./models.py) — Pydantic model definitions
|
||||
|
||||
4. **Consult code:**
|
||||
- [routers/users.py](./routers/users.py) — User logic
|
||||
- [routers/entries.py](./routers/entries.py) — Entry logic
|
||||
- [routers/users.py](./routers/users.py) — User logic
|
||||
- [routers/entries.py](./routers/entries.py) — Entry logic
|
||||
|
||||
---
|
||||
|
||||
|
||||
Reference in New Issue
Block a user