-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: fixed another minor bug with the order_by method
- Loading branch information
Showing
2 changed files
with
16 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,25 @@ | ||
from typing import Callable | ||
|
||
from pydantic.json_schema import SkipJsonSchema | ||
from sqlalchemy import asc, desc | ||
from sqlalchemy import String, asc, cast, desc | ||
from sqlalchemy.dialects.postgresql import UUID | ||
from sqlalchemy.sql.selectable import Select | ||
|
||
|
||
def order_by(order_by: str | SkipJsonSchema[None] = None) -> Callable[[Select], Select]: | ||
def _order_by(query: Select) -> Select: | ||
if order_by: | ||
direction = desc if order_by.startswith("-") else asc | ||
query = query.order_by(direction(order_by.lstrip("-"))) | ||
column_name = order_by.lstrip("-") | ||
|
||
# Get the column from the query | ||
for column in query.inner_columns: | ||
if column.key == column_name: | ||
# If the column is a UUID, cast it to a string | ||
if isinstance(column.type, UUID): | ||
column = cast(column, String) | ||
query = query.order_by(direction(column)) | ||
break | ||
return query | ||
|
||
return _order_by |