Skip to content

Commit

Permalink
readme
Browse files Browse the repository at this point in the history
  • Loading branch information
hadrien committed Apr 8, 2021
1 parent 36b0a49 commit fe3ebc6
Showing 1 changed file with 16 additions and 18 deletions.
34 changes: 16 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,24 +96,25 @@ def run_bg():

```python
from fastapi import APIRouter, Depends
from fastapi_sqla import Base, Page, Paginate, Session
from fastapi_sqla import Base, Page, Paginate
from pydantic import BaseModel
from sqlalchemy import select

router = APIRouter()


class UserEntity(Base):
class User(Base):
__tablename__ = "user"


class User(BaseModel):
class UserModel(BaseModel):
id: int
name: str


@router.get("/users", response_model=Page[User])
def all_users(session: Session = Depends(), paginate: Paginate = Depends()):
query = session.query(UserEntity)
@router.get("/users", response_model=Page[UserModel])
def all_users(paginate: Paginate = Depends()):
query = select(User)
return paginate(query)
```

Expand All @@ -134,23 +135,23 @@ To customize pagination, create a dependency using `fastapi_sqla.Pagination`
from fastapi import APIRouter, Depends
from fastapi_sqla import Base, Page, Pagination, Session
from pydantic import BaseModel
from sqlalchemy import func
from sqlalchemy.orm import Query
from sqlalchemy import func, select
from sqlalchemy.sql import Select

router = APIRouter()


class UserEntity(Base):
class User(Base):
__tablename__ = "user"


class User(BaseModel):
class UserModel(BaseModel):
id: int
name: str


def query_count(session: Session, query: Query):
return query.statement.with_only_columns([func.count()]).scalar()
def query_count(session: Session, query: Select) -> int:
return session.execute(select(func.count()).select_from(User)).scalar()


Paginate = Pagination(
Expand All @@ -160,12 +161,9 @@ Paginate = Pagination(
)


@router.get("/users", response_model=Page[User])
def all_users(
session: Session = Depends(),
paginate: Paginate = Depends(),
):
query = session.query(UserEntity)
@router.get("/users", response_model=Page[UserModel])
def all_users(paginate: Paginate = Depends()):
query = select(User)
return paginate(query)
```

Expand Down

0 comments on commit fe3ebc6

Please sign in to comment.