Session Backends

This module provides SQLAlchemy-based session backends for Litestar’s server-side session middleware.

Session Model Mixin

class advanced_alchemy.extensions.litestar.session.SessionModelMixin[source]

Bases: UUIDv7Base

Mixin for session storage.

is_expired

SQL-Expression to check if the session has expired.

Returns:

SQL-Expression to check if the session has expired.

Session Backend Base

class advanced_alchemy.extensions.litestar.session.SQLAlchemySessionBackendBase[source]

Bases: ServerSideSessionBackend, ABC, Generic[SQLAlchemyConfigT]

Session backend to store data in a database with SQLAlchemy. Works with both sync and async engines.

Notes

  • Requires sqlalchemy which needs to be installed separately, and a configured SQLAlchemyPlugin.

__init__(config, alchemy_config, model)[source]

Initialize BaseSQLAlchemyBackend.

Parameters:
__deepcopy__(memo)[source]

Custom deepcopy implementation to handle unpicklable SQLAlchemy objects.

Return type:

SQLAlchemySessionBackendBase[TypeVar(SQLAlchemyConfigT, bound= Union[SQLAlchemyAsyncConfig, SQLAlchemySyncConfig])]

static supports_merge(dialect=None, force_disable_merge=False)[source]

Check if the dialect supports MERGE statements for upserts.

static supports_upsert(dialect=None, force_disable_upsert=False)[source]

Check if the dialect supports native upsert operations.

abstractmethod async delete_expired()[source]

Delete all expired sessions from the database.

Return type:

None

Async Session Backend

class advanced_alchemy.extensions.litestar.session.SQLAlchemyAsyncSessionBackend[source]

Bases: SQLAlchemySessionBackendBase[SQLAlchemyAsyncConfig]

Asynchronous SQLAlchemy backend.

async get(session_id, store)[source]

Retrieve data associated with session_id.

Parameters:
  • session_id (str) – The session-ID

  • store (Store) – The store to get the session from (not used in this backend)

Return type:

Optional[bytes]

Returns:

The session data, if existing, otherwise None.

async set(session_id, data, store)[source]

Store data under the session_id for later retrieval.

If there is already data associated with session_id, replace it with data and reset its expiry time

Parameters:
  • session_id (str) – The session-ID.

  • data (bytes) – Serialized session data

  • store (Store) – The store to store the session in (not used in this backend)

Return type:

None

async delete(session_id, store)[source]

Delete the data associated with session_id. Fails silently if no such session-ID exists.

Parameters:
  • session_id (str) – The session-ID

  • store (Store) – The store to delete the session from (not used in this backend)

Return type:

None

async delete_all(store)[source]

Delete all session data.

Return type:

None

async delete_expired()[source]

Delete all expired session from the database.

Return type:

None

Sync Session Backend

class advanced_alchemy.extensions.litestar.session.SQLAlchemySyncSessionBackend[source]

Bases: SQLAlchemySessionBackendBase[SQLAlchemySyncConfig]

Synchronous SQLAlchemy backend.

async get(session_id, store)[source]

Retrieve data associated with session_id.

Parameters:
  • session_id (str) – The session-ID

  • store (Store) – The store to get the session from

Return type:

Optional[bytes]

Returns:

The session data, if existing, otherwise None.

async set(session_id, data, store)[source]

Store data under the session_id for later retrieval.

If there is already data associated with session_id, replace it with data and reset its expiry time

Parameters:
  • session_id (str) – The session-ID

  • data (bytes) – Serialized session data

  • store (Store) – The store to store the session in

Return type:

None

async delete(session_id, store)[source]

Delete the data associated with session_id. Fails silently if no such session-ID exists.

Parameters:
  • session_id (str) – The session-ID

  • store (Store) – The store to delete the session from

Return type:

None

async delete_all()[source]

Delete all session data.

Return type:

None

async delete_expired()[source]

Delete all expired session from the database.

Return type:

None