Skip to content

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 async with context or close manually.

Source code in tux/database/client.py
Python
def __init__(self) -> None:
    # Track whether connect() and init_db() have run
    self._connected = False
    self._registered = False

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
disconnect() -> None async

Dispose the engine and reset connection state.

Source code in tux/database/client.py
Python
async def disconnect(self) -> None:
    """Dispose the engine and reset connection state."""
    await self.engine.dispose()
    self._connected = False
    self._registered = False
is_connected() -> bool

Return True if connect() has been successfully called.

Source code in tux/database/client.py
Python
def is_connected(self) -> bool:
    """Return True if connect() has been successfully called."""
    return self._connected
is_registered() -> bool

Return True if tables have been created.

Source code in tux/database/client.py
Python
def is_registered(self) -> bool:
    """Return True if tables have been created."""
    return self._registered
init_db() -> None async

Create all tables defined in SQLModel metadata.

Source code in tux/database/client.py
Python
async def init_db(self) -> None:
    """
    Create all tables defined in SQLModel metadata.
    """
    async with self.engine.begin() as conn:
        await conn.run_sync(SQLModel.metadata.create_all)
get_session() -> AsyncSession

Return a new AsyncSession. Use with async with context or close manually.

Source code in tux/database/client.py
Python
def get_session(self) -> AsyncSession:
    """
    Return a new AsyncSession. Use with `async with` context or close manually.
    """
    return self.SessionLocal()