Skip to content

State Stores

Abstract interface and implementations for persisting entity state.

StateStoreClient

StateStoreClient(connection_string: str | None = None)

Base class for persistent state store implementations.

Provides connect/disconnect lifecycle, context manager support, and connection guards. Mirrors pycharter's MetadataStoreClient.

Subclasses must override connect() and should call _require_connection() at the top of every method that touches the backend.

Source code in src/pystator/stores/base.py
def __init__(self, connection_string: str | None = None) -> None:
    self.connection_string = connection_string
    self._connection: Any = None

connect

connect() -> None

Establish backend connection. Subclasses must implement.

Source code in src/pystator/stores/base.py
def connect(self) -> None:
    """Establish backend connection.  Subclasses must implement."""
    raise NotImplementedError("Subclasses must implement connect()")

disconnect

disconnect() -> None

Close the backend connection.

Source code in src/pystator/stores/base.py
def disconnect(self) -> None:
    """Close the backend connection."""
    if self._connection is not None:
        self._connection = None

InMemoryStateStore

InMemoryStateStore()

In-memory state store for testing and simple use cases.

Implements sync, async, and versioned protocols. Not suitable for production.

Source code in src/pystator/stores/base.py
def __init__(self) -> None:
    self._state: dict[str, str] = {}
    self._context: dict[str, dict[str, Any]] = {}
    self._metadata: dict[str, dict[str, Any]] = {}
    self._versions: dict[str, int] = {}

Other backends (SQLiteStateStore, PostgresStateStore, MongoDBStateStore, RedisStateStore) are available from pystator.stores when the corresponding optional dependencies are installed. See the package structure and state stores guide.