|
Download src/apps/shared/README.md from anaselamr/yaz: direct link, hf CLI and curl.
- Browser
- Download file 4.36 kB
-
https://huggingface.co/spaces/anaselamr/yaz/resolve/main/src/apps/shared/README.md
- Command line
-
hf download hf://spaces/anaselamr/yaz/src/apps/shared/README.md
-
curl -L -o README.md https://huggingface.co/spaces/anaselamr/yaz/resolve/main/src/apps/shared/README.md
4.36 kB
Shared - Common Utilities & Database Helpers
Purpose: Shared utilities and database helper functions used across all healthcare applications to reduce code duplication and ensure consistency.
Version: 1.0.0
Category: Platform Infrastructure
📁 Structure
shared/
├── __init__.py # Module exports
└── db/ # Database utilities
├── __init__.py
├── session.py # Database session management
├── mixins.py # SQLModel mixins (TenantMixin, TimestampMixin)
├── base.py # Base model classes
└── utils.py # Database helper functions
🚀 Usage
1. Database Session Management
from apps.shared.db.session import get_async_session
@router.get("/patients")
async def list_patients(
session: AsyncSession = Depends(get_async_session)
):
"""
Use shared session dependency for database access
"""
result = await session.execute(select(Patient))
return result.scalars().all()
2. SQLModel Mixins
All domain models should inherit from shared mixins:
from apps.shared.db.mixins import TenantMixin, TimestampMixin, SoftDeleteMixin
from sqlmodel import SQLModel, Field
from uuid import UUID, uuid4
class Patient(TenantMixin, TimestampMixin, SoftDeleteMixin, SQLModel, table=True):
"""
Automatically includes:
- tenant_id: UUID (from TenantMixin)
- created_at, updated_at, created_by, updated_by (from TimestampMixin)
- deleted_at, deleted_by (from SoftDeleteMixin)
"""
__tablename__ = "patients"
__table_args__ = {"schema": "clinica"}
id: UUID = Field(default_factory=uuid4, primary_key=True)
name: str = Field(max_length=200)
# ... other fields
3. Database Utilities
from apps.shared.db.utils import (
paginate_query,
apply_filters,
soft_delete,
restore_soft_deleted
)
# Pagination helper
async def list_with_pagination(
session: AsyncSession,
page: int = 1,
page_size: int = 50
):
query = select(Patient).where(Patient.tenant_id == tenant_id)
result = await paginate_query(session, query, page, page_size)
return result
# Soft delete helper
async def delete_patient(patient_id: UUID, user_id: UUID, session: AsyncSession):
await soft_delete(session, Patient, patient_id, deleted_by=user_id)
📦 Shared Components
TenantMixin
Ensures multi-tenancy isolation:
class TenantMixin(SQLModel):
"""Automatic tenant_id field for all domain models"""
tenant_id: UUID = Field(
foreign_key="core.tenants.id",
index=True,
description="Tenant isolation"
)
Benefits:
- Consistent tenant_id across all tables
- Automatic index creation
- Foreign key constraint enforcement
TimestampMixin
Audit trail timestamps:
class TimestampMixin(SQLModel):
"""Automatic created_at, updated_at, created_by, updated_by fields"""
created_at: datetime = Field(default_factory=datetime.utcnow)
updated_at: datetime = Field(default_factory=datetime.utcnow)
created_by: Optional[UUID] = Field(default=None)
updated_by: Optional[UUID] = Field(default=None)
Benefits:
- Automatic timestamp tracking
- User accountability
- HIPAA audit trail compliance
SoftDeleteMixin
Non-destructive deletion:
class SoftDeleteMixin(SQLModel):
"""Soft delete support with deleted_at, deleted_by fields"""
deleted_at: Optional[datetime] = Field(default=None)
deleted_by: Optional[UUID] = Field(default=None)
Benefits:
- Data recovery capability
- Audit trail preservation
- Regulatory compliance (HIPAA requires 6-year retention)
🧪 Testing
# Test database utilities
pytest tests/apps/shared/test_db_utils.py -v
# Test mixins
pytest tests/apps/shared/test_mixins.py -v
📚 Related Documentation
- Database Layer: ../../../docs/architecture/DATABASE.md
- Multi-Tenancy: ../../../docs/architecture/GOVERNANCE_MODEL.md
- Core Database: ../../core/database/
Maintained By: Platform Infrastructure Team
Last Updated: January 2, 2026