add migrations, create domain and crud layers

This commit is contained in:
2026-10-26 21:59:40 +03:00
parent 76b34fca4b
commit 3082b84f61
27 changed files with 481 additions and 278 deletions
+5 -1
View File
@@ -15,7 +15,7 @@ T = TypeVar("T")
class Positioned(Protocol):
"""Всё, у чего есть позиция в порядке (created_at, id): ORM-модели и схемы."""
"""Объект с позицией в порядке (created_at, id)."""
@property
def id(self) -> UUID: ...
@@ -30,6 +30,7 @@ def encode_cursor(
created_at: datetime,
item_id: UUID,
) -> str:
"""Кодирует позицию и ресурс, к которому она привязана, в непрозрачный cursor."""
payload = {
"kind": kind,
"resource_id": str(resource_id),
@@ -41,6 +42,7 @@ def encode_cursor(
def decode_cursor(cursor: str, kind: str, resource_id: UUID) -> Position:
"""Возвращает позицию из cursor; битый или чужой cursor даёт 400."""
try:
payload = json.loads(base64.urlsafe_b64decode(cursor))
cursor_kind = payload["kind"]
@@ -61,6 +63,7 @@ def decode_cursor(cursor: str, kind: str, resource_id: UUID) -> Position:
def split_page(rows: list[T], limit: int) -> tuple[list[T], bool]:
"""Отделяет страницу от лишней строки limit + 1, по которой видно продолжение."""
has_next = len(rows) > limit
items = rows[:limit]
return items, has_next
@@ -72,6 +75,7 @@ def build_next_cursor(
items: Sequence[Positioned],
has_next: bool,
) -> str | None:
"""Возвращает cursor на последний элемент страницы или None, если она последняя."""
if not has_next:
return None