tux.database.client
¶
Classes:
Name | Description |
---|---|
DatabaseClient | A singleton database client that manages the Prisma connection. |
Classes¶
DatabaseClient
¶
A singleton database client that manages the Prisma connection.
This class provides a centralized way to manage the database connection and ensures proper connection handling throughout the application lifecycle.
Methods:
Name | Description |
---|---|
is_connected | Check if the database client is connected. |
is_registered | Check if the database client is properly registered. |
connect | Connect to the database. |
disconnect | Disconnect from the database. |
transaction | Create a database transaction. |
batch | Create a batch operation context. |
Attributes:
Name | Type | Description |
---|---|---|
client | Prisma | Get the Prisma client instance. |
Attributes¶
client: Prisma
property
¶
Get the Prisma client instance.
Returns:
Type | Description |
---|---|
Prisma | The Prisma client instance. |
Raises:
Type | Description |
---|---|
RuntimeError | If the client is not connected. |
Functions¶
is_connected() -> bool
¶
Check if the database client is connected.
Returns:
Type | Description |
---|---|
bool | True if the client is connected, False otherwise. |
is_registered() -> bool
¶
Check if the database client is properly registered.
Returns:
Type | Description |
---|---|
bool | True if the client is registered with models, False otherwise. |
Source code in tux/database/client.py
connect() -> None
async
¶
Connect to the database.
This method establishes the database connection and performs any necessary initialization.
Notes
The DATABASE_URL environment variable should be set before calling this method, which is handled by the tux.utils.env module.
Source code in tux/database/client.py
async def connect(self) -> None:
"""Connect to the database.
This method establishes the database connection and performs
any necessary initialization.
Notes
-----
The DATABASE_URL environment variable should be set before calling
this method, which is handled by the tux.utils.env module.
"""
if self._client is not None:
logger.warning(CLIENT_ALREADY_CONNECTED)
return
try:
self._client = Prisma(
log_queries=False,
auto_register=True,
)
await self._client.connect()
logger.info("Successfully connected to database.")
except Exception as e:
logger.error(f"Failed to connect to database: {e}")
raise
disconnect() -> None
async
¶
Disconnect from the database.
This method closes the database connection and performs any necessary cleanup.
Source code in tux/database/client.py
async def disconnect(self) -> None:
"""Disconnect from the database.
This method closes the database connection and performs
any necessary cleanup.
"""
if self._client is None:
logger.warning("Database client is not connected.")
return
try:
await self._client.disconnect()
self._client = None
logger.info("Successfully disconnected from database.")
except Exception as e:
logger.error(f"Failed to disconnect from database: {e}")
raise
transaction() -> AsyncGenerator[None]
async
¶
Create a database transaction.
This context manager ensures that database operations are atomic and handles rollback in case of errors.
Yields:
Type | Description |
---|---|
None | Control is yielded to the caller within the transaction. |
Source code in tux/database/client.py
@asynccontextmanager
async def transaction(self) -> AsyncGenerator[None]:
"""Create a database transaction.
This context manager ensures that database operations are atomic
and handles rollback in case of errors.
Yields
------
None
Control is yielded to the caller within the transaction.
"""
if self._client is None:
raise RuntimeError(CLIENT_NOT_CONNECTED)
async with self._client.batch_() as _:
try:
yield
except Exception as e:
logger.error(f"Transaction failed, rolling back: {e}")
raise
batch() -> AsyncGenerator[None]
async
¶
Create a batch operation context.
This context manager allows batching multiple write operations into a single database call for better performance.
Yields:
Type | Description |
---|---|
None | Control is yielded to the caller within the batch context. |
Source code in tux/database/client.py
async def batch(self) -> AsyncGenerator[None]:
"""Create a batch operation context.
This context manager allows batching multiple write operations
into a single database call for better performance.
Yields
------
None
Control is yielded to the caller within the batch context.
"""
if self._client is None:
raise RuntimeError(CLIENT_NOT_CONNECTED)
async with self._client.batch_() as _:
yield