mirror of
https://github.com/d3vyce/fastapi-toolsets.git
synced 2026-03-01 17:00:48 +01:00
Add examples in documentations (#99)
* docs: fix crud * docs: update README features * docs: add pagination/search example * docs: update zensical.toml * docs: cleanup * docs: update status to Stable + update description * docs: add example run commands
This commit is contained in:
0
docs_src/__init__.py
Normal file
0
docs_src/__init__.py
Normal file
0
docs_src/examples/__init__.py
Normal file
0
docs_src/examples/__init__.py
Normal file
0
docs_src/examples/pagination_search/__init__.py
Normal file
0
docs_src/examples/pagination_search/__init__.py
Normal file
6
docs_src/examples/pagination_search/app.py
Normal file
6
docs_src/examples/pagination_search/app.py
Normal file
@@ -0,0 +1,6 @@
|
||||
from fastapi import FastAPI
|
||||
|
||||
from .routes import router
|
||||
|
||||
app = FastAPI()
|
||||
app.include_router(router=router)
|
||||
19
docs_src/examples/pagination_search/crud.py
Normal file
19
docs_src/examples/pagination_search/crud.py
Normal file
@@ -0,0 +1,19 @@
|
||||
from fastapi_toolsets.crud import CrudFactory
|
||||
|
||||
from .models import Article, Category
|
||||
|
||||
ArticleCrud = CrudFactory(
|
||||
model=Article,
|
||||
cursor_column=Article.created_at,
|
||||
searchable_fields=[ # default fields for full-text search
|
||||
Article.title,
|
||||
Article.body,
|
||||
(Article.category, Category.name),
|
||||
],
|
||||
facet_fields=[ # fields exposed as filter dropdowns
|
||||
Article.status,
|
||||
(Article.category, Category.name),
|
||||
],
|
||||
)
|
||||
|
||||
ArticleFilters = ArticleCrud.filter_params()
|
||||
17
docs_src/examples/pagination_search/db.py
Normal file
17
docs_src/examples/pagination_search/db.py
Normal file
@@ -0,0 +1,17 @@
|
||||
from typing import Annotated
|
||||
|
||||
from fastapi import Depends
|
||||
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine
|
||||
|
||||
from fastapi_toolsets.db import create_db_context, create_db_dependency
|
||||
|
||||
DATABASE_URL = "postgresql+asyncpg://postgres:postgres@localhost:5432/postgres"
|
||||
|
||||
engine = create_async_engine(url=DATABASE_URL, future=True)
|
||||
async_session_maker = async_sessionmaker(bind=engine, expire_on_commit=False)
|
||||
|
||||
get_db = create_db_dependency(session_maker=async_session_maker)
|
||||
get_db_context = create_db_context(session_maker=async_session_maker)
|
||||
|
||||
|
||||
SessionDep = Annotated[AsyncSession, Depends(get_db)]
|
||||
36
docs_src/examples/pagination_search/models.py
Normal file
36
docs_src/examples/pagination_search/models.py
Normal file
@@ -0,0 +1,36 @@
|
||||
import datetime
|
||||
import uuid
|
||||
|
||||
from sqlalchemy import Boolean, DateTime, ForeignKey, String, Text, func
|
||||
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column, relationship
|
||||
|
||||
|
||||
class Base(DeclarativeBase):
|
||||
pass
|
||||
|
||||
|
||||
class Category(Base):
|
||||
__tablename__ = "categories"
|
||||
|
||||
id: Mapped[uuid.UUID] = mapped_column(primary_key=True, default=uuid.uuid4)
|
||||
name: Mapped[str] = mapped_column(String(64), unique=True)
|
||||
|
||||
articles: Mapped[list["Article"]] = relationship(back_populates="category")
|
||||
|
||||
|
||||
class Article(Base):
|
||||
__tablename__ = "articles"
|
||||
|
||||
id: Mapped[uuid.UUID] = mapped_column(primary_key=True, default=uuid.uuid4)
|
||||
created_at: Mapped[datetime.datetime] = mapped_column(
|
||||
DateTime(timezone=True), server_default=func.now()
|
||||
)
|
||||
title: Mapped[str] = mapped_column(String(256))
|
||||
body: Mapped[str] = mapped_column(Text)
|
||||
status: Mapped[str] = mapped_column(String(32))
|
||||
published: Mapped[bool] = mapped_column(Boolean, default=False)
|
||||
category_id: Mapped[uuid.UUID | None] = mapped_column(
|
||||
ForeignKey("categories.id"), nullable=True
|
||||
)
|
||||
|
||||
category: Mapped["Category | None"] = relationship(back_populates="articles")
|
||||
45
docs_src/examples/pagination_search/routes.py
Normal file
45
docs_src/examples/pagination_search/routes.py
Normal file
@@ -0,0 +1,45 @@
|
||||
from fastapi import APIRouter, Depends, Query
|
||||
|
||||
from fastapi_toolsets.schemas import PaginatedResponse
|
||||
|
||||
from .crud import ArticleCrud
|
||||
from .db import SessionDep
|
||||
from .schemas import ArticleRead
|
||||
|
||||
router = APIRouter(prefix="/articles")
|
||||
|
||||
|
||||
@router.get("/offset")
|
||||
async def list_articles_offset(
|
||||
session: SessionDep,
|
||||
page: int = Query(1, ge=1),
|
||||
items_per_page: int = Query(20, ge=1, le=100),
|
||||
search: str | None = None,
|
||||
filter_by: dict[str, list[str]] = Depends(ArticleCrud.filter_params()),
|
||||
) -> PaginatedResponse[ArticleRead]:
|
||||
return await ArticleCrud.offset_paginate(
|
||||
session=session,
|
||||
page=page,
|
||||
items_per_page=items_per_page,
|
||||
search=search,
|
||||
filter_by=filter_by or None,
|
||||
schema=ArticleRead,
|
||||
)
|
||||
|
||||
|
||||
@router.get("/cursor")
|
||||
async def list_articles_cursor(
|
||||
session: SessionDep,
|
||||
cursor: str | None = None,
|
||||
items_per_page: int = Query(20, ge=1, le=100),
|
||||
search: str | None = None,
|
||||
filter_by: dict[str, list[str]] = Depends(ArticleCrud.filter_params()),
|
||||
) -> PaginatedResponse[ArticleRead]:
|
||||
return await ArticleCrud.cursor_paginate(
|
||||
session=session,
|
||||
cursor=cursor,
|
||||
items_per_page=items_per_page,
|
||||
search=search,
|
||||
filter_by=filter_by or None,
|
||||
schema=ArticleRead,
|
||||
)
|
||||
13
docs_src/examples/pagination_search/schemas.py
Normal file
13
docs_src/examples/pagination_search/schemas.py
Normal file
@@ -0,0 +1,13 @@
|
||||
import datetime
|
||||
import uuid
|
||||
|
||||
from fastapi_toolsets.schemas import PydanticBase
|
||||
|
||||
|
||||
class ArticleRead(PydanticBase):
|
||||
id: uuid.UUID
|
||||
created_at: datetime.datetime
|
||||
title: str
|
||||
status: str
|
||||
published: bool
|
||||
category_id: uuid.UUID | None
|
||||
Reference in New Issue
Block a user