11from datetime import date
2- from typing import Any , Iterator , Dict
2+ from pathlib import Path
3+ from typing import Any , Iterator , Dict , TypeVar
34
45from faker import Faker
56from fastapi import Depends , FastAPI , Request
89from sqlmodel import Field , Session , SQLModel , create_engine , select
910
1011from fastapi_pagination import pagination_ctx , resolve_params
11- from fastapi_pagination .cursor import CursorPage
12+ from fastapi_pagination .cursor import CursorPage as BaseCursorPage
13+ from fastapi_pagination .customization import CustomizedPage , UseIncludeTotal , UseParamsFields
1214from fastapi_pagination .ext .sqlmodel import paginate
1315
16+ ROOT = Path (__file__ ).parent
17+
1418fake = Faker ()
1519
1620app = FastAPI ()
17- templates = Jinja2Templates (directory = "templates" )
21+ templates = Jinja2Templates (directory = ROOT / "templates" )
1822
1923engine = create_engine ("sqlite:///.db" )
2024
21- CursorPage = CursorPage .with_custom_options (size = 10 )
25+
26+ T = TypeVar ("T" )
27+
28+ CursorPage = CustomizedPage [
29+ BaseCursorPage [T ],
30+ UseIncludeTotal (True ),
31+ UseParamsFields (size = 10 ),
32+ ]
2233
2334
2435class User (SQLModel , table = True ):
@@ -40,7 +51,7 @@ def get_db() -> Iterator[Session]:
4051def user_data (id_ : int ) -> Dict [str , Any ]:
4152 return {
4253 "id" : id_ + 1 ,
43- "profile_pic" : f"https://avatars .dicebear.com/api /croodles/{ id_ } .svg " ,
54+ "profile_pic" : f"https://api .dicebear.com/8.x /croodles/svg?seed= { id_ + 1 } " ,
4455 "first_name" : fake .first_name (),
4556 "last_name" : fake .last_name (),
4657 "email" : fake .email (),
@@ -53,8 +64,10 @@ def on_startup():
5364 User .metadata .drop_all (engine )
5465 User .metadata .create_all (engine )
5566
67+ total = fake .pyint (100 , 200 )
68+
5669 with Session (engine ) as session :
57- session .add_all ([User (** user_data (i )) for i in range (100 )])
70+ session .add_all ([User (** user_data (i )) for i in range (total )])
5871 session .commit ()
5972
6073
0 commit comments