0.4.0
0.4.0 Summary
🚀Features
- For the
get_multi
method we now get a pythondict
with full support to pagination:
{
"data": [
{
"id": 4,
"name": "User Userson",
"username": "userson4",
"email": "[email protected]",
"profile_image_url": "https://profileimageurl.com"
},
{
"id": 5,
"name": "User Userson",
"username": "userson5",
"email": "[email protected]",
"profile_image_url": "https://profileimageurl.com"
}
],
"total_count": 2,
"has_more": false,
"page": 1,
"items_per_page": 10
}
Warning
What's retrieved from the get and get multi methods is no longer asqlalchemy.engine.row.Row
, is a pythondict
instead.
- To fully use it, you may use the
PaginatedListResponse
as your response_model:
@router.get("/users", response_model=PaginatedListResponse[UserRead])
async def read_users(
request: Request,
db: Annotated[AsyncSession, Depends(async_get_db)],
page: int = 1,
items_per_page: int = 10
):
users_data = await crud_users.get_multi(
db=db,
offset=(page - 1) * items_per_page,
limit=items_per_page,
schema_to_select=UserRead,
is_deleted=False
) # this returns a python dict
return {
"data": users_data["data"],
"total_count": users_data["total_count"],
"has_more": (page * items_per_page) < users_data["total_count"],
"page": page,
"items_per_page": items_per_page
}
-
There's also a less powerful ListResponse.
-
You can also get the count of a certain object with the specified filter:
# Here I'm getting the count of users with the name 'User Userson'
user = await crud_users.count(
db=db,
name="User Userson"
)
Indexes
were added to relevant fields in pydantic for faster count
🚚Migration
- every result to a get or get_multi query now a python
dict
, so instead of:
# BAD
db_user = crud_user.get(db=db, username=username)
db_user.id
You should do:
# GOOD
db_user = crud_user.get(db=db, username=username)
db_user["id"]
- Now using
jsonable_encoder
instead of custom function incache
field_serializer
in date fields
🔎Bug fixes
- ports added to db in docker-compose.yml
- alembic .env now getting the right database URL for docker compose
- gunicorn installed
What's Changed
- 34 dont return arrays as top level responses by @igorbenav in #35
Full Changelog: v0.3.3...v0.4.0