Overview
Repositories provide an abstraction layer between the domain and data access layers, implementing the Repository pattern from Domain-Driven Design (DDD). They translate between domain entities and database models.Repository Pattern
The repository pattern in Soft-Bee follows these principles:- Interface Segregation: Each repository implements a domain-specific interface
- Dependency Inversion: Use cases depend on repository interfaces, not implementations
- Data Mapping: Repositories handle conversion between domain entities and persistence models
- Encapsulation: Database operations are encapsulated within repository methods
User Repository Implementation
Location
src/features/auth/infrastructure/repositories/user_repository_impl.py:11
Structure
Key Methods
Existence Checks
Check if a user exists by email or username without loading the full entity:user_repository_impl.py:56-64
Token Management
Manage refresh tokens stored as JSON arrays:user_repository_impl.py:87-100
user_repository_impl.py:102-114
Session Tracking
Update last login timestamp:user_repository_impl.py:76-85
Delete Operations
user_repository_impl.py:66-74
Best Practices
UUID Validation
Always validate UUID strings before querying:Session Management
- Repositories receive database sessions via dependency injection
- Always commit after mutations
- Use
scalar()for existence checks to get boolean results
Error Handling
- Handle UUID parsing errors gracefully
- Return appropriate default values (None, False, empty list)
- Don’t expose SQLAlchemy exceptions to the application layer