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:
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:
__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:
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:

EngineT

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.

Parameters:

request (starlette.requests.Request) – The incoming HTTP request.

Returns:

The database session.

Return type:

SessionT

async middleware_dispatch(request: Request, call_next: RequestResponseEndpoint) Response[source]

Middleware dispatch function to handle requests and responses.

Processes the request, invokes the next middleware or route handler, and applies the session handler after the response is generated.

Parameters:
  • request (starlette.requests.Request) – The incoming HTTP request.

  • call_next (starlette.middleware.base.RequestResponseEndpoint) – The next middleware or route handler.

Returns:

The HTTP response.

Return type:

starlette.responses.Response

async on_shutdown() None[source]

Handles the shutdown event by disposing of the SQLAlchemy engine.

Ensures that all connections are properly closed during application shutdown.

Returns:

None