File tree Expand file tree Collapse file tree 5 files changed +63
-0
lines changed
Expand file tree Collapse file tree 5 files changed +63
-0
lines changed File renamed without changes.
Original file line number Diff line number Diff line change 1+ # SQLAlchemy
2+
3+ A minimal example of using the SQLAlchemy integration can be seen below:
4+
5+ ``` python
6+ from sqlalchemy import select
7+ from fastapi_pagination.ext.sqlalchemy import paginate
8+
9+
10+ @app.get (' /users' , response_model = Page[UserOut])
11+ def get_users (db : Session = Depends(get_db)):
12+ return paginate(db, select(User).order_by(User.created_at))
13+ ```
14+
15+ ## Scalar column
16+
17+ If you want to paginate a scalar column and return non-scalar value, then you will need to use ` transformer `
18+ ``` python
19+ from sqlalchemy import select
20+ from fastapi_pagination.ext.sqlalchemy import paginate
21+ from pydantic import BaseModel
22+
23+
24+ class UserNameOut (BaseModel ):
25+ name: str
26+
27+
28+ @app.get (' /users' , response_model = Page[UserNameOut])
29+ def get_user_names (db : Session = Depends(get_db)):
30+ return paginate(
31+ db,
32+ select(User.name).order_by(User.created_at),
33+ transformer = lambda items : [{" name" : name} for name in items],
34+ )
35+ ```
Original file line number Diff line number Diff line change 1+ # Items Transformer
2+
3+ If you want to transform items before passing it to schema validation stage,
4+ you can use ` transformer ` argument of ` paginate ` function.
5+
6+ ``` py hl_lines="13"
7+ {! ../ docs_src/ tutorial/ items_transformer.py !}
8+ ```
9+
10+ ` transformer ` argument is a function that accepts a list of items and returns a list of items that will be passed to
11+ page schema validation stage.
Original file line number Diff line number Diff line change 1+ from fastapi import FastAPI
2+
3+ from fastapi_pagination import Page , add_pagination , paginate
4+
5+ app = FastAPI ()
6+ add_pagination (app )
7+
8+
9+ @app .get ("/double-nums" )
10+ def get_double_nums () -> Page [int ]:
11+ return paginate (
12+ [* range (1_000 )],
13+ transformer = lambda x : [i * 2 for i in x ],
14+ )
Original file line number Diff line number Diff line change 2828 - " Limit Offset Pagination " : tutorials/limit-offset-pagination.md
2929 - " Links in Pagination " : tutorials/links-pagination.md
3030 - " Cursor Pagination " : tutorials/cursor-pagination.md
31+ - " Items Transformer " : tutorials/items-transformer.md
3132 - " Advanced User Guide " :
3233 - " Custom Params " : tutorials_advanced/custom-params.md
3334 - " Custom Page " : tutorials_advanced/custom-page.md
3435 - " Custom Pagination Function " : tutorials_advanced/custom-paginate.md
3536 - " Integrations " :
37+ - " Integrations " : integrations/integrations.md
3638 - " Available Integrations " : integrations/available-integrations.md
39+ - " SQLAlchemy " : integrations/sqlalchemy.md
3740 - " Contributing " : contributing.md
3841 - " License " : license.md
3942
You can’t perform that action at this time.
0 commit comments