File size: 9,011 Bytes
5be081e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
# Database Module - Todo API

## Overview

This module provides Neon Serverless PostgreSQL integration for the Todo API with multi-tenant isolation and Better Auth compatibility.

## Architecture

- **ORM**: SQLModel (Pydantic + SQLAlchemy)
- **Driver**: asyncpg (async PostgreSQL driver)
- **Database**: Neon Serverless PostgreSQL
- **Pattern**: Async/await throughout
- **Isolation**: Multi-tenant with user_id filtering

## Key Features

### 1. Serverless Optimization
- `pool_pre_ping=True`: Validates connections before use (handles cold starts)
- Small connection pool (5 connections): Optimized for serverless
- Connection recycling: Prevents stale connections
- Exponential backoff: Retry logic for connection failures

### 2. Multi-Tenant Isolation
- Every table includes `user_id: str` column (REQUIRED)
- `user_id` is indexed for query performance
- All queries MUST filter by `user_id`
- User ID extracted from JWT sub claim (never from client input)

### 3. Better Auth Compatibility
- No conflicts with Better Auth tables (user, session, account, verification)
- `user_id` references Better Auth's user table (managed externally)
- No foreign key constraints (Better Auth is source of truth)

## Files

```
backend/src/db/
β”œβ”€β”€ __init__.py       # Package exports and initialization
β”œβ”€β”€ connection.py     # Database connection and session management
β”œβ”€β”€ models.py         # SQLModel schemas (Todo, TodoCreate, TodoUpdate, TodoResponse)
β”œβ”€β”€ verify.py         # Verification script
└── README.md         # This file
```

## Setup

### 1. Environment Variables

Add to your `.env` file:

```bash
DATABASE_URL=postgresql://user:password@ep-xxx.us-east-2.aws.neon.tech/neondb
```

**Note**: The connection string will be automatically converted to `postgresql+asyncpg://` for async operations.

### 2. Install Dependencies

```bash
pip install -r backend/requirements.txt
```

Required packages:
- `sqlmodel==0.0.14`
- `asyncpg==0.29.0`
- `sqlalchemy[asyncio]==2.0.25`

### 3. Initialize Database

```python
from backend.src.db import init_database

# Call during application startup
await init_database()
```

### 4. Verify Setup

```bash
python -m backend.src.db.verify
```

This will:
- Test database connectivity
- Create tables and indexes
- Verify schema matches SQLModel definitions
- Display detailed status of each check

## Usage

### In FastAPI Routes

```python
from fastapi import Depends
from sqlalchemy.ext.asyncio import AsyncSession
from backend.src.db import get_session, Todo, TodoCreate

@app.post("/api/todos")
async def create_todo(
    todo_data: TodoCreate,
    user_id: str,  # Extracted from JWT middleware
    session: AsyncSession = Depends(get_session)
):
    # Create todo with user isolation
    todo = Todo(
        **todo_data.model_dump(),
        user_id=user_id  # REQUIRED for multi-tenant isolation
    )
    session.add(todo)
    await session.commit()
    await session.refresh(todo)
    return todo
```

### Query with User Isolation

```python
from sqlmodel import select
from backend.src.db import Todo

async def get_user_todos(user_id: str, session: AsyncSession):
    # ALWAYS filter by user_id
    statement = select(Todo).where(Todo.user_id == user_id)
    result = await session.execute(statement)
    return result.scalars().all()
```

### Application Lifecycle

```python
from fastapi import FastAPI
from backend.src.db import init_database, cleanup_database

app = FastAPI()

@app.on_event("startup")
async def startup():
    await init_database()

@app.on_event("shutdown")
async def shutdown():
    await cleanup_database()
```

## Database Schema

### Todos Table

```sql
CREATE TABLE todos (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    user_id VARCHAR(255) NOT NULL,
    title VARCHAR(200) NOT NULL,
    description TEXT,
    is_completed BOOLEAN NOT NULL DEFAULT FALSE,
    created_at TIMESTAMP NOT NULL DEFAULT NOW(),
    updated_at TIMESTAMP NOT NULL DEFAULT NOW()
);

-- Indexes
CREATE INDEX ix_todos_user_id ON todos(user_id);
CREATE INDEX idx_todos_user_completed ON todos(user_id, is_completed);
```

### Field Specifications

