Skip to content

Commit

Permalink
perf: Update the Paginator class
Browse files Browse the repository at this point in the history
  • Loading branch information
amisadmin committed Dec 25, 2023
1 parent b090c10 commit 69b18da
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
4 changes: 2 additions & 2 deletions fastapi_amis_admin/crud/_sqlalchemy.py
Original file line number Diff line number Diff line change
Expand Up @@ -512,14 +512,14 @@ async def route(
data.filters = await self.on_filter_pre(request, filters)
if data.filters:
sel = sel.filter(*self.calc_filter_clause(data.filters))
if paginator.show_total:
if paginator.showTotal:
data.total = await self.db.async_scalar(sel.with_only_columns(func.count("*")))
if data.total == 0:
return BaseApiOut(data=data)
orderBy = self._calc_ordering(paginator.orderBy, paginator.orderDir)
if orderBy:
sel = sel.order_by(*orderBy)
sel = sel.limit(paginator.perPage).offset((paginator.page - 1) * paginator.perPage)
sel = sel.limit(paginator.perPage).offset(paginator.offset)
result = await self.db.async_execute(sel)
return BaseApiOut(data=await self.on_list_after(request, result, data))

Expand Down
18 changes: 16 additions & 2 deletions fastapi_amis_admin/crud/schema.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from enum import Enum
from typing import Any, Dict, Generic, List, Optional, TypeVar, Union
from warnings import warn

from fastapi_amis_admin.utils.pydantic import AllowExtraModelMixin, GenericModel

Expand Down Expand Up @@ -46,7 +47,7 @@ def __call__(
self,
page: Union[int, str] = 1,
perPage: Union[int, str] = None,
show_total: int = 1,
showTotal: bool = True,
orderBy: str = None,
orderDir: str = "asc",
):
Expand All @@ -56,7 +57,20 @@ def __call__(
self.perPage = perPage if perPage > 0 else self.perPageDefault
if self.perPageMax:
self.perPage = min(self.perPage, self.perPageMax)
self.show_total = show_total
self.showTotal = showTotal
self.orderBy = orderBy
self.orderDir = orderDir
return self

@property
def offset(self):
return (self.page - 1) * self.perPage

@property
def limit(self):
return self.perPage

@property
def show_total(self):
warn("show_total is deprecated, use showTotal instead", DeprecationWarning, stacklevel=1)
return self.showTotal

0 comments on commit 69b18da

Please sign in to comment.