# fastapi_agmin

`fastapi_agmin` is an open-source Python package that provides an autogenerated admin interface for SQLAlchemy models.  
It delivers an interactive, in-table CRUD experience using FastAPI + Vue 3 + AG Grid, a CRUD Admin interface for FastAPI SQLAlchemy backed models.

Using it should be as simple as:

```python
from fastapi import FastAPI
from fastapi_agmin import create_agmin

# SQLAlchemy setup
Base = declarative_base()
engine = create_engine("sqlite:///test.db")
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)

# Dependency for FastAPI: yields a session and always closes it
def get_db() -> Generator:
    db = SessionLocal()
    try:
        yield db
    finally:
        db.close()

app = FastAPI()
# BELOW IS THE PART THAT MATTERS
create_agmin(Base, get_session=get_db, app=app_test)  # your Base, app, and session here
```

Then just start your server with uvicorn as normally and access localhost:8000/agmin (if running a local server).

## Setup for development

1. **Install backend dependencies**

```bash
pip install uv  # in case it is not installed 
uv venv
uv pip install '.[dev]'
```

2. **Build frontend**

```bash
cd frontend
npm install
npm run build
```

or, if you prefer serving the frontend separately for quicker iteration

```bash
cd frontend
npm install
npm run dev
```

but remeber to build before commiting, because the project is designed to be served by fastapi without an extra frontend server.

3. **Run fastapi test app**

```bash
uvicorn tests.fastapi_agmin.app_test:app_test --reload --log-level debug
```


---

## ✨ **Project Goal**

Build a plug-and-play admin interface that:

- Works with any project using SQLAlchemy models.
- Automatically exposes CRUD endpoints without manual wiring.
- Provides a rich, interactive web interface with inline edits, add/remove, and relationship handling.
- Ships as a single Python package with html and Vue built assets builtin.

---

## 🐍 **Python Backend Responsibilities**

The backend is powered by FastAPI and provides:

✅ A `create_agmin` function you can import use with your SQLAlchemy `Base`.  
✅ Automatic model introspection (fields, relationships, many-to-many).  
✅ A JSON/YAML **metadata endpoint** at `/models/metadata` that describes the models + relationships.  
✅ Standardized **CRUD API endpoints**:

- `GET /models/{model}`
- `POST /models/{model}`
- `PUT /models/{model}/{id}`
- `DELETE /models/{model}/{id}`

✅ A FastAPI router that serves the **prebuilt Vue app** at `/admin`.

Everything is bundled — no extra setup on the Python side.

---

## 🌐 **Vue 3 Frontend Responsibilities**

The frontend (under `frontend/`) is a standalone TypeScript + Vue 3 app that:

✅ Dynamically loads the backend's model metadata.  
✅ Renders AG Grid tables for each model with:

- Editable cells.
- Add/delete row buttons.
- Support for many-to-many and foreign key relationships.

✅ Talks to the backend **only over REST APIs** using Axios.
✅ Builds into static assets (HTML, JS, CSS) using Vite.
✅ Outputs to `src/fastapi_agmin/static/` for Python to serve.

# Work to be done

1. Pagination and pushed down columns filters: the agmin can handle small databases now. To handle big ones, we need to paginate results fetched from the backend. If we do so, column level filters need to be pushed to the API, otherwise they will only filter the results in the current page.
1. Add Column names and Modal Form fields comming from backend defined names. The same for help text and alike.