tux.database.client
¶
Classes:
Name | Description |
---|---|
DatabaseClient | A singleton database client that manages the SQLModel connection. |
Classes¶
DatabaseClient()
¶
A singleton database client that manages the SQLModel connection.
This class provides a centralized way to manage the database connection and ensures proper connection handling throughout the application lifecycle.
Methods:
Name | Description |
---|---|
connect | Establish a test connection and initialize database tables. |
disconnect | Dispose the engine and reset connection state. |
is_connected | Return True if connect() has been successfully called. |
is_registered | Return True if tables have been created. |
init_db | Create all tables defined in SQLModel metadata. |
get_session | Return a new AsyncSession. Use with |
Source code in tux/database/client.py
Functions¶
connect() -> None
async
¶
Establish a test connection and initialize database tables.
Source code in tux/database/client.py
Python
async def connect(self) -> None:
"""Establish a test connection and initialize database tables."""
if self._connected:
raise RuntimeError(CLIENT_ALREADY_CONNECTED)
# Create tables and test engine
async with self.engine.begin() as conn:
await conn.run_sync(SQLModel.metadata.create_all)
self._connected = True
self._registered = True