0.11.0
0.11.0 Summary
🚀Features
- replaced CRUDBase with fastcrud for more robust operations
- worker script refactored, thanks @AlessioBugetti
- print statements removed, thanks @shreyasSarve
- PGAdmin container for PostgreSQL administration and debugging, thanks @gmirsky
create_tables_on_start
parameter added in setup functionruff
added aspre-commit
, thanks @luca-medeiros
📝Docs
- all docs updated to reflect changes
- pull request template added
- Contributing moved to its own file and enhanced
- Issue template added
- Code of conduct added
1. fastcrud
Set Up FastAPI and FastCRUD
from fastapi import FastAPI
from fastcrud import FastCRUD, crud_router
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine
from sqlalchemy.orm import sessionmaker
# Database setup (Async SQLAlchemy)
DATABASE_URL = "sqlite+aiosqlite:///./test.db"
engine = create_async_engine(DATABASE_URL, echo=True)
async_session = sessionmaker(engine, class_=AsyncSession, expire_on_commit=False)
# FastAPI app
app = FastAPI()
# CRUD operations setup
crud = FastCRUD(Item)
# CRUD router setup
item_router = crud_router(
session=async_session,
model=Item,
crud=crud,
create_schema=ItemCreateSchema,
update_schema=ItemUpdateSchema,
path="/items",
tags=["Items"]
)
app.include_router(item_router)
Using FastCRUD in User-Defined FastAPI Endpoints
For more control over your endpoints, you can use FastCRUD directly within your custom FastAPI route functions. Here's an example:
Usage:
from fastapi import FastAPI, Depends, HTTPException
from sqlalchemy.ext.asyncio import AsyncSession
from fastcrud import FastCRUD
from yourapp.models import Item
from yourapp.schemas import ItemCreateSchema, ItemUpdateSchema
app = FastAPI()
# Assume async_session is already set up as per the previous example
# Instantiate FastCRUD with your model
item_crud = FastCRUD(Item)
@app.post("/custom/items/")
async def create_item(item_data: ItemCreateSchema, db: AsyncSession = Depends(async_session)):
return await item_crud.create(db, item_data)
@app.get("/custom/items/{item_id}")
async def read_item(item_id: int, db: AsyncSession = Depends(async_session)):
item = await item_crud.get(db, id=item_id)
if not item:
raise HTTPException(status_code=404, detail="Item not found")
return item
# You can add more routes for update and delete operations in a similar fashion
To know all available methods, check it in fastcrud readme.
2. create_tables_on_start
If you want to stop tables from being created every time you run the api, you should disable this here:
# app/main.py
from .api import router
from .core.config import settings
from .core.setup import create_application
# create_tables_on_start defaults to True
app = create_application(router=router, settings=settings, create_tables_on_start=False)
🔎Bug fixes
- pyproject.toml fixed, thanks @DmitryIo
- get task endpoint bug fixed
- deprecated typing classes replaced, thanks @eredden
What's Changed
- Add ruff linter by @luca-medeiros in #83
- Added the comment and in the docs to change docker-compose.yml for migrations by @igorbenav in #85
- create_tables_on_start created, docs added by @igorbenav in #86
- Add PGAdmin container for PostgreSQL administration and debugging by @gmirsky in #75
- Removed deprecated typing classes and replaced with native Python dict, list, type, etc. by @eredden in #90
- add pre-commit configs by @luca-medeiros in #92
- Remove all print statements #93 by @shreyasSarve in #95
- Move worker script by @AlessioBugetti in #97
- Fix for get task endpoint bug by @igorbenav in #99
- Fix of pyproject.toml by @DmitryIo in #101
- Create CODE_OF_CONDUCT.md by @igorbenav in #102
- Update issue templates by @igorbenav in #103
- Create CONTRIBUTING.md by @igorbenav in #104
- Update CONTRIBUTING.md by @igorbenav in #105
- Create PULL_REQUEST_TEMPLATE.md by @igorbenav in #106
- Update README.md by @igorbenav in #107
- Fast crud by @igorbenav in #109
- Update README.md by @igorbenav in #110
New Contributors
- @gmirsky made their first contribution in #75
- @eredden made their first contribution in #90
- @shreyasSarve made their first contribution in #95
- @AlessioBugetti made their first contribution in #97
- @DmitryIo made their first contribution in #101
Full Changelog: v0.10.0...v0.11.0