# bcl32-connect-db

Generic async database repository for SQLAlchemy and FastAPI.

## Installation

```bash
pip install bcl32-connect-db
```

## Usage

```python
from connect_db import DatabaseSession, DatabaseRepository, get_repository
from fastapi import Depends
from your_app.models import User

# Initialize the database session manager
db = DatabaseSession("postgresql+asyncpg://user:pass@host/db")

# Use in FastAPI routes
@app.get("/users")
async def list_users(
    repo: DatabaseRepository[User] = Depends(get_repository(User, db.get_session))
):
    return await repo.filter()
```

## Publishing to PyPI

### Manual publish

```bash
cd /home/brandon/Projects/web-app-monorepo/python-modules/connect_db

# Create virtual environment
python3 -m venv .venv
source .venv/bin/activate

# Install build tools
pip install build twine

# Build the package
python -m build

# Upload to PyPI (requires API token from https://pypi.org/manage/account/token/)
twine upload dist/*
```

### Automated publish (GitHub Actions)

The workflow at `.github/workflows/publish-connect-db.yml` auto-publishes on push to `main` when files in `python-modules/connect_db/` change.

**Setup:** Add `PYPI_API_TOKEN` secret to your GitHub repo (Settings → Secrets → Actions).

## Package Structure

```
python-modules/connect_db/
├── pyproject.toml
├── src/connect_db/
│   ├── __init__.py
│   ├── session.py      # DatabaseSession class
│   ├── repository.py   # DatabaseRepository class
│   └── dependencies.py # FastAPI dependency helpers
└── old/                # Original files (backup)
```

## Integration with Docker

The Dockerfile installs directly from PyPI:

```dockerfile
RUN pip install --no-cache-dir bcl32-connect-db
```

## App Integration Example

**main.py:**
```python
from connect_db import DatabaseSession
from config import AsyncPostgres

db_settings = AsyncPostgres()
db = DatabaseSession(db_settings.DATABASE_URL)
```

**routes/classifier.py:**
```python
from connect_db import get_repository, DatabaseRepository
from main import db
from database import db_models

ModelRepository = Annotated[
    DatabaseRepository[db_models.Classifier],
    Depends(get_repository(db_models.Classifier, db.get_session)),
]
```
