|
| 1 | +# Built-in Read Operations |
| 2 | + |
| 3 | +**Note**: PySpringModel currently only supports **read operations**. Write operations (Create, Update, Delete) are not yet implemented. |
| 4 | + |
| 5 | +The `CrudRepository` provides a comprehensive set of built-in methods for common database read operations. These methods handle data retrieval with automatic session management and error handling. |
| 6 | + |
| 7 | +## Read Operations |
| 8 | + |
| 9 | +### Find by Primary Key |
| 10 | + |
| 11 | +```python |
| 12 | +class UserRepository(CrudRepository[int, User]): |
| 13 | + # Find a single entity by its primary key |
| 14 | + def find_by_id(self, id: int) -> Optional[User]: ... |
| 15 | + |
| 16 | + # Find multiple entities by their primary keys |
| 17 | + def find_all_by_ids(self, ids: List[int]) -> List[User]: ... |
| 18 | +``` |
| 19 | + |
| 20 | +### Find All Records |
| 21 | + |
| 22 | +```python |
| 23 | +class UserRepository(CrudRepository[int, User]): |
| 24 | + # Find all entities in the table |
| 25 | + def find_all(self) -> List[User]: ... |
| 26 | +``` |
| 27 | + |
| 28 | +### Usage Examples |
| 29 | + |
| 30 | +```python |
| 31 | +class UserService: |
| 32 | + user_repository: UserRepository |
| 33 | + |
| 34 | + def get_user_by_id(self, user_id: int) -> Optional[User]: |
| 35 | + return self.user_repository.find_by_id(user_id) |
| 36 | + |
| 37 | + def get_users_by_ids(self, user_ids: List[int]) -> List[User]: |
| 38 | + return self.user_repository.find_all_by_ids(user_ids) |
| 39 | + |
| 40 | + def get_all_users(self) -> List[User]: |
| 41 | + return self.user_repository.find_all() |
| 42 | +``` |
| 43 | + |
| 44 | +## Write Operations (Not Yet Supported) |
| 45 | + |
| 46 | +The following operations are planned but not yet implemented in PySpringModel: |
| 47 | + |
| 48 | +### Save Single Entity |
| 49 | +```python |
| 50 | +# This functionality is not yet available |
| 51 | +# def save(self, entity: User) -> User: ... |
| 52 | +``` |
| 53 | + |
| 54 | +### Save Multiple Entities |
| 55 | +```python |
| 56 | +# This functionality is not yet available |
| 57 | +# def save_all(self, entities: List[User]) -> List[User]: ... |
| 58 | +``` |
| 59 | + |
| 60 | +### Delete Operations |
| 61 | +```python |
| 62 | +# This functionality is not yet available |
| 63 | +# def delete(self, entity: User) -> None: ... |
| 64 | +# def delete_by_id(self, id: int) -> None: ... |
| 65 | +``` |
| 66 | + |
| 67 | +## Complete Read Operations Example |
| 68 | + |
| 69 | +Here's a complete example showing all available read operations: |
| 70 | + |
| 71 | +```python |
| 72 | +from py_spring_model import PySpringModel, CrudRepository |
| 73 | +from sqlmodel import Field |
| 74 | +from typing import Optional, List |
| 75 | + |
| 76 | +# Model definition |
| 77 | +class User(PySpringModel, table=True): |
| 78 | + id: int = Field(default=None, primary_key=True) |
| 79 | + name: str = Field() |
| 80 | + email: str = Field() |
| 81 | + age: int = Field() |
| 82 | + status: str = Field() |
| 83 | + |
| 84 | +# Repository with read operations only |
| 85 | +class UserRepository(CrudRepository[int, User]): |
| 86 | + # Dynamic queries |
| 87 | + def find_by_name(self, name: str) -> Optional[User]: ... |
| 88 | + def find_all_by_status(self, status: str) -> List[User]: ... |
| 89 | + |
| 90 | +# Service using read operations only |
| 91 | +class UserService: |
| 92 | + user_repository: UserRepository |
| 93 | + |
| 94 | + # Read operations |
| 95 | + def get_user_by_id(self, user_id: int) -> Optional[User]: |
| 96 | + return self.user_repository.find_by_id(user_id) |
| 97 | + |
| 98 | + def get_user_by_name(self, name: str) -> Optional[User]: |
| 99 | + return self.user_repository.find_by_name(name) |
| 100 | + |
| 101 | + def get_all_users(self) -> List[User]: |
| 102 | + return self.user_repository.find_all() |
| 103 | + |
| 104 | + def get_active_users(self) -> List[User]: |
| 105 | + return self.user_repository.find_all_by_status("active") |
| 106 | + |
| 107 | + def get_users_by_ids(self, user_ids: List[int]) -> List[User]: |
| 108 | + return self.user_repository.find_all_by_ids(user_ids) |
| 109 | +``` |
| 110 | + |
| 111 | +## Session Management |
| 112 | + |
| 113 | +PySpringModel automatically handles database sessions for read operations: |
| 114 | + |
| 115 | +### Automatic Session Handling |
| 116 | + |
| 117 | +```python |
| 118 | +class UserService: |
| 119 | + user_repository: UserRepository |
| 120 | + |
| 121 | + def get_user_data(self, user_id: int) -> Optional[dict]: |
| 122 | + # Session is automatically managed for read operations |
| 123 | + user = self.user_repository.find_by_id(user_id) |
| 124 | + if user: |
| 125 | + return { |
| 126 | + "id": user.id, |
| 127 | + "name": user.name, |
| 128 | + "email": user.email, |
| 129 | + "age": user.age, |
| 130 | + "status": user.status |
| 131 | + } |
| 132 | + return None |
| 133 | +``` |
| 134 | + |
| 135 | +## Error Handling |
| 136 | + |
| 137 | +PySpringModel provides robust error handling for read operations: |
| 138 | + |
| 139 | +### Common Error Scenarios |
| 140 | + |
| 141 | +```python |
| 142 | +class UserService: |
| 143 | + user_repository: UserRepository |
| 144 | + |
| 145 | + def safe_get_user(self, user_id: int) -> Optional[User]: |
| 146 | + try: |
| 147 | + return self.user_repository.find_by_id(user_id) |
| 148 | + except Exception as e: |
| 149 | + # Handle database connection errors, etc. |
| 150 | + print(f"Error retrieving user: {e}") |
| 151 | + return None |
| 152 | + |
| 153 | + def safe_get_users_by_status(self, status: str) -> List[User]: |
| 154 | + try: |
| 155 | + return self.user_repository.find_all_by_status(status) |
| 156 | + except Exception as e: |
| 157 | + # Handle database errors |
| 158 | + print(f"Error retrieving users: {e}") |
| 159 | + return [] |
| 160 | +``` |
| 161 | + |
| 162 | +## Performance Considerations |
| 163 | + |
| 164 | +### Batch Reading |
| 165 | + |
| 166 | +```python |
| 167 | +class UserService: |
| 168 | + user_repository: UserRepository |
| 169 | + |
| 170 | + def get_multiple_users(self, user_ids: List[int]) -> List[User]: |
| 171 | + # Efficient batch reading |
| 172 | + return self.user_repository.find_all_by_ids(user_ids) |
| 173 | +``` |
| 174 | + |
| 175 | +### Selective Loading |
| 176 | + |
| 177 | +```python |
| 178 | +class UserService: |
| 179 | + user_repository: UserRepository |
| 180 | + |
| 181 | + def get_user_names_only(self) -> List[str]: |
| 182 | + # Use custom queries for selective loading |
| 183 | + users = self.user_repository.find_all() |
| 184 | + return [user.name for user in users] |
| 185 | +``` |
| 186 | + |
| 187 | +## Best Practices |
| 188 | + |
| 189 | +1. **Use appropriate read methods**: Choose the right read method for your use case |
| 190 | +2. **Handle errors gracefully**: Always handle potential exceptions in your service layer |
| 191 | +3. **Use batch operations**: Prefer batch operations for multiple entities |
| 192 | +4. **Monitor performance**: Use custom queries for complex operations that can't be handled by built-in methods |
| 193 | +5. **Plan for future write operations**: Design your models and repositories with future write operations in mind |
| 194 | +6. **Use SQLAlchemy for writes**: For now, use SQLAlchemy directly for any write operations you need |
0 commit comments