factory-class-crud-generator is a Python FastAPI application that automatically discovers model classes in a models directory, creates PostgreSQL database tables, and generates complete CRUD endpoints with OpenAPI documentation.
- Automatic Model Discovery: Automatically finds and registers all model classes in the
models/
directory - Dynamic Table Creation: Creates PostgreSQL tables based on SQLAlchemy model definitions
- Auto-Generated CRUD Endpoints: Generates complete Create, Read, Update, Delete endpoints for each model
- OpenAPI Documentation: Provides interactive API documentation at
/docs
and/redoc
- Type Safety: Uses Pydantic for request/response validation
- Comprehensive Testing: Includes full test suite with pytest
- Easy Extension: Simply add new model files to automatically get new API endpoints
- Python 3.11+
- PostgreSQL
- pip
- Clone or download the project:
git clone <repository-url>
cd factory-class-crud-generator
- Install dependencies:
pip install -r requirements.txt
- Set up PostgreSQL:
# Install PostgreSQL (Ubuntu/Debian)
sudo apt update
sudo apt install postgresql postgresql-contrib
# Start PostgreSQL service
sudo systemctl start postgresql
# Create database and user
sudo -u postgres createdb fccg
sudo -u postgres psql -c "ALTER USER postgres PASSWORD 'password';"
- Configure environment variables (optional):
cp .env.example .env
# Edit .env with your database settings
- Run the application:
python main.py
The API will be available at:
- Main API: http://localhost:8000
- Interactive docs: http://localhost:8000/docs
- ReDoc: http://localhost:8000/redoc
To add a new model and automatically get CRUD endpoints:
- Create a new Python file in the
models/
directory - Define your model class inheriting from
BaseModel
- Restart the application
Example model (models/blog_post.py
):
from sqlalchemy import Column, String, Text, Boolean
from models.base import BaseModel
class BlogPost(BaseModel):
title = Column(String(200), nullable=False)
content = Column(Text, nullable=True)
published = Column(Boolean, default=False, nullable=False)
author = Column(String(100), nullable=False)
This will automatically create:
- Database table:
blog_posts
- API endpoints:
POST /blogpost/
- Create new blog postGET /blogpost/
- List all blog posts (with pagination)GET /blogpost/{id}
- Get specific blog postPUT /blogpost/{id}
- Update blog postDELETE /blogpost/{id}
- Delete blog post
All models automatically get the following endpoints:
Method | Endpoint | Description |
---|---|---|
POST | /{model}/ |
Create new item |
GET | /{model}/ |
List all items (supports skip and limit query params) |
GET | /{model}/{id} |
Get item by ID |
PUT | /{model}/{id} |
Update item by ID |
DELETE | /{model}/{id} |
Delete item by ID |
# Create a new user
curl -X POST "http://localhost:8000/user/" \
-H "Content-Type: application/json" \
-d '{
"name": "John Doe",
"email": "john@example.com",
"age": 30,
"is_active": true
}'
# Get all users
curl "http://localhost:8000/user/"
# Get user by ID
curl "http://localhost:8000/user/1"
# Update user
curl -X PUT "http://localhost:8000/user/1" \
-H "Content-Type: application/json" \
-d '{
"name": "John Smith",
"age": 31
}'
# Delete user
curl -X DELETE "http://localhost:8000/user/1"
Create a .env
file in the project root:
# Database Configuration
DATABASE_URL=postgresql://postgres:password@localhost:5432/fccg
POSTGRES_DB=example
POSTGRES_USER=example
POSTGRES_PASSWORD=pass
# API Configuration
API_TITLE=factory-class-crud-generator
API_DESCRIPTION=Auto-generated CRUD API from model classes
API_VERSION=1.0.0
# Server Configuration
HOST=0.0.0.0
PORT=8000
DEBUG=true
# Model Discovery
MODELS_DIR=models
The application uses PostgreSQL by default. You can modify the database URL in the .env
file or set the DATABASE_URL
environment variable.
factory-class-crud-generator/
├── app/
│ ├── __init__.py
│ ├── config.py # Application configuration
│ ├── database.py # Database setup and connection
│ ├── model_discovery.py # Automatic model discovery
│ └── crud_generator.py # Dynamic CRUD endpoint generation
├── models/
│ ├── __init__.py
│ ├── base.py # Base model class
│ ├── offer.py # Example Offer model
│ ├── product.py # Example Product model
├── main.py # Application entry point
├── requirements.txt # Python dependencies
├── .env # Environment configuration
└── README.md # This file
- Model Discovery: On startup, the application scans the
models/
directory for Python files - Class Detection: Finds all classes that inherit from
BaseModel
- Table Creation: Uses SQLAlchemy to create database tables based on model definitions
- Endpoint Generation: Dynamically creates FastAPI routes for each model
- Documentation: Automatically generates OpenAPI documentation with proper schemas
python main.py
- Using Uvicorn directly:
uvicorn main:app --host 0.0.0.0 --port 8000 --workers 4
- Using Docker (create Dockerfile):
FROM python:3.11-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
EXPOSE 8000
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
- Environment Variables for Production:
DATABASE_URL=postgresql://user:password@db-host:5432/fccg
DEBUG=false
- Fork the repository
- Create a feature branch
- Submit a pull request
This project is licensed under the MIT License.
For issues and questions, please create an issue in the repository.