crouton streamlines CRUD operations in FastAPI applications by auto-generating endpoints for SQLAlchemy models, reducing the need for repetitive code and improving development speed.
By integrating Pydantic for data validation, Crouton ensures type safety and consistent data handling, allowing developers to focus on application logic instead of boilerplate code.
To install Crouton libraries. Run the following command:
pip install crouton
pip install crouton-client
pip install crouton --pre
pip install crouton-client --pre
Below is a simple example of what the CRUDRouter can do. In just ten lines of code, you can generate all the crud routes you need for any model. A full list of the routes generated can be found here.
from sqlalchemy import Column, String, Float, Integer
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from pydantic import BaseModel
from fastapi import FastAPI
from crouton import SQLAlchemyCRUDRouter
app = FastAPI()
engine = create_engine(
"sqlite:///./app.db",
connect_args={"check_same_thread": False}
)
SessionLocal = sessionmaker(
autocommit=False,
autoflush=False,
bind=engine
)
Base = declarative_base()
def get_db():
session = SessionLocal()
try:
yield session
session.commit()
finally:
session.close()
class PotatoCreate(BaseModel):
thickness: float
mass: float
color: str
type: str
class Potato(PotatoCreate):
id: int
class Config:
orm_mode = True
class PotatoModel(Base):
__tablename__ = 'potatoes'
id = Column(Integer, primary_key=True, index=True)
thickness = Column(Float)
mass = Column(Float)
color = Column(String)
type = Column(String)
Base.metadata.create_all(bind=engine)
router = SQLAlchemyCRUDRouter(
schema=Potato,
create_schema=PotatoCreate,
db_model=PotatoModel,
db=get_db,
prefix='potato'
)
app.include_router(router)