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
- GET /users: Retrieve all users.
- GET /users/{id}: Retrieve a user by ID.
- POST /users: Create a new user.
- PUT /users/{id}: Update an existing user.
- 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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file simplefastcrud-0.1.4.tar.gz.
File metadata
- Download URL: simplefastcrud-0.1.4.tar.gz
- Upload date:
- Size: 8.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c885ccd5f24579d9dd06f5b5aa4aad0f8ce1852827e78e37d10be6c1ce43edb7
|
|
| MD5 |
46e9d09b5631ce020a06951987b99723
|
|
| BLAKE2b-256 |
bc4a2cc8f6798ad2196b321b39ac1935abb914a89a1809109219d9cc2ad7a800
|
File details
Details for the file simplefastcrud-0.1.4-py3-none-any.whl.
File metadata
- Download URL: simplefastcrud-0.1.4-py3-none-any.whl
- Upload date:
- Size: 7.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
00acfe7e5931b0d103e151fb4f52eb0e974bfec30c0f2528d30420c20965b493
|
|
| MD5 |
b1f4a4ee85e76224a06f3f0cdb84056f
|
|
| BLAKE2b-256 |
2c0ba07f0b023ccbf840c55738c1e11652ab140f60fb60ad6d2251b5b0f5066e
|