Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add offset, limit and order_by parameters to CRUDMixin.get() method #168

Merged
merged 2 commits into from
Feb 1, 2025
Merged
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
17 changes: 12 additions & 5 deletions src/ouranos/core/database/models/abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
from typing import NamedTuple, Self, Sequence
from uuid import UUID

from sqlalchemy import and_, delete, insert, inspect, Select, select, update
from sqlalchemy import (
and_, delete, insert, inspect, Select, select, UnaryExpression, update)
from sqlalchemy.ext.asyncio import AsyncSession

from gaia_validators import missing
Expand Down Expand Up @@ -100,14 +101,20 @@ async def get(
cls,
session: AsyncSession,
/,
**lookup_keys: lookup_keys_type | None,
offset: int | None = None,
limit: int | None = None,
order_by: UnaryExpression | None = None,
**lookup_keys: list[lookup_keys_type] | lookup_keys_type | None,
) -> Self | None:
"""
:param offset: the offset from which to start looking
:param limit: the maximum number of rows to query
:param order_by: how to order the results
:param session: an AsyncSession instance
:param lookup_keys: a dict with table column names as keys and values
depending on the related column data type
"""
stmt = cls._generate_get_query(**lookup_keys)
stmt = cls._generate_get_query(offset, limit, order_by, **lookup_keys)
result = await session.execute(stmt)
return result.scalar_one_or_none()

Expand All @@ -118,7 +125,7 @@ async def get_multiple(
/,
offset: int | None = None,
limit: int | None = None,
order_by: str | None = None,
order_by: UnaryExpression | None = None,
**lookup_keys: list[lookup_keys_type] | lookup_keys_type | None,
) -> Sequence[Self]:
"""
Expand All @@ -129,7 +136,7 @@ async def get_multiple(
:param lookup_keys: a dict with table column names as keys and values
depending on the related column data type
"""
stmt = cls._generate_get_query(**lookup_keys)
stmt = cls._generate_get_query(offset, limit, order_by, **lookup_keys)
result = await session.execute(stmt)
return result.scalars().all()

Expand Down