Download src/core/sync/models.py from anaselamr/yaz: direct link, hf CLI and curl.
- Browser
- Download file 3.94 kB
-
https://huggingface.co/spaces/anaselamr/yaz/resolve/main/src/core/sync/models.py
- Command line
-
hf download hf://spaces/anaselamr/yaz/src/core/sync/models.py
-
curl -L -o models.py https://huggingface.co/spaces/anaselamr/yaz/resolve/main/src/core/sync/models.py
3.94 kB
| """ | |
| Core Sync — SQLModel Tables | |
| ============================= | |
| SyncOperation: a queued mutation from an offline or delayed client. | |
| ConflictRecord: when server and client diverge, both versions are stored | |
| for user-driven resolution. | |
| Multi-tenancy: tenant_id on every row (Commandment #3) | |
| """ | |
| from datetime import datetime, timezone | |
| from enum import Enum | |
| from typing import Any, Dict, Optional | |
| from uuid import UUID, uuid4 | |
| from sqlalchemy import Column, JSON, Text | |
| from sqlmodel import Field, SQLModel | |
| class SyncOpStatus(str, Enum): | |
| PENDING = "pending" | |
| APPLIED = "applied" | |
| REJECTED = "rejected" | |
| CONFLICT = "conflict" | |
| class SyncOpMethod(str, Enum): | |
| POST = "POST" | |
| PATCH = "PATCH" | |
| PUT = "PUT" | |
| DELETE = "DELETE" | |
| class ConflictResolutionStrategy(str, Enum): | |
| CLIENT_WINS = "client_wins" | |
| SERVER_WINS = "server_wins" | |
| MERGE = "merge" | |
| MANUAL = "manual" | |
| class SyncOperation(SQLModel, table=True): | |
| """ | |
| A single queued mutation from an offline client. | |
| The client sends these in a batch via POST /api/v1/sync/push. | |
| Each operation targets a specific API path with a payload. | |
| """ | |
| __tablename__ = "sync_operations" | |
| id: UUID = Field(default_factory=uuid4, primary_key=True) | |
| tenant_id: UUID = Field(nullable=False, index=True) | |
| user_id: Optional[UUID] = Field(default=None, index=True) | |
| device_id: Optional[str] = Field(default=None, max_length=100) | |
| # What was the client trying to do? | |
| method: SyncOpMethod = Field(nullable=False) | |
| path: str = Field(max_length=500, nullable=False) | |
| payload: Dict[str, Any] = Field( | |
| default_factory=dict, sa_column=Column(JSON) | |
| ) | |
| # Version tracking for conflict detection | |
| base_version: Optional[int] = Field( | |
| default=None, | |
| description="Entity version the client based its edit on", | |
| ) | |
| client_ts: datetime = Field( | |
| nullable=False, | |
| description="Timestamp on the client when the mutation was created", | |
| ) | |
| # Server processing | |
| status: SyncOpStatus = Field( | |
| default=SyncOpStatus.PENDING, index=True, | |
| ) | |
| server_version: Optional[int] = Field( | |
| default=None, | |
| description="Resulting entity version after apply", | |
| ) | |
| error: Optional[str] = Field(default=None, sa_column=Column(Text)) | |
| # Metadata | |
| created_at: datetime = Field(default_factory=lambda: datetime.now(timezone.utc), nullable=False) | |
| processed_at: Optional[datetime] = Field(default=None) | |
| class ConflictRecord(SQLModel, table=True): | |
| """ | |
| Stores both versions when a sync conflict is detected. | |
| The user resolves via UI (side-by-side diff). | |
| """ | |
| __tablename__ = "sync_conflicts" | |
| id: UUID = Field(default_factory=uuid4, primary_key=True) | |
| tenant_id: UUID = Field(nullable=False, index=True) | |
| user_id: Optional[UUID] = Field(default=None) | |
| # Link to the sync operation that triggered the conflict | |
| sync_op_id: UUID = Field(nullable=False, index=True) | |
| # Divergent data | |
| resource_type: str = Field(max_length=100, nullable=False) | |
| resource_id: str = Field(max_length=200, nullable=False) | |
| client_data: Dict[str, Any] = Field( | |
| default_factory=dict, sa_column=Column(JSON) | |
| ) | |
| server_data: Dict[str, Any] = Field( | |
| default_factory=dict, sa_column=Column(JSON) | |
| ) | |
| client_ts: datetime = Field(nullable=False) | |
| server_ts: datetime = Field(nullable=False) | |
| # Resolution | |
| resolved: bool = Field(default=False, index=True) | |
| resolution_strategy: Optional[ConflictResolutionStrategy] = Field(default=None) | |
| resolved_data: Optional[Dict[str, Any]] = Field( | |
| default=None, sa_column=Column(JSON) | |
| ) | |
| resolved_by: Optional[str] = Field(default=None, max_length=100) | |
| resolved_at: Optional[datetime] = Field(default=None) | |
| # Audit | |
| detected_at: datetime = Field(default_factory=lambda: datetime.now(timezone.utc), nullable=False) | |