infrastructure.persistence.database¶
src.infrastructure.persistence.database
¶
Database connection and session management.
This module provides database connection management using SQLAlchemy's async engine and session handling. It follows dependency injection patterns to provide database sessions to repositories and services.
Following hexagonal architecture: - This is an infrastructure concern - Provides database sessions to repository implementations - Handles transaction boundaries and connection pooling
Classes¶
Database
¶
Database connection and session management.
This class manages the database engine and provides async sessions for database operations. It handles: - Connection pooling - Session lifecycle - Transaction management
Usage
db = Database("postgresql+asyncpg://user:pass@host/dbname") async with db.get_session() as session: # Use session for database operations # Automatically commits on success, rolls back on error
Source code in src/infrastructure/persistence/database.py
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 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 | |
Functions¶
__init__
¶
__init__(
database_url: str,
echo: bool = False,
pool_size: int = 20,
max_overflow: int = 0,
) -> None
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
database_url
|
str
|
Database connection URL (e.g., postgresql+asyncpg://...) |
required |
echo
|
bool
|
If True, log all SQL statements (useful for debugging) |
False
|
pool_size
|
int
|
Number of connections to maintain in pool |
20
|
max_overflow
|
int
|
Maximum overflow connections above pool_size |
0
|
Source code in src/infrastructure/persistence/database.py
get_session
async
¶
Provide a transactional database session.
This is a context manager that: - Creates a new session - Commits on successful exit - Rolls back on exception - Always closes the session
Yields:
| Name | Type | Description |
|---|---|---|
AsyncSession |
AsyncGenerator[AsyncSession, None]
|
Database session for operations |
Example
async with db.get_session() as session: user = User(email="test@example.com") session.add(user) # Automatically commits when context exits
Source code in src/infrastructure/persistence/database.py
transaction
async
¶
Provide an explicit transaction context.
Use this when you need explicit transaction control, especially for operations that span multiple repositories.
Yields:
| Name | Type | Description |
|---|---|---|
AsyncSession |
AsyncGenerator[AsyncSession, None]
|
Database session within a transaction |
Example
async with db.transaction() as session: user_repo = UserRepository(session) account_repo = AccountRepository(session)
await user_repo.save(user)
await account_repo.save(account)
# Both operations commit together
Source code in src/infrastructure/persistence/database.py
create_all
async
¶
Create all tables defined in the models.
Warning: This should only be used for development/testing. Production should use Alembic migrations.
Source code in src/infrastructure/persistence/database.py
drop_all
async
¶
Drop all tables defined in the models.
Warning: This will delete all data! Only use for testing.
Source code in src/infrastructure/persistence/database.py
close
async
¶
Close all database connections.
Should be called when shutting down the application.
check_connection
async
¶
Check if database connection is working.
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if connection is successful, False otherwise |