Base SQLAlchemy¶
The base/sqlalchemy subpackage provides the foundational SQLAlchemy components shared across all relational database
adapters, including the abstract port interface, session managers, and session manager registries.
Ports¶
Abstract port interface defining the contract all SQLAlchemy-based adapters must fulfil.
archipy.adapters.base.sqlalchemy.ports.AnyExecuteParams
module-attribute
¶
archipy.adapters.base.sqlalchemy.ports.SQLAlchemyPort ¶
Abstract interface defining synchronous SQLAlchemy database operations.
This interface defines the contract that all synchronous SQLAlchemy adapters must implement, providing standard methods for database operations like create, read, update, delete (CRUD), as well as search and transaction management.
Implementations of this interface are responsible for handling the specific details of database interactions and connection management.
Source code in archipy/adapters/base/sqlalchemy/ports.py
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 | |
archipy.adapters.base.sqlalchemy.ports.SQLAlchemyPort.get_session
abstractmethod
¶
Retrieves a SQLAlchemy session for database operations.
Returns:
| Name | Type | Description |
|---|---|---|
Session |
Session
|
A SQLAlchemy session object |
archipy.adapters.base.sqlalchemy.ports.SQLAlchemyPort.execute_search_query
abstractmethod
¶
execute_search_query(
entity: type[BaseEntity],
query: Select,
pagination: PaginationDTO | None = None,
sort_info: SortDTO | None = None,
has_multiple_entities: bool = False,
) -> tuple[list[BaseEntity], int]
Executes a search query with pagination and sorting.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
entity
|
type[BaseEntity]
|
The entity class to query |
required |
query
|
Select
|
The SQLAlchemy SELECT query |
required |
pagination
|
PaginationDTO | None
|
Optional pagination settings |
None
|
sort_info
|
SortDTO | None
|
Optional sorting information |
None
|
has_multiple_entities
|
bool
|
Optional bool. |
False
|
Returns:
| Type | Description |
|---|---|
tuple[list[BaseEntity], int]
|
A tuple containing the list of entities and the total count |
Source code in archipy/adapters/base/sqlalchemy/ports.py
archipy.adapters.base.sqlalchemy.ports.SQLAlchemyPort.create
abstractmethod
¶
Creates a new entity in the database.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
entity
|
BaseEntity
|
The entity to create |
required |
Returns:
| Type | Description |
|---|---|
BaseEntity | None
|
The created entity (with updated attributes) or None if creation failed |
Source code in archipy/adapters/base/sqlalchemy/ports.py
archipy.adapters.base.sqlalchemy.ports.SQLAlchemyPort.bulk_create
abstractmethod
¶
Creates multiple entities in the database.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
entities
|
list[BaseEntity]
|
List of entities to create |
required |
Returns:
| Type | Description |
|---|---|
list[BaseEntity] | None
|
The list of created entities or None if creation failed |
Source code in archipy/adapters/base/sqlalchemy/ports.py
archipy.adapters.base.sqlalchemy.ports.SQLAlchemyPort.get_by_uuid
abstractmethod
¶
Retrieves an entity by its UUID.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
entity_type
|
type[BaseEntity]
|
The type of entity to retrieve |
required |
entity_uuid
|
UUID
|
The UUID of the entity |
required |
Returns:
| Type | Description |
|---|---|
BaseEntity | None
|
The entity if found, None otherwise |
Source code in archipy/adapters/base/sqlalchemy/ports.py
archipy.adapters.base.sqlalchemy.ports.SQLAlchemyPort.delete
abstractmethod
¶
Deletes an entity from the database.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
entity
|
BaseEntity
|
The entity to delete |
required |
archipy.adapters.base.sqlalchemy.ports.SQLAlchemyPort.bulk_delete
abstractmethod
¶
Deletes multiple entities from the database.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
entities
|
list[BaseEntity]
|
List of entities to delete |
required |
archipy.adapters.base.sqlalchemy.ports.SQLAlchemyPort.execute
abstractmethod
¶
Executes a raw SQL statement.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
statement
|
Executable
|
The SQL statement to execute |
required |
params
|
AnyExecuteParams | None
|
Optional parameters for the SQL statement |
None
|
Returns:
| Type | Description |
|---|---|
Result[Any]
|
The result of the execution |
Source code in archipy/adapters/base/sqlalchemy/ports.py
archipy.adapters.base.sqlalchemy.ports.SQLAlchemyPort.scalars
abstractmethod
¶
Executes a statement and returns the scalar result.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
statement
|
Executable
|
The SQL statement to execute |
required |
params
|
AnyExecuteParams | None
|
Optional parameters for the SQL statement |
None
|
Returns:
| Type | Description |
|---|---|
ScalarResult[Any]
|
The scalar result of the execution |
Source code in archipy/adapters/base/sqlalchemy/ports.py
archipy.adapters.base.sqlalchemy.ports.AsyncSQLAlchemyPort ¶
Abstract interface defining asynchronous SQLAlchemy database operations.
This interface defines the contract that all asynchronous SQLAlchemy adapters must implement, providing standard methods for database operations like create, read, update, delete (CRUD), as well as search and transaction management.
Implementations of this interface are responsible for handling the specific details of asynchronous database interactions and connection management.
Source code in archipy/adapters/base/sqlalchemy/ports.py
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 | |
archipy.adapters.base.sqlalchemy.ports.AsyncSQLAlchemyPort.get_session
abstractmethod
¶
Retrieves an asynchronous SQLAlchemy session for database operations.
Returns:
| Name | Type | Description |
|---|---|---|
AsyncSession |
AsyncSession
|
An asynchronous SQLAlchemy session object |
Source code in archipy/adapters/base/sqlalchemy/ports.py
archipy.adapters.base.sqlalchemy.ports.AsyncSQLAlchemyPort.execute_search_query
abstractmethod
async
¶
execute_search_query(
entity: type[BaseEntity],
query: Select,
pagination: PaginationDTO | None,
sort_info: SortDTO | None = None,
has_multiple_entities: bool = False,
) -> tuple[list[BaseEntity], int]
Executes a search query with pagination and sorting asynchronously.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
entity
|
type[BaseEntity]
|
The entity class to query |
required |
query
|
Select
|
The SQLAlchemy SELECT query |
required |
pagination
|
PaginationDTO | None
|
Optional pagination settings |
required |
sort_info
|
SortDTO | None
|
Optional sorting information |
None
|
has_multiple_entities
|
bool
|
Optional bool |
False
|
Returns:
| Type | Description |
|---|---|
tuple[list[BaseEntity], int]
|
A tuple containing the list of entities and the total count |
Source code in archipy/adapters/base/sqlalchemy/ports.py
archipy.adapters.base.sqlalchemy.ports.AsyncSQLAlchemyPort.create
abstractmethod
async
¶
Creates a new entity in the database asynchronously.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
entity
|
BaseEntity
|
The entity to create |
required |
Returns:
| Type | Description |
|---|---|
BaseEntity | None
|
The created entity (with updated attributes) or None if creation failed |
Source code in archipy/adapters/base/sqlalchemy/ports.py
archipy.adapters.base.sqlalchemy.ports.AsyncSQLAlchemyPort.bulk_create
abstractmethod
async
¶
Creates multiple entities in the database asynchronously.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
entities
|
list[BaseEntity]
|
List of entities to create |
required |
Returns:
| Type | Description |
|---|---|
list[BaseEntity] | None
|
The list of created entities or None if creation failed |
Source code in archipy/adapters/base/sqlalchemy/ports.py
archipy.adapters.base.sqlalchemy.ports.AsyncSQLAlchemyPort.get_by_uuid
abstractmethod
async
¶
Retrieves an entity by its UUID asynchronously.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
entity_type
|
type[BaseEntity]
|
The type of entity to retrieve |
required |
entity_uuid
|
UUID
|
The UUID of the entity |
required |
Returns:
| Type | Description |
|---|---|
BaseEntity | None
|
The entity if found, None otherwise |
Source code in archipy/adapters/base/sqlalchemy/ports.py
archipy.adapters.base.sqlalchemy.ports.AsyncSQLAlchemyPort.delete
abstractmethod
async
¶
Deletes an entity from the database asynchronously.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
entity
|
BaseEntity
|
The entity to delete |
required |
archipy.adapters.base.sqlalchemy.ports.AsyncSQLAlchemyPort.bulk_delete
abstractmethod
async
¶
Deletes multiple entities from the database asynchronously.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
entities
|
list[BaseEntity]
|
List of entities to delete |
required |
Source code in archipy/adapters/base/sqlalchemy/ports.py
archipy.adapters.base.sqlalchemy.ports.AsyncSQLAlchemyPort.execute
abstractmethod
async
¶
Executes a raw SQL statement asynchronously.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
statement
|
Executable
|
The SQL statement to execute |
required |
params
|
AnyExecuteParams | None
|
Optional parameters for the SQL statement |
None
|
Returns:
| Type | Description |
|---|---|
Result[Any]
|
The result of the execution |
Source code in archipy/adapters/base/sqlalchemy/ports.py
archipy.adapters.base.sqlalchemy.ports.AsyncSQLAlchemyPort.scalars
abstractmethod
async
¶
Executes a statement and returns the scalar result asynchronously.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
statement
|
Executable
|
The SQL statement to execute |
required |
params
|
AnyExecuteParams | None
|
Optional parameters for the SQL statement |
None
|
Returns:
| Type | Description |
|---|---|
ScalarResult[Any]
|
The scalar result of the execution |
Source code in archipy/adapters/base/sqlalchemy/ports.py
options: show_root_toc_entry: false heading_level: 3
Session Manager Ports¶
Abstract interface for SQLAlchemy session managers, decoupling session lifecycle from adapter logic.
archipy.adapters.base.sqlalchemy.session_manager_ports.SessionManagerPort ¶
Interface for SQLAlchemy session management operations.
This interface defines the contract for session management adapters, providing methods for retrieving and managing database sessions in a synchronous context.
Implementing classes must provide mechanisms to: 1. Retrieve a properly configured SQLAlchemy session 2. Release/remove sessions when they're no longer needed
Source code in archipy/adapters/base/sqlalchemy/session_manager_ports.py
archipy.adapters.base.sqlalchemy.session_manager_ports.SessionManagerPort.get_session
abstractmethod
¶
Retrieve a SQLAlchemy session.
This method provides a database session that can be used for querying, creating, updating, and deleting data.
Returns:
| Name | Type | Description |
|---|---|---|
Session |
Session
|
A SQLAlchemy session object |
Examples:
Source code in archipy/adapters/base/sqlalchemy/session_manager_ports.py
archipy.adapters.base.sqlalchemy.session_manager_ports.SessionManagerPort.remove_session
abstractmethod
¶
Remove the current session from the registry.
This method should be called to clean up the session when it's no longer needed, helping to prevent resource leaks and ensure proper session management.
Source code in archipy/adapters/base/sqlalchemy/session_manager_ports.py
archipy.adapters.base.sqlalchemy.session_manager_ports.AsyncSessionManagerPort ¶
Interface for asynchronous SQLAlchemy session management operations.
This interface defines the contract for asynchronous session management adapters, providing methods for retrieving and managing database sessions in an asynchronous context using SQLAlchemy's async capabilities.
Implementing classes must provide mechanisms to: 1. Retrieve a properly configured asynchronous SQLAlchemy session 2. Release/remove sessions asynchronously when they're no longer needed
Source code in archipy/adapters/base/sqlalchemy/session_manager_ports.py
archipy.adapters.base.sqlalchemy.session_manager_ports.AsyncSessionManagerPort.get_session
abstractmethod
¶
Retrieve an asynchronous SQLAlchemy session.
This method provides an async database session that can be used for asynchronous querying, creating, updating, and deleting data.
Returns:
| Name | Type | Description |
|---|---|---|
AsyncSession |
AsyncSession
|
An asynchronous SQLAlchemy session object |
Source code in archipy/adapters/base/sqlalchemy/session_manager_ports.py
archipy.adapters.base.sqlalchemy.session_manager_ports.AsyncSessionManagerPort.remove_session
abstractmethod
async
¶
Asynchronously remove the current session from the registry.
This method should be called to clean up the session when it's no longer needed, helping to prevent resource leaks and ensure proper session management in async contexts.
Source code in archipy/adapters/base/sqlalchemy/session_manager_ports.py
options: show_root_toc_entry: false heading_level: 3
Session Manager Registry¶
Registry for tracking and resolving active session manager instances.
archipy.adapters.base.sqlalchemy.session_manager_registry.SessionManagerRegistry ¶
Registry for SQLAlchemy session managers.
This registry provides a centralized access point for both synchronous and asynchronous session managers, implementing the Service Locator pattern.
Subclasses should override get_sync_manager and get_async_manager to provide concrete session managers, or use set_sync_manager and set_async_manager to register managers manually.
Examples:
>>> from archipy.adapters.postgres.sqlalchemy.session_manager_registry import PostgresSessionManagerRegistry
>>> sync_manager = PostgresSessionManagerRegistry.get_sync_manager()
>>> session = sync_manager.get_session()
Source code in archipy/adapters/base/sqlalchemy/session_manager_registry.py
archipy.adapters.base.sqlalchemy.session_manager_registry.SessionManagerRegistry.get_sync_manager
classmethod
¶
Get the synchronous session manager instance.
Returns:
| Name | Type | Description |
|---|---|---|
SessionManagerPort |
SessionManagerPort
|
The registered synchronous session manager |
Raises:
| Type | Description |
|---|---|
InternalError
|
If no synchronous session manager is set |
DatabaseConnectionError
|
If there's an error initializing the session manager |
Source code in archipy/adapters/base/sqlalchemy/session_manager_registry.py
archipy.adapters.base.sqlalchemy.session_manager_registry.SessionManagerRegistry.set_sync_manager
classmethod
¶
Set a custom synchronous session manager.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
manager
|
SessionManagerPort
|
An instance implementing SessionManagerPort |
required |
Raises:
| Type | Description |
|---|---|
InvalidArgumentError
|
If the manager is None or doesn't implement SessionManagerPort |
Source code in archipy/adapters/base/sqlalchemy/session_manager_registry.py
archipy.adapters.base.sqlalchemy.session_manager_registry.SessionManagerRegistry.get_async_manager
classmethod
¶
Get the asynchronous session manager instance.
Returns:
| Name | Type | Description |
|---|---|---|
AsyncSessionManagerPort |
AsyncSessionManagerPort
|
The registered asynchronous session manager |
Raises:
| Type | Description |
|---|---|
InternalError
|
If no asynchronous session manager is set |
DatabaseConnectionError
|
If there's an error initializing the session manager |
Source code in archipy/adapters/base/sqlalchemy/session_manager_registry.py
archipy.adapters.base.sqlalchemy.session_manager_registry.SessionManagerRegistry.set_async_manager
classmethod
¶
Set a custom asynchronous session manager.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
manager
|
AsyncSessionManagerPort
|
An instance implementing AsyncSessionManagerPort |
required |
Raises:
| Type | Description |
|---|---|
InvalidArgumentError
|
If the manager is None or doesn't implement AsyncSessionManagerPort |
Source code in archipy/adapters/base/sqlalchemy/session_manager_registry.py
archipy.adapters.base.sqlalchemy.session_manager_registry.SessionManagerRegistry.reset
classmethod
¶
Reset the registry to its initial state.
This method clears both registered managers, useful for testing.
Source code in archipy/adapters/base/sqlalchemy/session_manager_registry.py
options: show_root_toc_entry: false heading_level: 3
Session Managers¶
Concrete session manager implementations that handle SQLAlchemy session creation, scoping, and teardown.
archipy.adapters.base.sqlalchemy.session_managers.ConfigT
module-attribute
¶
archipy.adapters.base.sqlalchemy.session_managers.BaseSQLAlchemySessionManager ¶
Bases: SessionManagerPort
Base synchronous SQLAlchemy session manager.
Implements the SessionManagerPort interface to provide session management for synchronous database operations. Database-specific session managers should inherit from this class and implement database-specific engine creation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
orm_config
|
ConfigT
|
SQLAlchemy configuration. Must match the expected config type for the database. |
required |
Source code in archipy/adapters/base/sqlalchemy/session_managers.py
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 | |
archipy.adapters.base.sqlalchemy.session_managers.BaseSQLAlchemySessionManager.engine
instance-attribute
¶
archipy.adapters.base.sqlalchemy.session_managers.BaseSQLAlchemySessionManager.get_session ¶
Retrieve a thread-safe SQLAlchemy session.
Returns:
| Name | Type | Description |
|---|---|---|
Session |
Session
|
A SQLAlchemy session instance for database operations. |
Raises:
| Type | Description |
|---|---|
DatabaseConnectionError
|
If there's an error creating the session. |
DatabaseConfigurationError
|
If there's an error in the database configuration. |
Source code in archipy/adapters/base/sqlalchemy/session_managers.py
archipy.adapters.base.sqlalchemy.session_managers.BaseSQLAlchemySessionManager.remove_session ¶
Remove the current session from the registry.
Cleans up the session to prevent resource leaks, typically called at the end of a request.
Raises:
| Type | Description |
|---|---|
DatabaseConnectionError
|
If there's an error removing the session. |
DatabaseConfigurationError
|
If there's an error in the database configuration. |
Source code in archipy/adapters/base/sqlalchemy/session_managers.py
archipy.adapters.base.sqlalchemy.session_managers.AsyncBaseSQLAlchemySessionManager ¶
Bases: AsyncSessionManagerPort
Base asynchronous SQLAlchemy session manager.
Implements the AsyncSessionManagerPort interface to provide session management for asynchronous database operations. Database-specific session managers should inherit from this class and implement database-specific async engine creation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
orm_config
|
ConfigT
|
SQLAlchemy configuration. Must match the expected config type for the database. |
required |
Source code in archipy/adapters/base/sqlalchemy/session_managers.py
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 | |
archipy.adapters.base.sqlalchemy.session_managers.AsyncBaseSQLAlchemySessionManager.engine
instance-attribute
¶
archipy.adapters.base.sqlalchemy.session_managers.AsyncBaseSQLAlchemySessionManager.get_session ¶
Retrieve a task-safe async SQLAlchemy session.
Returns:
| Name | Type | Description |
|---|---|---|
AsyncSession |
AsyncSession
|
An async SQLAlchemy session instance for database operations. |
Raises:
| Type | Description |
|---|---|
DatabaseConnectionError
|
If there's an error creating the session. |
DatabaseConfigurationError
|
If there's an error in the database configuration. |
Source code in archipy/adapters/base/sqlalchemy/session_managers.py
archipy.adapters.base.sqlalchemy.session_managers.AsyncBaseSQLAlchemySessionManager.remove_session
async
¶
Remove the current session from the registry.
Cleans up the session to prevent resource leaks, typically called at the end of a request.
Raises:
| Type | Description |
|---|---|
DatabaseConnectionError
|
If there's an error removing the session. |
DatabaseConfigurationError
|
If there's an error in the database configuration. |
Source code in archipy/adapters/base/sqlalchemy/session_managers.py
options: show_root_toc_entry: false heading_level: 3
Adapters¶
The base SQLAlchemy adapter implements generic CRUD operations that concrete database adapters (PostgreSQL, SQLite, StarRocks) inherit from.
archipy.adapters.base.sqlalchemy.adapters.ConfigT
module-attribute
¶
archipy.adapters.base.sqlalchemy.adapters.SQLAlchemyExceptionHandlerMixin ¶
Mixin providing centralized exception handling for SQLAlchemy operations.
This mixin provides a standard method for handling database exceptions and converting them to appropriate application-specific exceptions.
Source code in archipy/adapters/base/sqlalchemy/adapters.py
archipy.adapters.base.sqlalchemy.adapters.SQLAlchemyFilterMixin ¶
Mixin providing filtering capabilities for SQLAlchemy queries.
Supports equality, inequality, string operations, list operations, and NULL checks.
Source code in archipy/adapters/base/sqlalchemy/adapters.py
archipy.adapters.base.sqlalchemy.adapters.SQLAlchemyPaginationMixin ¶
Mixin providing pagination capabilities for SQLAlchemy queries.
Supports limiting results and applying offsets for paginated queries.
Source code in archipy/adapters/base/sqlalchemy/adapters.py
archipy.adapters.base.sqlalchemy.adapters.SQLAlchemySortMixin ¶
Mixin providing sorting capabilities for SQLAlchemy queries.
Supports dynamic column selection and ascending/descending order.
Source code in archipy/adapters/base/sqlalchemy/adapters.py
archipy.adapters.base.sqlalchemy.adapters.BaseSQLAlchemyAdapter ¶
Bases: SQLAlchemyPort, SQLAlchemyPaginationMixin, SQLAlchemySortMixin, SQLAlchemyFilterMixin, SQLAlchemyExceptionHandlerMixin
Base synchronous SQLAlchemy adapter for ORM operations.
Provides a standardized interface for CRUD operations, pagination, sorting, and filtering. Specific database adapters should inherit from this class and provide their own session manager.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
orm_config
|
ConfigT | None
|
Configuration for SQLAlchemy. If None, uses global config. |
None
|
Source code in archipy/adapters/base/sqlalchemy/adapters.py
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 | |
archipy.adapters.base.sqlalchemy.adapters.BaseSQLAlchemyAdapter.session_manager
instance-attribute
¶
archipy.adapters.base.sqlalchemy.adapters.BaseSQLAlchemyAdapter.execute_search_query ¶
execute_search_query(
entity: type[T],
query: Select,
pagination: PaginationDTO | None = None,
sort_info: SortDTO | None = None,
has_multiple_entities: bool = False,
) -> tuple[list[T], int]
Execute a search query with pagination and sorting.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
entity
|
type[T]
|
The entity class to query. |
required |
query
|
Select
|
The SQLAlchemy SELECT query. |
required |
pagination
|
PaginationDTO | None
|
Optional pagination settings. |
None
|
sort_info
|
SortDTO | None
|
Optional sorting information. |
None
|
has_multiple_entities
|
bool
|
Optional bool. |
False
|
Returns:
| Type | Description |
|---|---|
tuple[list[T], int]
|
Tuple of the list of entities and the total count. |
Raises:
| Type | Description |
|---|---|
DatabaseQueryError
|
If the database query fails. |
DatabaseTimeoutError
|
If the query times out. |
DatabaseConnectionError
|
If there's a connection error. |
DatabaseTransactionError
|
If there's a transaction error. |
Source code in archipy/adapters/base/sqlalchemy/adapters.py
archipy.adapters.base.sqlalchemy.adapters.BaseSQLAlchemyAdapter.get_session ¶
Get a database session.
Returns:
| Name | Type | Description |
|---|---|---|
Session |
Session
|
A SQLAlchemy session. |
Raises:
| Type | Description |
|---|---|
DatabaseConnectionError
|
If there's an error getting the session. |
DatabaseConfigurationError
|
If there's an error in the database configuration. |
Source code in archipy/adapters/base/sqlalchemy/adapters.py
archipy.adapters.base.sqlalchemy.adapters.BaseSQLAlchemyAdapter.create ¶
Create a new entity in the database.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
entity
|
T
|
The entity to create. |
required |
Returns:
| Type | Description |
|---|---|
T | None
|
The created entity with updated attributes, preserving the original type. |
Raises:
| Type | Description |
|---|---|
InvalidEntityTypeError
|
If the entity type is not a valid SQLAlchemy model. |
DatabaseQueryError
|
If the database operation fails. |
DatabaseIntegrityError
|
If there's an integrity constraint violation. |
DatabaseConstraintError
|
If there's a constraint violation. |
DatabaseConnectionError
|
If there's a connection error. |
DatabaseTransactionError
|
If there's a transaction error. |
Source code in archipy/adapters/base/sqlalchemy/adapters.py
archipy.adapters.base.sqlalchemy.adapters.BaseSQLAlchemyAdapter.bulk_create ¶
Creates multiple entities in a single database operation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
entities
|
list[T]
|
List of entities to create. |
required |
Returns:
| Type | Description |
|---|---|
list[T] | None
|
List of created entities with updated attributes, preserving original types. |
Raises:
| Type | Description |
|---|---|
InvalidEntityTypeError
|
If any entity is not a valid SQLAlchemy model. |
DatabaseQueryError
|
If the database operation fails. |
DatabaseIntegrityError
|
If there's an integrity constraint violation. |
DatabaseConstraintError
|
If there's a constraint violation. |
DatabaseConnectionError
|
If there's a connection error. |
DatabaseTransactionError
|
If there's a transaction error. |
Source code in archipy/adapters/base/sqlalchemy/adapters.py
archipy.adapters.base.sqlalchemy.adapters.BaseSQLAlchemyAdapter.get_by_uuid ¶
Retrieve an entity by its UUID.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
entity_type
|
type[T]
|
The type of entity to retrieve. |
required |
entity_uuid
|
UUID
|
The UUID of the entity. |
required |
Returns:
| Type | Description |
|---|---|
BaseEntity | None
|
The entity if found, None otherwise. |
Raises:
| Type | Description |
|---|---|
InvalidEntityTypeError
|
If the entity type is not a valid SQLAlchemy model. |
DatabaseQueryError
|
If the database operation fails. |
DatabaseTimeoutError
|
If the query times out. |
DatabaseConnectionError
|
If there's a connection error. |
DatabaseTransactionError
|
If there's a transaction error. |
Source code in archipy/adapters/base/sqlalchemy/adapters.py
archipy.adapters.base.sqlalchemy.adapters.BaseSQLAlchemyAdapter.delete ¶
Delete an entity from the database.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
entity
|
T
|
The entity to delete. |
required |
Raises:
| Type | Description |
|---|---|
InvalidEntityTypeError
|
If the entity is not a valid SQLAlchemy model. |
DatabaseQueryError
|
If the database operation fails. |
DatabaseIntegrityError
|
If there's an integrity constraint violation. |
DatabaseConstraintError
|
If there's a constraint violation. |
DatabaseConnectionError
|
If there's a connection error. |
DatabaseTransactionError
|
If there's a transaction error. |
Source code in archipy/adapters/base/sqlalchemy/adapters.py
archipy.adapters.base.sqlalchemy.adapters.BaseSQLAlchemyAdapter.bulk_delete ¶
Delete multiple entities from the database.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
entities
|
list[T]
|
List of entities to delete. |
required |
Raises:
| Type | Description |
|---|---|
InvalidEntityTypeError
|
If any entity is not a valid SQLAlchemy model. |
DatabaseQueryError
|
If the database operation fails. |
DatabaseIntegrityError
|
If there's an integrity constraint violation. |
DatabaseConstraintError
|
If there's a constraint violation. |
DatabaseConnectionError
|
If there's a connection error. |
DatabaseTransactionError
|
If there's a transaction error. |
Source code in archipy/adapters/base/sqlalchemy/adapters.py
archipy.adapters.base.sqlalchemy.adapters.BaseSQLAlchemyAdapter.execute ¶
Execute a SQLAlchemy statement.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
statement
|
Executable
|
The SQLAlchemy statement to execute. |
required |
params
|
AnyExecuteParams | None
|
Optional parameters for the statement. |
None
|
Returns:
| Type | Description |
|---|---|
Result[Any]
|
The result of the execution. |
Raises:
| Type | Description |
|---|---|
DatabaseQueryError
|
If the database operation fails. |
DatabaseTimeoutError
|
If the query times out. |
DatabaseConnectionError
|
If there's a connection error. |
DatabaseTransactionError
|
If there's a transaction error. |
Source code in archipy/adapters/base/sqlalchemy/adapters.py
archipy.adapters.base.sqlalchemy.adapters.BaseSQLAlchemyAdapter.scalars ¶
Execute a SQLAlchemy statement and return scalar results.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
statement
|
Executable
|
The SQLAlchemy statement to execute. |
required |
params
|
AnyExecuteParams | None
|
Optional parameters for the statement. |
None
|
Returns:
| Type | Description |
|---|---|
ScalarResult[Any]
|
The scalar results of the execution. |
Raises:
| Type | Description |
|---|---|
DatabaseQueryError
|
If the database operation fails. |
DatabaseTimeoutError
|
If the query times out. |
DatabaseConnectionError
|
If there's a connection error. |
DatabaseTransactionError
|
If there's a transaction error. |
Source code in archipy/adapters/base/sqlalchemy/adapters.py
archipy.adapters.base.sqlalchemy.adapters.AsyncBaseSQLAlchemyAdapter ¶
Bases: AsyncSQLAlchemyPort, SQLAlchemyPaginationMixin, SQLAlchemySortMixin, SQLAlchemyFilterMixin, SQLAlchemyExceptionHandlerMixin
Base asynchronous SQLAlchemy adapter for ORM operations.
Provides a standardized interface for CRUD operations, pagination, sorting, and filtering. Specific database adapters should inherit from this class and provide their own session manager.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
orm_config
|
ConfigT | None
|
Configuration for SQLAlchemy. If None, uses global config. |
None
|
Source code in archipy/adapters/base/sqlalchemy/adapters.py
540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 | |
archipy.adapters.base.sqlalchemy.adapters.AsyncBaseSQLAlchemyAdapter.session_manager
instance-attribute
¶
session_manager: AsyncBaseSQLAlchemySessionManager[
ConfigT
] = _create_async_session_manager(configs)
archipy.adapters.base.sqlalchemy.adapters.AsyncBaseSQLAlchemyAdapter.execute_search_query
async
¶
execute_search_query(
entity: type[T],
query: Select,
pagination: PaginationDTO | None,
sort_info: SortDTO | None = None,
has_multiple_entities: bool = False,
) -> tuple[list[T], int]
Execute a search query with pagination and sorting.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
entity
|
type[T]
|
The entity class to query. |
required |
query
|
Select
|
The SQLAlchemy SELECT query. |
required |
pagination
|
PaginationDTO | None
|
Optional pagination settings. |
required |
sort_info
|
SortDTO | None
|
Optional sorting information. |
None
|
has_multiple_entities
|
bool
|
Optional bool |
False
|
Returns:
| Type | Description |
|---|---|
tuple[list[T], int]
|
Tuple of the list of entities and the total count. |
Raises:
| Type | Description |
|---|---|
DatabaseQueryError
|
If the database query fails. |
DatabaseTimeoutError
|
If the query times out. |
DatabaseConnectionError
|
If there's a connection error. |
DatabaseTransactionError
|
If there's a transaction error. |
Source code in archipy/adapters/base/sqlalchemy/adapters.py
archipy.adapters.base.sqlalchemy.adapters.AsyncBaseSQLAlchemyAdapter.get_session ¶
Get a database session.
Returns:
| Name | Type | Description |
|---|---|---|
AsyncSession |
AsyncSession
|
A SQLAlchemy async session. |
Raises:
| Type | Description |
|---|---|
DatabaseConnectionError
|
If there's an error getting the session. |
DatabaseConfigurationError
|
If there's an error in the database configuration. |
Source code in archipy/adapters/base/sqlalchemy/adapters.py
archipy.adapters.base.sqlalchemy.adapters.AsyncBaseSQLAlchemyAdapter.create
async
¶
Create a new entity in the database.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
entity
|
T
|
The entity to create. |
required |
Returns:
| Type | Description |
|---|---|
T | None
|
The created entity with updated attributes, preserving the original type. |
Raises:
| Type | Description |
|---|---|
InvalidEntityTypeError
|
If the entity type is not a valid SQLAlchemy model. |
DatabaseQueryError
|
If the database operation fails. |
DatabaseIntegrityError
|
If there's an integrity constraint violation. |
DatabaseConstraintError
|
If there's a constraint violation. |
DatabaseConnectionError
|
If there's a connection error. |
DatabaseTransactionError
|
If there's a transaction error. |
Source code in archipy/adapters/base/sqlalchemy/adapters.py
archipy.adapters.base.sqlalchemy.adapters.AsyncBaseSQLAlchemyAdapter.bulk_create
async
¶
Creates multiple entities in a single database operation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
entities
|
list[T]
|
List of entities to create. |
required |
Returns:
| Type | Description |
|---|---|
list[T] | None
|
List of created entities with updated attributes, preserving original types. |
Raises:
| Type | Description |
|---|---|
InvalidEntityTypeError
|
If any entity is not a valid SQLAlchemy model. |
DatabaseQueryError
|
If the database operation fails. |
DatabaseIntegrityError
|
If there's an integrity constraint violation. |
DatabaseConstraintError
|
If there's a constraint violation. |
DatabaseConnectionError
|
If there's a connection error. |
DatabaseTransactionError
|
If there's a transaction error. |
Source code in archipy/adapters/base/sqlalchemy/adapters.py
archipy.adapters.base.sqlalchemy.adapters.AsyncBaseSQLAlchemyAdapter.get_by_uuid
async
¶
Retrieve an entity by its UUID.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
entity_type
|
type[T]
|
The type of entity to retrieve. |
required |
entity_uuid
|
UUID
|
The UUID of the entity. |
required |
Returns:
| Type | Description |
|---|---|
BaseEntity | None
|
The entity if found, None otherwise. |
Raises:
| Type | Description |
|---|---|
InvalidEntityTypeError
|
If the entity type is not a valid SQLAlchemy model. |
DatabaseQueryError
|
If the database operation fails. |
DatabaseTimeoutError
|
If the query times out. |
DatabaseConnectionError
|
If there's a connection error. |
DatabaseTransactionError
|
If there's a transaction error. |
Source code in archipy/adapters/base/sqlalchemy/adapters.py
archipy.adapters.base.sqlalchemy.adapters.AsyncBaseSQLAlchemyAdapter.delete
async
¶
Delete an entity from the database.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
entity
|
BaseEntity
|
The entity to delete. |
required |
Raises:
| Type | Description |
|---|---|
InvalidEntityTypeError
|
If the entity is not a valid SQLAlchemy model. |
DatabaseQueryError
|
If the database operation fails. |
DatabaseIntegrityError
|
If there's an integrity constraint violation. |
DatabaseConstraintError
|
If there's a constraint violation. |
DatabaseConnectionError
|
If there's a connection error. |
DatabaseTransactionError
|
If there's a transaction error. |
Source code in archipy/adapters/base/sqlalchemy/adapters.py
archipy.adapters.base.sqlalchemy.adapters.AsyncBaseSQLAlchemyAdapter.bulk_delete
async
¶
Delete multiple entities from the database.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
entities
|
list[T]
|
List of entities to delete. |
required |
Raises:
| Type | Description |
|---|---|
InvalidEntityTypeError
|
If any entity is not a valid SQLAlchemy model. |
DatabaseQueryError
|
If the database operation fails. |
DatabaseIntegrityError
|
If there's an integrity constraint violation. |
DatabaseConstraintError
|
If there's a constraint violation. |
DatabaseConnectionError
|
If there's a connection error. |
DatabaseTransactionError
|
If there's a transaction error. |
Source code in archipy/adapters/base/sqlalchemy/adapters.py
archipy.adapters.base.sqlalchemy.adapters.AsyncBaseSQLAlchemyAdapter.execute
async
¶
Execute a SQLAlchemy statement.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
statement
|
Executable
|
The SQLAlchemy statement to execute. |
required |
params
|
AnyExecuteParams | None
|
Optional parameters for the statement. |
None
|
Returns:
| Type | Description |
|---|---|
Result[Any]
|
The result of the execution. |
Raises:
| Type | Description |
|---|---|
DatabaseQueryError
|
If the database operation fails. |
DatabaseTimeoutError
|
If the query times out. |
DatabaseConnectionError
|
If there's a connection error. |
DatabaseTransactionError
|
If there's a transaction error. |
Source code in archipy/adapters/base/sqlalchemy/adapters.py
archipy.adapters.base.sqlalchemy.adapters.AsyncBaseSQLAlchemyAdapter.scalars
async
¶
Execute a SQLAlchemy statement and return scalar results.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
statement
|
Executable
|
The SQLAlchemy statement to execute. |
required |
params
|
AnyExecuteParams | None
|
Optional parameters for the statement. |
None
|
Returns:
| Type | Description |
|---|---|
ScalarResult[Any]
|
The scalar results of the execution. |
Raises:
| Type | Description |
|---|---|
DatabaseQueryError
|
If the database operation fails. |
DatabaseTimeoutError
|
If the query times out. |
DatabaseConnectionError
|
If there's a connection error. |
DatabaseTransactionError
|
If there's a transaction error. |
Source code in archipy/adapters/base/sqlalchemy/adapters.py
options: show_root_toc_entry: false heading_level: 3