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
andSessionModelMixin
- 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:
base – Either a
sqlalchemy.orm.registry
orDeclarativeBase
model – SQLAlchemy model to register
- Returns:
A mapped model subclassing
SessionModelMixin
, and registered inregistry
- 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 configuredSQLAlchemyPlugin
.
- __init__(config: SQLAlchemyBackendConfig) None #
Initialize
BaseSQLAlchemyBackend
.- Parameters:
config – An instance of
SQLAlchemyBackendConfig
- 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 thesession_id
for later retrieval.If there is already data associated with
session_id
, replace it withdata
and reset its expiry time- Parameters:
session_id – The session-ID.
data – Serialized session data
- 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 thesession_id
for later retrieval.If there is already data associated with
session_id
, replace it withdata
and reset its expiry time- Parameters:
session_id – The session-ID
data – Serialized session data
- Returns:
None
- class starlite.middleware.session.sqlalchemy_backend.SQLAlchemyBackendConfig#
Bases:
ServerSideSessionConfig
Configuration for
SQLAlchemyBackend
andAsyncSQLAlchemyBackend
- classmethod validate_plugin_config(value: SQLAlchemyPlugin) SQLAlchemyPlugin #
Check if the SQLAlchemyPlugin is configured.