Skip to main content

A package for generating FastAPI CRUD operations and routes

Project description

CRUD FORGE

PyPI version Documentation Status

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.4

Quick Start

Here's how you can leverage CRUD FORGE's automatic generators:

from fastapi import FastAPI
from crud_forge.db import DatabaseManager
from crud_forge.models import generate_models_from_metadata
from crud_forge.crud import generate_crud
from crud_forge.routes import generate_metadata_routes, generate_default_routes

# Set up FastAPI app and database
app = FastAPI()
db_manager = DatabaseManager(db_url="postgresql://user:password@localhost/dbname")

# Generate default routes
generate_default_routes(app)

# Generate metadata routes
metadata_router = generate_metadata_routes(db_manager.metadata, db_manager.get_db)
app.include_router(metadata_router)

# Automatically generate models from database metadata
models = generate_models_from_metadata(db_manager.metadata)

# Generate CRUD routes for all models
for schema, schema_models in models.items():
    for table_name, (sqlalchemy_model, pydantic_model) in schema_models.items():
        generate_crud(sqlalchemy_model, pydantic_model, app, db_manager.get_db, tags=[f"{schema}_{table_name}"])

# 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

CRUD FORGE MAIN STRUCTURE

Detailed Usage

Database Connection

Use the DatabaseManager to set up your database connection:

from crud_forge.db import DatabaseManager

# Using a database URL
db_manager = DatabaseManager(db_url="postgresql://user:password@localhost/dbname")

# Or using individual parameters
db_manager = DatabaseManager(
    db_type="postgresql",
    user="user",
    password="password",
    host="localhost",D
    database="dbname"
)

Automatic Model Generation

Use generate_models_from_metadata to automatically create models:

from crud_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 crud_forge.crud import generate_crud

for schema, schema_models in models.items():
    for table_name, (sqlalchemy_model, pydantic_model) in schema_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 crud_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 database
  • GET /dt/{schema}/tables: List all tables in a specific schema
  • GET /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 crud_forge.routes import generate_default_routes

generate_default_routes(app)

This will create the following endpoints:

  • GET /: Root endpoint with basic API information
  • GET /health: Health check endpoint
  • GET /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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

crud_forge-0.1.5.tar.gz (14.2 kB view details)

Uploaded Source

Built Distribution

crud_forge-0.1.5-py3-none-any.whl (11.5 kB view details)

Uploaded Python 3

File details

Details for the file crud_forge-0.1.5.tar.gz.

File metadata

  • Download URL: crud_forge-0.1.5.tar.gz
  • Upload date:
  • Size: 14.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.3

File hashes

Hashes for crud_forge-0.1.5.tar.gz
Algorithm Hash digest
SHA256 946c312ca441f8ada7d43a491e69ed6ba415b81fa983edbf467536e2ebbec5c6
MD5 6317e8b86ee2973268b1d52ea1ec91eb
BLAKE2b-256 e6ed8400651a8801991ad4f9f435af6a5eff6d2b25bb26f311fc1b5f9c3de0c7

See more details on using hashes here.

File details

Details for the file crud_forge-0.1.5-py3-none-any.whl.

File metadata

  • Download URL: crud_forge-0.1.5-py3-none-any.whl
  • Upload date:
  • Size: 11.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.3

File hashes

Hashes for crud_forge-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 1848fe7ca2132dcf7525745af3c9d4f1169e06771ac21b8c98f5671812f51446
MD5 a1b591326a5d7c211ed15c93c62b39ec
BLAKE2b-256 39a0ad25ef504659668e40fb750b24b8cf73c584f34a2030f9423c20fa601df9

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page