Skip to main content

SimpleFastCrud is a tool that simplifies the creation of dynamic CRUD endpoints in FastAPI using SQLAlchemy and Pydantic.

Project description

SimpleFastCrud

SimpleFastCrud is a tool that simplifies the creation of dynamic CRUD endpoints in FastAPI using SQLAlchemy and Pydantic. This package automatically generates schemas and endpoints for SQLAlchemy models.

Installation

pip install SimpleFastCrud

Basic Usage

Initial Setup

from fastapi import FastAPI, Depends, APIRouter
from sqlalchemy.orm import Session
from SimpleFastCrud.crud import SimpleFastCrud
from your_project.database import get_db  # Function to get the database session
from your_project.models import YourModel  # SQLAlchemy model

app = FastAPI()
api_router = APIRouter()
crud = SimpleCrud()

crud.add(
    model=YourModel,
    api_router=api_router,
    get_db=get_db
)

app.include_router(api_router)

Complete Example

SQLAlchemy Model

from sqlalchemy import Column, Integer, String
from sqlalchemy.ext.declarative import declarative_base

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)

FastAPI Configuration

from fastapi import FastAPI, Depends, APIRouter
from sqlalchemy.orm import Session
from SimpleFastCrud.crud import SimpleFastCrud
from your_project.database import get_db
from your_project.models import User

app = FastAPI()
api_router = APIRouter()
crud = SimpleCrud()

crud.add(
    model=User,
    api_router=api_router,
    get_db=get_db
)

app.include_router(api_router)

Generated Endpoints

  1. GET /users: Retrieve all users.
  2. GET /users/{id}: Retrieve a user by ID.
  3. POST /users: Create a new user.
  4. PUT /users/{id}: Update an existing user.
  5. DELETE /users/{id}: Delete a user.

Customization

Adding Authentication Dependencies

from fastapi import Depends
from your_project.auth import get_current_user

crud.add(
    model=User,
    api_router=api_router,
    get_db=get_db,
    auth_dep=Depends(get_current_user)
)

Filters and Pagination

crud.add(
    model=User,
    api_router=api_router,
    get_db=get_db,
    filter_param='tenant_id',
    pagination=True,
    steps=20
)

Contributions

Contributions are welcome! Please open an issue or a pull request in the project repository.

License

This project is licensed under the MIT License.

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

simplefastcrud-0.1.3.tar.gz (4.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

simplefastcrud-0.1.3-py3-none-any.whl (3.6 kB view details)

Uploaded Python 3

File details

Details for the file simplefastcrud-0.1.3.tar.gz.

File metadata

  • Download URL: simplefastcrud-0.1.3.tar.gz
  • Upload date:
  • Size: 4.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.12

File hashes

Hashes for simplefastcrud-0.1.3.tar.gz
Algorithm Hash digest
SHA256 69a0f60028c0b8bb367b7f304b9ab55477237d44b2edc0149472800e1228dee0
MD5 599842bab2ae72cad4e85c76aa14fb1b
BLAKE2b-256 4341c49f1dab41e1d5e9ebedafdcfe6b8e628deeddc052acc2e4c0dc7c76537c

See more details on using hashes here.

File details

Details for the file simplefastcrud-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: simplefastcrud-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 3.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.12

File hashes

Hashes for simplefastcrud-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 353ab9f2b3643860cb6b5506b424aa2e2a64b3723cda143771c677d637c104e6
MD5 c4b61cef62356e3107fb62d8f90356ad
BLAKE2b-256 09a4120eb1aa529ad0f05749087b57c26f8747ab6f54dab6048fa7b1d1d73d65

See more details on using hashes here.

Supported by

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