Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## Version 5.2.2

* Add parameter sorting result for `read_filtered_list`.

## Version 5.2.1

* Fix count for paginate.
Expand Down
12 changes: 6 additions & 6 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,16 @@ license = {file = "LICENSE"}
name = "DB-First"
readme = "README.md"
requires-python = ">=3.11"
version = "5.2.1"
version = "5.2.2"

[project.optional-dependencies]
dev = [
"build==1.4.0",
"psycopg[binary]==3.3.2",
"build==1.4.3",
"psycopg[binary]==3.3.3",
"pre-commit==4.5.1",
"pytest==9.0.2",
"pytest-cov==7.0.0",
"python-dotenv==1.2.1",
"pytest==9.0.3",
"pytest-cov==7.1.0",
"python-dotenv==1.2.2",
"tox==4.34.1",
"twine==6.2.0"
]
Expand Down
13 changes: 12 additions & 1 deletion src/db_first/dbal/sqla.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from typing import Any
from typing import get_args
from typing import Literal

from db_first.dbal.exceptions import DBALCreateException
from db_first.dbal.exceptions import DBALObjectNotFoundException
Expand Down Expand Up @@ -78,7 +79,9 @@ def read_filtered(self, **kwargs) -> M:
except NoResultFound as e:
raise DBALObjectNotFoundException(repr(e))

def read_filtered_list(self, **kwargs) -> Sequence[M]:
def read_filtered_list(
self, sort_order: Literal['asc', 'desc'] = 'asc', sort_field: str | None = None, **kwargs
) -> Sequence[M]:
filters = []
for k, v in kwargs.items():
if isinstance(v, list):
Expand All @@ -87,6 +90,14 @@ def read_filtered_list(self, **kwargs) -> Sequence[M]:
filters.append(getattr(self._model, k) == v)

stmt = select(self._model).where(*filters)

if sort_field:
if sort_order == 'desc':
order_column = getattr(self._model, sort_field).desc()
else:
order_column = getattr(self._model, sort_field)
stmt = stmt.order_by(order_column)

return self._session.scalars(stmt).all()

def update(self, id: Any, **data) -> M:
Expand Down
25 changes: 25 additions & 0 deletions tests/dbal/test_dbal.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,3 +155,28 @@ def test_dbal__read_filtered_list(fx_db, fx_parent_dbal):
result = fx_parent_dbal(session_db).read_filtered_list(first=new_3.first, second=None)

assert result == [new_3]


def test_dbal__read_filtered_list_with_sort(fx_db, fx_parent_dbal):
session_db, parents_model, _, _ = fx_db

new_1 = fx_parent_dbal(session_db).create(**{'first': next(UNIQUE_STRING)})
new_2 = fx_parent_dbal(session_db).create(**{'first': next(UNIQUE_STRING)})
new_3 = fx_parent_dbal(session_db).create(**{'first': next(UNIQUE_STRING)})
fx_parent_dbal(session_db).create(**{'first': new_3.first, 'second': 'Not None'})

result = fx_parent_dbal(session_db).read_filtered_list(first=[new_1.first, new_2.first])

assert result == [new_1, new_2]

result = fx_parent_dbal(session_db).read_filtered_list(
first=[new_1.first, new_2.first], sort_order='asc', sort_field='first'
)

assert result == [new_1, new_2]

result = fx_parent_dbal(session_db).read_filtered_list(
first=[new_1.first, new_2.first], sort_order='desc', sort_field='first'
)

assert result == [new_2, new_1]
Loading