ZenithModel - Clean and Type-Safe
# Creating a user - so many manual steps!from passlib.context import CryptContextfrom sqlalchemy.ext.asyncio import AsyncSessionfrom sqlalchemy import select, func
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
@app.post("/users")async def create_user( email: str, password: str, full_name: str, db: AsyncSession = Depends(get_db) # Manual dependency injection): # Manual password hashing hashed_password = pwd_context.hash(password)
# Manual existence check stmt = select(User).where(User.email == email) result = await db.execute(stmt) if result.first(): raise HTTPException(status_code=400, detail="Email already exists")
# Manual model creation user = User( email=email, hashed_password=hashed_password, full_name=full_name )
# Manual session management db.add(user) try: await db.commit() await db.refresh(user) except Exception as e: await db.rollback() raise HTTPException(status_code=500, detail="Database error")
return user
# Finding users - verbose and error-prone@app.get("/users/{user_id}")async def get_user(user_id: int, db: AsyncSession = Depends(get_db)): stmt = select(User).where(User.id == user_id) result = await db.execute(stmt) user = result.scalar_one_or_none()
if not user: raise HTTPException(status_code=404, detail="User not found")
return user
# Pagination - complex and repetitive@app.get("/users")async def list_users( skip: int = 0, limit: int = 100, db: AsyncSession = Depends(get_db)): # Count query for pagination count_stmt = select(func.count()).select_from(User) total_result = await db.execute(count_stmt) total = total_result.scalar()
# Data query query = select(User).where(User.is_active == True) query = query.offset(skip).limit(limit) result = await db.execute(query) users = result.scalars().all()
return {"users": users, "total": total, "skip": skip, "limit": limit}
# This is just 3 endpoints and already ~80 lines!# Imagine multiply this by every model in your app...