starlette¶
Note
More details soon
- class advanced_alchemy.extensions.starlette.CommitStrategyExecutor[source]¶
Bases:
Protocol
Protocol for commit strategy executors.
Defines the callable signature for commit strategy functions.
- Parameters:
session¶ (sqlalchemy.orm.Session | sqlalchemy.ext.asyncio.AsyncSession) – The database session.
response¶ (starlette.responses.Response) – The HTTP response.
- Returns:
None
- __init__(*args, **kwargs)¶
- class advanced_alchemy.extensions.starlette.StarletteAdvancedAlchemy[source]¶
Bases:
Generic
[EngineT
,SessionT
]AdvancedAlchemy integration for Starlette applications.
This class manages SQLAlchemy sessions and engine lifecycle within a Starlette application. It provides middleware for handling transactions based on commit strategies.
- Parameters:
config¶ (advanced_alchemy.config.asyncio.SQLAlchemyAsyncConfig | advanced_alchemy.config.sync.SQLAlchemySyncConfig) – The SQLAlchemy configuration.
autocommit¶ (advanced_alchemy.config.types.CommitStrategy | None) – The commit strategy to use. Defaults to None.
app¶ (starlette.applications.Starlette | None) – The Starlette application instance. Defaults to None.
- __init__(config: SQLAlchemyAsyncConfig, autocommit: CommitStrategy | None = None, app: Starlette | None = None) None [source]¶
- __init__(config: SQLAlchemySyncConfig, autocommit: CommitStrategy | None = None, app: Starlette | None = None) None
- init_app(app: Starlette) None [source]¶
Initializes the Starlette application with SQLAlchemy engine and sessionmaker.
Sets up middleware and shutdown handlers for managing the database engine.
- Parameters:
app¶ (starlette.applications.Starlette) – The Starlette application instance.
- async session_handler(session: Session | AsyncSession, request: Request, response: Response) None [source]¶
Handles the session after a request is processed.
Applies the commit strategy and ensures the session is closed.
- Parameters:
session¶ (sqlalchemy.orm.Session | sqlalchemy.ext.asyncio.AsyncSession) – The database session.
request¶ (starlette.requests.Request) – The incoming HTTP request.
response¶ (starlette.responses.Response) – The outgoing HTTP response.
- Returns:
None
- property app: Starlette¶
Returns the Starlette application instance.
- Raises:
advanced_alchemy.exceptions.ImproperConfigurationError – If the application is not initialized.
- Returns:
The Starlette application instance.
- Return type:
starlette.applications.Starlette
- get_engine() sqlalchemy.engine.Engine [source]¶
Retrieves the SQLAlchemy engine from the application state.
- Returns:
The SQLAlchemy engine instance.
- Return type:
- get_sessionmaker() Callable[[], sqlalchemy.orm.Session] [source]¶
Retrieves the sessionmaker from the application state.
- Returns:
A callable that returns a new session instance.
- Return type:
Callable[[], SessionT]
- get_session(request: Request) sqlalchemy.orm.Session [source]¶
Retrieves or creates a database session for the current request.