A package for generating FastAPI CRUD operations and routes
Project description
CRUD FORGE
Overview
CRUD FORGE is a powerful Python package that revolutionizes the creation of CRUD (Create, Read, Update, Delete) operations for FastAPI applications. It automatically generates API routes, database models, and even provides metadata endpoints, significantly reducing development time and boilerplate code.
Key Features
- Automatic Model Generation: Instantly create SQLAlchemy and Pydantic models from your existing database schema.
- Dynamic CRUD Route Generation: Automatically generate FastAPI routes for all your models.
- Metadata API: Built-in routes to explore your database structure programmatically.
- Flexible Database Connection Management: Easy setup for various database types including PostgreSQL, MySQL, and SQLite.
- Customizable Route Generation: Tailor the generated routes to your specific needs.
- Advanced Filtering: Built-in filtering capabilities for GET requests.
- Comprehensive Error Handling: Robust error management out of the box.
- Type Hinting: Full type hint support for better IDE integration and code quality.
Installation
Install CRUD FORGE using pip:
pip install crud-forge==0.1.7
Quick Start
Here's how you can leverage CRUD FORGE's automatic generators:
from fastapi import FastAPI
from forge.db import DBForge
from forge.models import generate_models_from_metadata
from forge.crud import generate_crud
from forge.routes import generate_metadata_routes, generate_default_routes
# Set up FastAPI app and database
app = FastAPI()
db_manager = DBForge(db_url="postgresql://user:password@localhost/dbname")
metadata = db_manager.metadata # get the metadata from the database
# recieve the fastapi app to generate default routes
generate_default_routes(app)
# Generate metadata routes
app.include_router(generate_metadata_routes(metadata, db_manager.get_db))
# Automatically generate models from database metadata
models = generate_models_from_metadata(metadata)
# Generate CRUD routes for all models
for schema, models in models.items():
for table_name, (sqlalchemy_model, pydantic_model) in models.items():
generate_crud(sqlalchemy_model, pydantic_model, app, db_manager.get_db)
# Run the app
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000)
This example automatically generates models and CRUD routes for your entire database schema!
Package Structure
Detailed Usage
Database Connection
Use the DBForge
to set up your database connection:
from forge.db import DBForge
# Using a database URL
db_manager = DBForge(db_url="postgresql://user:password@localhost/dbname")
# Or using individual parameters
db_manager = DBForge(
db_type="postgresql",
user="user",
password="password",
host="localhost",
database="dbname"
)
Automatic Model Generation
Use generate_models_from_metadata
to automatically create models:
from forge.models import generate_models_from_metadata
models = generate_models_from_metadata(db_manager.metadata)
Dynamic CRUD Route Generation
Generate CRUD routes for all your models dynamically:
from forge.crud import generate_crud
for schema, models in models.items():
for table_name, (sqlalchemy_model, pydantic_model) in models.items():
generate_crud(sqlalchemy_model, pydantic_model, app, db_manager.get_db, tags=[f"{schema}_{table_name}"])
Metadata API
CRUD FORGE includes built-in routes to explore your database structure:
from forge.routes import generate_metadata_routes
metadata_router = generate_metadata_routes(db_manager.metadata, db_manager.get_db)
app.include_router(metadata_router)
This will create the following endpoints:
GET /dt/schemas
: List all schemas in the databaseGET /dt/{schema}/tables
: List all tables in a specific schemaGET /dt/{schema}/{table}/columns
: Get details of columns in a specific table
Default Routes
CRUD FORGE can generate some default routes for your application:
from forge.routes import generate_default_routes
generate_default_routes(app)
This will create the following endpoints:
GET /
: Root endpoint with basic API informationGET /health
: Health check endpointGET /version
: Version information endpoint
Documentation
For more detailed information about the API and advanced usage, please refer to our documentation.
Contributing
We welcome contributions! Please see our Contributing Guide for more details.
License
CRUD FORGE is released under the MIT License. See the LICENSE file for more details.
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
File details
Details for the file crud_forge-0.3.0.tar.gz
.
File metadata
- Download URL: crud_forge-0.3.0.tar.gz
- Upload date:
- Size: 22.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 561fa27d1abe610830e73a6e00d48ed24cb0b7958c7cb71f3dc26f22812ce503 |
|
MD5 | 657952ac1d2c207e4ca9c9616c22eb61 |
|
BLAKE2b-256 | 4083a1a344d09f1ff4f930407691263be01482f79a9ee477da8ecc889aab8976 |
File details
Details for the file crud_forge-0.3.0-py3-none-any.whl
.
File metadata
- Download URL: crud_forge-0.3.0-py3-none-any.whl
- Upload date:
- Size: 23.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6b9334249e60a9aab729d2377a1a0d1424d3bad6e603f20805f8225e819fe6ba |
|
MD5 | d37b10032b94ba4443ba96e00d05ad13 |
|
BLAKE2b-256 | 8d84ed70e0c3eb7407a51438affad4c6ea658eb16d408cc96159e982b15023a2 |