FastAPI#

API Reference for the FastAPI extensions module.

Extension & CLI#

class advanced_alchemy.extensions.fastapi.AdvancedAlchemy(config, app=None)#

Bases: AdvancedAlchemy

AdvancedAlchemy integration for FastAPI applications.

This class manages SQLAlchemy sessions and engine lifecycle within a FastAPI application. It provides middleware for handling transactions based on commit strategies.

provide_service(
service_class,
/,
key=None,
statement=None,
error_messages=Empty,
load=None,
execution_options=None,
uniquify=None,
count_with_window_function=None,
)#

Provides a service instance for dependency injection.

Parameters:
  • service_class (type[AsyncServiceT_co | SyncServiceT_co]) – The service class to provide.

  • key (Optional[str]) – Optional key for the service.

  • statement (Optional[Select[tuple[ModelT]]]) – Optional SQLAlchemy statement.

  • error_messages (Optional[Union[ErrorMessages, EmptyType]]) – Optional error messages.

  • load (Optional[LoadSpec]) – Optional load specification.

  • execution_options (Optional[dict[str, Any]]) – Optional execution options.

  • uniquify (bool | None) – Optional flag to uniquify the service.

  • count_with_window_function (bool | None) – Optional flag to use window function for counting.

Returns:

A callable that returns an async generator for async services or a generator for sync services.

Return type:

Callable[…, Union[AsyncGenerator[AsyncServiceT_co, None], Generator[SyncServiceT_co, None, None]]]

static provide_filters(config, /, dep_defaults=None)#

Provides filters for dependency injection.

Parameters:
Returns:

A callable that returns an async generator for async filters or a generator for sync filters.

Return type:

Callable[…, list[FilterTypes]]

advanced_alchemy.extensions.fastapi.assign_cli_group(app)#
advanced_alchemy.extensions.fastapi.get_database_migration_plugin(app)#

Retrieve the Advanced Alchemy extension from a FastAPI application instance.

Parameters:

app (FastAPI) – The FastAPI application instance.

Raises:

ImproperConfigurationError – If the Advanced Alchemy extension is not properly configured.

Returns:

The Advanced Alchemy extension instance.

Return type:

AdvancedAlchemy

Configuration#

FastAPI re-exports the Starlette configuration classes:

Dependency Providers#

advanced_alchemy.extensions.fastapi.providers.provide_service(
service_class,
/,
extension,
key=None,
statement=None,
error_messages=Empty,
load=None,
execution_options=None,
uniquify=None,
count_with_window_function=None,
)[source]#

Create a dependency provider for a service.

This function creates a generator-based dependency that manages the service lifecycle. The generator owns the session lifecycle and handles commit/rollback/close operations to ensure proper connection pool management (especially important for asyncpg).

Returns:

A dependency provider for the service.

Return type:

Callable[[…], AsyncGenerator[AsyncServiceT_co, None] | Generator[SyncServiceT_co, None, None]]

advanced_alchemy.extensions.fastapi.providers.provide_filters(config, dep_defaults=DEPENDENCY_DEFAULTS)[source]#

Create FastAPI dependency providers for filters based on the provided configuration.

Returns:

A FastAPI dependency provider function that aggregates multiple filter dependencies.

Return type:

Callable[[…], list[BeforeAfter | OnBeforeAfter | BooleanFilter | ChoicesFilter[Any] | CollectionFilter[Any] | LimitOffset | NullFilter | NotNullFilter | OrderBy | SearchFilter | NotInCollectionFilter[Any] | NotInSearchFilter | ExistsFilter | NotExistsFilter | ComparisonFilter | MultiFilter | FilterGroup]]

class advanced_alchemy.extensions.fastapi.providers.DependencyDefaults[source]#

Bases: object

Default values for dependency generation.

CREATED_FILTER_DEPENDENCY_KEY: str = 'created_filter'#

Key for the created filter dependency.

ID_FILTER_DEPENDENCY_KEY: str = 'id_filter'#

Key for the id filter dependency.

LIMIT_OFFSET_FILTER_DEPENDENCY_KEY: str = 'limit_offset_filter'#

Key for the limit offset dependency.

UPDATED_FILTER_DEPENDENCY_KEY: str = 'updated_filter'#

Key for the updated filter dependency.

ORDER_BY_FILTER_DEPENDENCY_KEY: str = 'order_by_filter'#

Key for the order by dependency.

SEARCH_FILTER_DEPENDENCY_KEY: str = 'search_filter'#

Key for the search filter dependency.

DEFAULT_PAGINATION_SIZE: int = 20#

Default pagination size.

advanced_alchemy.extensions.fastapi.providers.DEPENDENCY_DEFAULTS#

Default values for dependency generation.