Skip to main content

A package for generating FastAPI CRUD operations and routes

Project description

CRUD FORGE

PyPI version Documentation Status

CRUD FORGE is a powerful Python package that simplifies the creation of CRUD (Create, Read, Update, Delete) operations for FastAPI applications. It automatically generates API routes and database models based on your SQLAlchemy models, saving you time and reducing boilerplate code.

Features

  • Automatic generation of CRUD routes for FastAPI
  • Support for SQLAlchemy models and Pydantic schemas
  • Flexible database connection management
  • Easy-to-use API for creating models and routes
  • Customizable route generation
  • Built-in filtering for GET requests
  • Comprehensive error handling
  • Type hinting for better IDE support

Installation

Install FastAPI CRUD Generator using pip:

pip install crud-forge

Quick Start

Here's a simple example of how to use FastAPI CRUD Generator:

from fastapi import FastAPI
from sqlalchemy import Column, Integer, String
from sqlalchemy.ext.declarative import declarative_base
from pydantic import BaseModel
from crud_forge.generators import generate_crud
from crud_forge.db import DatabaseManager

# Define your SQLAlchemy model
Base = declarative_base()

class User(Base):
    __tablename__ = "users"
    id = Column(Integer, primary_key=True, index=True)
    name = Column(String, index=True)
    email = Column(String, unique=True, index=True)

# Define your Pydantic model
class UserSchema(BaseModel):
    id: int
    name: str
    email: str

    class Config:
        orm_mode = True

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

# Generate CRUD routes
generate_crud(User, UserSchema, app, db_manager.get_db)

# Run the app
if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=8000)

Package Structure

CRUD FORGE

This example sets up a simple User model and generates CRUD routes for it.

Usage

Setting up the Database Connection

Use the DatabaseManager class to manage your database connection:

from crud_forge.db import DatabaseManager

db_manager = DatabaseManager(
    user="myuser",
    password="mypassword",
    host="localhost",
    database="mydatabase"
)

Or provide a complete database URL:

db_manager = DatabaseManager(db_url="postgresql://user:password@localhost/dbname")

Generating Models

Use the generators.models module to generate SQLAlchemy & Pydantic models from your database:

from crud_forge.generators.models import generate_models, generate_views
from crud_forge.db import DatabaseManager

db_manager = DatabaseManager(db_url="postgresql://user:password@localhost/dbname")

store_models = generate_models(db_manager.engine, ['store'])
store_views = generate_views(db_manager.engine, ['store'])

Creating CRUD Routes

Use the create_crud_routes function to automatically generate CRUD routes for your models:

from crud_forge.generators.crud import generate_crud

generate_crud(sqlalchemy_model, pydantic_model, router, db_dependency)

This will create the following routes:

  • GET /users/columns: Get all columns of the User model
  • POST /users: Create a new user
  • GET /users: Get all users (with optional filtering)
  • PUT /users: Update users (with filtering)
  • DELETE /users: Delete users (with filtering)

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

FastAPI CRUD Generator 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.1.tar.gz (12.9 kB view details)

Uploaded Source

Built Distribution

crud_forge-0.1.1-py3-none-any.whl (10.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: crud_forge-0.1.1.tar.gz
  • Upload date:
  • Size: 12.9 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.1.tar.gz
Algorithm Hash digest
SHA256 a21de4c3de0c0bb667ca2b06b8d2fc5ffcf8f21f26721e1eeba466f5dfb3ad18
MD5 c15d08f8835e836d954ba2cb7a3d73f5
BLAKE2b-256 8a98d580617907065fbc31f1948ffb9f268ccef99bc2b15fc7107b47a43958db

See more details on using hashes here.

File details

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

File metadata

  • Download URL: crud_forge-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 10.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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 41b417fee07d4e26c6220d2f0b86db5cdc6c4264b85eb7fbf4f06e9ab8b342c9
MD5 1e71db1c184836809949246d2632d828
BLAKE2b-256 a1721215125d8b100f43d8ffc2b4c0bc44af52015880027647c79f79f33033f8

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