A production-ready wrapper application framework for FastAPI
Project description
RealFastAPI
A production-ready wrapper application framework for FastAPI.
Features
- Modular Architecture
- Clean Architecture Principles
- Async SQLAlchemy 2.0 Integration
- Generic CRUD
- JWT Authentication & Security
Installation
pip install realfastapi
Quick Start
Create a new application:
from realfastapi.core import RealFastAPI, RealFastAPIConfig, DatabaseConfig
config = RealFastAPIConfig(
title="My App",
db_config=DatabaseConfig(url="sqlite+aiosqlite:///:memory:")
)
app = RealFastAPI(config)
if __name__ == "__main__":
import uvicorn
uvicorn.run(app)
Real Example
For a complete, production-ready example including authentication, relationships, and custom endpoints, check out the Simple App Example.
This example demonstrates:
- Custom Models & Schemas: Using SQLAlchemy models with relationships (User <-> Items).
- Authentication: JWT authentication with protected routes.
- Custom Endpoints: Advanced filtering and eager loading to prevent N+1 queries.
Generic CRUD
RealFastAPI provides a powerful BaseCRUD class and create_crud_router helper to automatically generate standard CRUD (Create, Read, Update, Delete) endpoints for your SQLAlchemy models.
Usage
- Define Your Schemas: Create Pydantic models for Create, Update, and Output.
- Define Your Model: Create a SQLAlchemy model.
- Initialize BaseCRUD: Create an instance of
BaseCRUD. - Create Router: Use
create_crud_routerto register routes.
from realfastapi.crud.base import BaseCRUD
from realfastapi.crud.routes import create_crud_router
# ... Define Item, ItemCreate, ItemUpdate, ItemOut ...
# Initialize CRUD
crud_item = BaseCRUD(Item, ItemCreate, ItemUpdate, ItemOut)
# Register Routes
create_crud_router(
crud=crud_item,
path="/items",
app=app,
tags=["Items"]
)
This automatically generates the following endpoints:
POST /items: Create a new item.GET /items/{id}: Get an item by ID.GET /items: Get multiple items (supports paginationskip,limit, sortingsort, and filtering).PUT /items/{id}: Update an item.DELETE /items/{id}: Delete an item.
Authentication
RealFastAPI includes a built-in JWT authentication system.
Usage
- Configure Auth: Set up
AuthConfigin yourRealFastAPIConfig. - Create Auth Router: Use
create_auth_routerto generate login endpoints. - Protect Endpoints: Use
app.get_current_userdependency.
from realfastapi.auth.routes import create_auth_router
from realfastapi.core import AuthConfig
# 1. Config
auth_config = AuthConfig(
secret_key="YOUR_SECRET_KEY",
token_url="/auth/login"
)
config = RealFastAPIConfig(..., auth_config=auth_config)
app = RealFastAPI(config)
# 2. Auth Router
create_auth_router(
app=app,
prefix="/auth",
crud=crud_user, # Your User CRUD instance
secret_key="YOUR_SECRET_KEY",
username_field="username",
password_field="password",
)
# 3. Protect Endpoint
@app.get("/protected")
async def protected_route(user: str = Depends(app.get_current_user)):
return {"message": f"Hello {user}"}
Database Migrations
RealFastAPI integrates with Alembic for database migrations.
Usage
-
Initialize: Run the CLI command to scaffold migrations.
python -m realfastapi.cli init-db
This creates a
migrationsdirectory andalembic.ini. -
Configure: Edit
migrations/env.pyto import your SQLAlchemy Base.# migrations/env.py from myapp.database import Base # Import your Base target_metadata = Base.metadata
-
Generate Migration:
alembic revision --autogenerate -m "Initial migration"
-
Apply Migration:
alembic upgrade head
Testing
RealFastAPI provides utilities to make testing easier, including TestClient and database overrides.
Usage
Use realfastapi.testing.override_get_db to create an isolated test database. The returned object exposes the engine for setup (like creating tables) and behaves as a dependency override.
import pytest
from realfastapi.testing import TestClient, override_get_db
from myapp.main import app
from myapp.database import Base # Import your models
@pytest.mark.asyncio
async def test_create_item():
# 1. Initialize Test Database
test_db = override_get_db("sqlite+aiosqlite:///:memory:")
# 2. Create Tables (using exposed engine)
async with test_db.engine.begin() as conn:
await conn.run_sync(Base.metadata.create_all)
# 3. Override Dependency
app.dependency_overrides[app.db.get_db] = test_db
with TestClient(app) as client:
# Create
response = client.post("/items", json={"title": "Test Item"})
assert response.status_code == 200
data = response.json()
assert data["title"] == "Test Item"
Requirements
- Python 3.10+
- FastAPI
- SQLAlchemy 2.0+
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file realfastapi-0.1.0.tar.gz.
File metadata
- Download URL: realfastapi-0.1.0.tar.gz
- Upload date:
- Size: 6.1 MB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
345ac9ee57dca6ad39ffb606e365b5bd12a33e3514dd8fea682e99b8afa0aaa6
|
|
| MD5 |
bb99123f7ed6692d06c3dd082a49fc5c
|
|
| BLAKE2b-256 |
02953d74a425a8bbcb5cfb52d77e96f317587c14e8e4fcf9e66620460e9a3776
|
File details
Details for the file realfastapi-0.1.0-py3-none-any.whl.
File metadata
- Download URL: realfastapi-0.1.0-py3-none-any.whl
- Upload date:
- Size: 17.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
70db9eef1b3c58e5e7c1747e01906b45e32feb5239087acf5fd7bc16502d55e8
|
|
| MD5 |
1c81e59310c5ac26383550440c339355
|
|
| BLAKE2b-256 |
fcaabcf3713ac46356bb4256ca9cd5fe0b9013b972d01fb1f0e21ce257fc9a2b
|