sqlalchemy_backend#

class starlite.middleware.session.sqlalchemy_backend.SessionModelMixin#

Bases: object

Mixin for session storage.

expired#

SQL-Expression to check if the session has expired.

class starlite.middleware.session.sqlalchemy_backend.SessionModel#

Bases: SessionModelMixin

Session storage model.

starlite.middleware.session.sqlalchemy_backend.create_session_model(base: Type[Any], table_name: str = 'session') Type[SessionModelMixin]#

Dynamically generate a session storage model and register it with the declarative base.

Parameters:
  • base – SQLAlchemy declarative base

  • table_name – Alternative table name

Returns:

A mapped model subclassing base and SessionModelMixin

starlite.middleware.session.sqlalchemy_backend.register_session_model(base: Union[registry, Any], model: Type[SessionModelT]) Type[SessionModelT]#

Map and register a pre-existing model subclassing SessionModelMixin with a declarative base or registry.

Parameters:
Returns:

A mapped model subclassing SessionModelMixin, and registered in registry

class starlite.middleware.session.sqlalchemy_backend.BaseSQLAlchemyBackend#

Bases: Generic[AnySASessionT], ServerSideBackend[SQLAlchemyBackendConfig], ABC

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: SQLAlchemyBackendConfig) None#

Initialize BaseSQLAlchemyBackend.

Parameters:

config – An instance of SQLAlchemyBackendConfig

abstract async delete_expired() None#

Delete all expired sessions from the database.

class starlite.middleware.session.sqlalchemy_backend.AsyncSQLAlchemyBackend#

Bases: BaseSQLAlchemyBackend[AsyncSession]

Asynchronous SQLAlchemy backend.

async get(session_id: str) Optional[bytes]#

Retrieve data associated with session_id.

Parameters:

session_id – The session-ID

Returns:

The session data, if existing, otherwise None.

async set(session_id: str, data: bytes) None#

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 – The session-ID.

  • data – Serialized session data

Returns:

None

async delete(session_id: str) None#

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

Parameters:

session_id – The session-ID

Returns:

None

async delete_all() None#

Delete all session data.

Returns:

None

async delete_expired() None#

Delete all expired session from the database.

Returns:

None

class starlite.middleware.session.sqlalchemy_backend.SQLAlchemyBackend#

Bases: BaseSQLAlchemyBackend[Session]

Synchronous SQLAlchemy backend.

async get(session_id: str) Optional[bytes]#

Retrieve data associated with session_id.

Parameters:

session_id – The session-ID

Returns:

The session data, if existing, otherwise None.

async set(session_id: str, data: bytes) None#

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 – The session-ID

  • data – Serialized session data

Returns:

None

async delete(session_id: str) None#

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

Parameters:

session_id – The session-ID

Returns:

None

async delete_all() None#

Delete all session data.

Returns:

None

async delete_expired() None#

Delete all expired session from the database.

Returns:

None

class starlite.middleware.session.sqlalchemy_backend.SQLAlchemyBackendConfig#

Bases: ServerSideSessionConfig

Configuration for SQLAlchemyBackend and AsyncSQLAlchemyBackend

classmethod validate_plugin_config(value: SQLAlchemyPlugin) SQLAlchemyPlugin#

Check if the SQLAlchemyPlugin is configured.