prefect.server.database.dependencies

Injected database interface dependencies

Functions

provide_database_interface

provide_database_interface() -> 'PrefectDBInterface'

Get the current Prefect REST API database interface.

If components of the interface are not set, defaults will be inferred based on the dialect of the connection URL.

inject_db

inject_db(fn: Callable[P, R]) -> Callable[P, R]

Decorator that provides a database interface to a function.

The decorated function must take a db kwarg and if a db is passed when called it will be used instead of creating a new one.

db_injector

db_injector(func: Union[_DBMethod[T, P, R], _DBFunction[P, R]]) -> Union[_Method[T, P, R], _Function[P, R]]

Decorator to inject a PrefectDBInterface instance as the first positional argument to the decorated function.

Unlike inject_db, which injects the database connection as a keyword argument, db_injector adds it explicitly as the first positional argument. This change enhances type hinting by making the dependency on PrefectDBInterface explicit in the function signature.

When decorating a coroutine function, the result will continue to pass the iscoroutinefunction() test.

Args:

  • func: The function or method to decorate.

Returns:

  • A wrapped descriptor object which injects the PrefectDBInterface instance
  • as the first argument to the function or method. This handles method
  • binding transparently.

temporary_database_config

temporary_database_config(tmp_database_config: Optional[BaseDatabaseConfiguration]) -> Generator[None, object, None]

Temporarily override the Prefect REST API database configuration. When the context is closed, the existing database configuration will be restored.

Args:

  • tmp_database_config: Prefect REST API database configuration to inject.

temporary_query_components

temporary_query_components(tmp_queries: Optional['BaseQueryComponents']) -> Generator[None, object, None]

Temporarily override the Prefect REST API database query components. When the context is closed, the existing query components will be restored.

Args:

  • tmp_queries: Prefect REST API query components to inject.

temporary_orm_config

temporary_orm_config(tmp_orm_config: Optional['BaseORMConfiguration']) -> Generator[None, object, None]

Temporarily override the Prefect REST API ORM configuration. When the context is closed, the existing orm configuration will be restored.

Args:

  • tmp_orm_config: Prefect REST API ORM configuration to inject.

temporary_interface_class

temporary_interface_class(tmp_interface_class: Optional[type['PrefectDBInterface']]) -> Generator[None, object, None]

Temporarily override the Prefect REST API interface class When the context is closed, the existing interface will be restored.

Args:

  • tmp_interface_class: Prefect REST API interface class to inject.

temporary_database_interface

temporary_database_interface(tmp_database_config: Optional[BaseDatabaseConfiguration] = None, tmp_queries: Optional['BaseQueryComponents'] = None, tmp_orm_config: Optional['BaseORMConfiguration'] = None, tmp_interface_class: Optional[type['PrefectDBInterface']] = None) -> Generator[None, object, None]

Temporarily override the Prefect REST API database interface.

Any interface components that are not explicitly provided will be cleared and inferred from the Prefect REST API database connection string dialect.

When the context is closed, the existing database interface will be restored.

Args:

  • tmp_database_config: An optional Prefect REST API database configuration to inject.
  • tmp_orm_config: An optional Prefect REST API ORM configuration to inject.
  • tmp_queries: Optional Prefect REST API query components to inject.
  • tmp_interface_class: Optional database interface class to inject

set_database_config

set_database_config(database_config: Optional[BaseDatabaseConfiguration]) -> None

Set Prefect REST API database configuration.

set_query_components

set_query_components(query_components: Optional['BaseQueryComponents']) -> None

Set Prefect REST API query components.

set_orm_config

set_orm_config(orm_config: Optional['BaseORMConfiguration']) -> None

Set Prefect REST API orm configuration.

set_interface_class

set_interface_class(interface_class: Optional[type['PrefectDBInterface']]) -> None

Set Prefect REST API interface class.

Classes

DBInjector