- Created app/database.py with SQLAlchemy engine, session management, and connection utilities - Added comprehensive database models in app/models.py for User, Client, Phone, Case, Transaction, Document, and Payment - Implemented FastAPI application with database lifecycle management in app/main.py - Added health check endpoint to verify database connectivity - Created README.md with database configuration documentation - Verified database connection works correctly with SQLite backend
101 lines
2.6 KiB
Markdown
101 lines
2.6 KiB
Markdown
# Delphi Database
|
|
|
|
A legal case management database application built with FastAPI, SQLAlchemy, and modern Python practices.
|
|
|
|
## Database Configuration
|
|
|
|
The application uses SQLAlchemy ORM with support for multiple database backends.
|
|
|
|
### Environment Variables
|
|
|
|
Configure your database connection using environment variables:
|
|
|
|
```bash
|
|
# SQLite (default for development)
|
|
DATABASE_URL=sqlite:///./delphi.db
|
|
|
|
# PostgreSQL (production example)
|
|
DATABASE_URL=postgresql://username:password@localhost:5432/delphi_db
|
|
|
|
# MySQL (alternative)
|
|
DATABASE_URL=mysql://username:password@localhost:3306/delphi_db
|
|
```
|
|
|
|
### Database Models
|
|
|
|
The application includes the following models:
|
|
|
|
- **User**: Authentication and user management
|
|
- **Client**: Client/contact information
|
|
- **Phone**: Phone numbers linked to clients
|
|
- **Case**: Legal cases with unique file numbers
|
|
- **Transaction**: Financial transactions for cases
|
|
- **Document**: Case-related documents
|
|
- **Payment**: Payment records for cases
|
|
|
|
### Database Connection Management
|
|
|
|
The `app/database.py` module provides:
|
|
|
|
- **Database engine configuration** with connection pooling and error handling
|
|
- **Session management** with automatic cleanup via FastAPI dependency injection
|
|
- **Table creation utilities** for application startup
|
|
- **Connection health monitoring** via the `/health` endpoint
|
|
|
|
### Usage Examples
|
|
|
|
#### Basic Application Startup
|
|
|
|
```python
|
|
from fastapi import FastAPI, Depends
|
|
from sqlalchemy.orm import Session
|
|
from app.database import get_db
|
|
|
|
@app.get("/clients/")
|
|
async def get_clients(db: Session = Depends(get_db)):
|
|
# Use the database session
|
|
return db.query(Client).all()
|
|
```
|
|
|
|
#### Health Check
|
|
|
|
The `/health` endpoint verifies database connectivity:
|
|
|
|
```bash
|
|
curl http://localhost:8000/health
|
|
# {"status": "healthy", "database": "connected", "users": 0}
|
|
```
|
|
|
|
## Getting Started
|
|
|
|
1. Install dependencies:
|
|
```bash
|
|
pip install -r requirements.txt
|
|
```
|
|
|
|
2. Configure your database in environment variables (see `.env` file)
|
|
|
|
3. Run the application:
|
|
```bash
|
|
uvicorn app.main:app --reload
|
|
```
|
|
|
|
4. Access the API at `http://localhost:8000`
|
|
|
|
## Project Structure
|
|
|
|
```
|
|
app/
|
|
├── main.py # FastAPI application entry point
|
|
├── database.py # Database configuration and session management
|
|
├── models.py # SQLAlchemy models
|
|
└── templates/ # HTML templates
|
|
```
|
|
|
|
## Development
|
|
|
|
- Database tables are automatically created on application startup
|
|
- Use the health check endpoint to verify database connectivity
|
|
- Environment variables control database configuration (never hardcode credentials)
|
|
- SQLAlchemy provides type-safe database operations with relationship management
|