mirror of
https://github.com/creyD/creyPY.git
synced 2026-04-12 19:30:30 +02:00
feat: added order_by method
This commit is contained in:
14
creyPY/fastapi/order_by.py
Normal file
14
creyPY/fastapi/order_by.py
Normal file
@@ -0,0 +1,14 @@
|
||||
from typing import Callable, Optional
|
||||
|
||||
from sqlalchemy import asc, desc
|
||||
from sqlalchemy.sql.selectable import Select
|
||||
|
||||
|
||||
def order_by(order_by: Optional[str] = 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("-")))
|
||||
return query
|
||||
|
||||
return _order_by
|
||||
Reference in New Issue
Block a user