| Field | Type | Constraints | Description |
|-------|------|-------------|-------------|
| id | UUID | PRIMARY KEY | Unique identifier (auto-generated) |
| user_id | VARCHAR(255) | NOT NULL, INDEXED | Owner's user ID from JWT |
| title | VARCHAR(200) | NOT NULL, 1-200 chars | Todo title |
| description | TEXT | NULLABLE, max 2000 chars | Optional description |
| is_completed | BOOLEAN | NOT NULL, default FALSE | Completion status |
| created_at | TIMESTAMP | NOT NULL | Creation timestamp (UTC) |
| updated_at | TIMESTAMP | NOT NULL | Last modification timestamp (UTC) |

## SQLModel Models

### Todo (Database Model)
Full model with all fields including system-generated ones.

### TodoCreate (Request Model)
For creating todos. Excludes id, user_id, and timestamps.

### TodoUpdate (Request Model)
For updating todos. All fields optional (partial updates).

### TodoResponse (Response Model)
For API responses. Includes all fields.

## Security Considerations

### Multi-Tenant Isolation (CRITICAL)

1. **Every query MUST filter by user_id**
   ```python
   # βœ“ CORRECT
   select(Todo).where(Todo.user_id == user_id)

   # βœ— WRONG - Security vulnerability!
   select(Todo)  # Returns all users' todos
   ```

2. **Never accept user_id from client input**
   ```python
   # βœ“ CORRECT
   user_id = extract_from_jwt(token)

   # βœ— WRONG - Security vulnerability!
   user_id = request.body.get("user_id")
   ```

3. **Use UUID primary keys**
   - Prevents enumeration attacks
   - UUIDs are globally unique and unpredictable

### Input Validation

SQLModel automatically validates:
- Field types (string, boolean, datetime)
- String lengths (min/max)
- Required vs optional fields
- Data format (UUID, datetime)

## Performance Optimization

### Connection Pooling
- Pool size: 5 (optimized for serverless)
- Max overflow: 10 (additional connections when needed)
- Pool recycling: 3600 seconds (1 hour)

### Indexes
- `user_id`: Critical for all queries (every query filters by user_id)
- `(user_id, is_completed)`: Optimizes "show my incomplete todos" queries

### Query Patterns
- Use async/await for non-blocking I/O
- Select only needed columns when possible
- Consider pagination for large result sets

## Error Handling

### Connection Errors
```python
try:
    session = await get_session()
except ConnectionError as e:
    # Handle connection failure
    # Check DATABASE_URL is set
    # Verify Neon database is accessible
```

### Query Errors
```python
try:
    await session.execute(statement)
    await session.commit()
except Exception as e:
    await session.rollback()
    # Handle query error
```

## Testing

### Unit Tests
Test query functions with mock data:
```python
import pytest
from backend.src.db import Todo, TodoCreate

@pytest.mark.asyncio
async def test_create_todo(session):
    todo_data = TodoCreate(title="Test", description="Test todo")
    todo = Todo(**todo_data.model_dump(), user_id="test_user")
    session.add(todo)
    await session.commit()
    assert todo.id is not None
```

### Integration Tests
Test with real Neon database (test database):
```python
@pytest.mark.asyncio
async def test_user_isolation(session):
    # Create todos for two different users
    todo1 = Todo(title="User 1 Todo", user_id="user1")
    todo2 = Todo(title="User 2 Todo", user_id="user2")

    # Verify user1 cannot see user2's todos
    result = await session.execute(
        select(Todo).where(Todo.user_id == "user1")
    )
    todos = result.scalars().all()
    assert len(todos) == 1
    assert todos[0].user_id == "user1"
```

## Troubleshooting

### "DATABASE_URL environment variable not set"
- Ensure `.env` file exists in project root
- Verify `DATABASE_URL` is set with your Neon connection string
- Format: `postgresql://user:password@host.neon.tech/database`

### "Failed to connect to database"
- Check Neon database is running
- Verify connection string is correct
- Check network connectivity
- Review Neon dashboard for database status

### "Table does not exist"
- Run `await init_database()` during startup
- Or run verification script: `python -m backend.src.db.verify`

### "Connection pool exhausted"
- Increase `pool_size` in `connection.py` (default: 5)
- Check for connection leaks (ensure sessions are closed)
- Review query performance (slow queries hold connections)

## Next Steps

1. βœ“ Database module created
2. ⏭️ Create JWT middleware for user_id extraction
3. ⏭️ Implement FastAPI routes with user isolation
4. ⏭️ Add integration tests for multi-tenant scenarios
5. ⏭️ Configure Better Auth for user management

## References

- [SQLModel Documentation](https://sqlmodel.tiangolo.com/)
- [Neon PostgreSQL Documentation](https://neon.tech/docs)
- [FastAPI Database Guide](https://fastapi.tiangolo.com/tutorial/sql-databases/)
- [Better Auth Documentation](https://www.better-auth.com/)