plugin

class advanced_alchemy.extensions.litestar.plugins.EngineConfig[source]

Bases: EngineConfig

Configuration for SQLAlchemy’s Engine.

For details see: https://docs.sqlalchemy.org/en/20/core/engines.html

json_deserializer(target_type: type[T] | EmptyType = _EmptyEnum.EMPTY, type_decoders: TypeDecodersSequence | None = None, strict: bool = True) Any

For dialects that support the JSON datatype, this is a Python callable that will convert a JSON string to a Python object. By default, this is set to Litestar’s decode_json function.

__init__(connect_args: dict[Any, Any] | EmptyType = <class 'advanced_alchemy.utils.dataclass.Empty'>, echo: _EchoFlagType | EmptyType = <class 'advanced_alchemy.utils.dataclass.Empty'>, echo_pool: _EchoFlagType | EmptyType = <class 'advanced_alchemy.utils.dataclass.Empty'>, enable_from_linting: bool | EmptyType = <class 'advanced_alchemy.utils.dataclass.Empty'>, execution_options: Mapping[str, Any] | EmptyType = <class 'advanced_alchemy.utils.dataclass.Empty'>, hide_parameters: bool | EmptyType = <class 'advanced_alchemy.utils.dataclass.Empty'>, insertmanyvalues_page_size: int | EmptyType = <class 'advanced_alchemy.utils.dataclass.Empty'>, isolation_level: IsolationLevel | EmptyType = <class 'advanced_alchemy.utils.dataclass.Empty'>, json_deserializer: Callable[[str], Any] = <function decode_json>, json_serializer: Callable[[Any], str] = <function serializer>, label_length: int | None | EmptyType = <class 'advanced_alchemy.utils.dataclass.Empty'>, logging_name: str | EmptyType = <class 'advanced_alchemy.utils.dataclass.Empty'>, max_identifier_length: int | None | EmptyType = <class 'advanced_alchemy.utils.dataclass.Empty'>, max_overflow: int | EmptyType = <class 'advanced_alchemy.utils.dataclass.Empty'>, module: Any | None | EmptyType = <class 'advanced_alchemy.utils.dataclass.Empty'>, paramstyle: _ParamStyle | None | EmptyType = <class 'advanced_alchemy.utils.dataclass.Empty'>, pool: Pool | None | EmptyType = <class 'advanced_alchemy.utils.dataclass.Empty'>, poolclass: type[Pool] | None | EmptyType = <class 'advanced_alchemy.utils.dataclass.Empty'>, pool_logging_name: str | EmptyType = <class 'advanced_alchemy.utils.dataclass.Empty'>, pool_pre_ping: bool | EmptyType = <class 'advanced_alchemy.utils.dataclass.Empty'>, pool_size: int | EmptyType = <class 'advanced_alchemy.utils.dataclass.Empty'>, pool_recycle: int | EmptyType = <class 'advanced_alchemy.utils.dataclass.Empty'>, pool_reset_on_return: Literal['rollback', 'commit'] | EmptyType = <class 'advanced_alchemy.utils.dataclass.Empty'>, pool_timeout: int | EmptyType = <class 'advanced_alchemy.utils.dataclass.Empty'>, pool_use_lifo: bool | EmptyType = <class 'advanced_alchemy.utils.dataclass.Empty'>, plugins: list[str] | EmptyType = <class 'advanced_alchemy.utils.dataclass.Empty'>, query_cache_size: int | EmptyType = <class 'advanced_alchemy.utils.dataclass.Empty'>, use_insertmanyvalues: bool | EmptyType = <class 'advanced_alchemy.utils.dataclass.Empty'>) None
json_serializer() str

For dialects that support the JSON datatype, this is a Python callable that will render a given object as JSON. By default, Litestar’s encode_json function is used.

class advanced_alchemy.extensions.litestar.plugins.SQLAlchemyAsyncConfig[source]

Bases: SQLAlchemyAsyncConfig

Litestar Async SQLAlchemy Configuration.

before_send_handler: BeforeMessageSendHookHandler | None | Literal['autocommit', 'autocommit_include_redirects'] = None

Handler to call before the ASGI message is sent.

The handler should handle closing the session stored in the ASGI scope, if it’s still open, and committing and uncommitted data.

engine_dependency_key: str = 'db_engine'

Key to use for the dependency injection of database engines.

session_dependency_key: str = 'db_session'

Key to use for the dependency injection of database sessions.

engine_app_state_key: str = 'db_engine'

Key under which to store the SQLAlchemy engine in the application State instance.

session_maker_app_state_key: str = 'session_maker_class'

Key under which to store the SQLAlchemy sessionmaker in the application State instance.

session_scope_key: str = '_sqlalchemy_db_session'

Key under which to store the SQLAlchemy scope in the application.

engine_config: EngineConfig

Configuration for the SQLAlchemy engine.

The configuration options are documented in the SQLAlchemy documentation.

set_default_exception_handler: bool = True

Sets the default exception handler on application start.

__init__(create_engine_callable: Callable[[str], AsyncEngine] = <function create_async_engine>, session_config: AsyncSessionConfig = <factory>, session_maker_class: type[async_sessionmaker[AsyncSession]] = <class 'sqlalchemy.ext.asyncio.session.async_sessionmaker'>, connection_string: str | None = None, engine_config: EngineConfig = <factory>, session_maker: Callable[[], SessionT] | None = None, engine_instance: EngineT | None = None, create_all: bool = False, metadata: MetaData | None = None, enable_touch_updated_timestamp_listener: bool = True, bind_key: str | None = None, alembic_config: AlembicAsyncConfig = <factory>, before_send_handler: BeforeMessageSendHookHandler | None | Literal['autocommit', 'autocommit_include_redirects'] = None, engine_dependency_key: str = 'db_engine', session_dependency_key: str = 'db_session', engine_app_state_key: str = 'db_engine', session_maker_app_state_key: str = 'session_maker_class', session_scope_key: str = '_sqlalchemy_db_session', set_default_exception_handler: bool = True) None
create_session_maker() Callable[[], AsyncSession][source]

Get a session maker. If none exists yet, create one.

Returns:

Session factory used by the plugin.

session_config: AsyncSessionConfig

Configuration options for the async_sessionmaker.

alembic_config: AlembicAsyncConfig

Configuration for the SQLAlchemy Alembic migrations.

The configuration options are documented in the Alembic documentation.

provide_engine(state: State) AsyncEngine[source]

Create an engine instance.

Parameters:

state – The Litestar.state instance.

Returns:

An engine instance.

provide_session(state: State, scope: Scope) AsyncSession[source]

Create a session instance.

Parameters:
  • state – The Litestar.state instance.

  • scope – The current connection’s scope.

Returns:

A session instance.

property signature_namespace: dict[str, Any]

Return the plugin’s signature namespace.

Returns:

A string keyed dict of names to be added to the namespace for signature forward reference resolution.

async create_all_metadata(app: Litestar) None[source]

Create all metadata

Parameters:

app (Litestar) – The Litestar instance

create_app_state_items() dict[str, Any][source]

Key/value pairs to be stored in application state.

update_app_state(app: Litestar) None[source]

Set the app state with engine and session.

Parameters:

app – The Litestar instance.

class advanced_alchemy.extensions.litestar.plugins.SQLAlchemyInitPlugin[source]

Bases: InitPluginProtocol, CLIPluginProtocol, SlotsBase

SQLAlchemy application lifecycle configuration.

__init__(config: SQLAlchemyAsyncConfig | SQLAlchemySyncConfig | Sequence[SQLAlchemyAsyncConfig | SQLAlchemySyncConfig]) None[source]

Initialize SQLAlchemyPlugin.

Parameters:

config – configure DB connection and hook handlers and dependencies.

on_cli_init(cli: Group) None[source]

Called when the CLI is initialized.

This can be used to extend or override existing commands.

Parameters:

cli – The root click.Group of the Litestar CLI

Examples

from litestar import Litestar
from litestar.plugins import CLIPluginProtocol
from click import Group


class CLIPlugin(CLIPluginProtocol):
    def on_cli_init(self, cli: Group) -> None:
        @cli.command()
        def is_debug_mode(app: Litestar):
            print(app.debug)


app = Litestar(plugins=[CLIPlugin()])
on_app_init(app_config: AppConfig) AppConfig[source]

Configure application for use with SQLAlchemy.

Parameters:

app_config – The AppConfig instance.

class advanced_alchemy.extensions.litestar.plugins.SQLAlchemyPlugin[source]

Bases: InitPluginProtocol, SlotsBase

A plugin that provides SQLAlchemy integration.

__init__(config: SQLAlchemyAsyncConfig | SQLAlchemySyncConfig | list[SQLAlchemyAsyncConfig | SQLAlchemySyncConfig]) None[source]

Initialize SQLAlchemyPlugin.

Parameters:

config – configure DB connection and hook handlers and dependencies.

on_app_init(app_config: AppConfig) AppConfig[source]

Configure application for use with SQLAlchemy.

Parameters:

app_config – The AppConfig instance.

class advanced_alchemy.extensions.litestar.plugins.SQLAlchemySerializationPlugin[source]

Bases: SerializationPluginProtocol, SlotsBase

__init__() None[source]
supports_type(field_definition: FieldDefinition) bool[source]

Given a value of indeterminate type, determine if this value is supported by the plugin.

Parameters:

field_definition – A parsed type.

Returns:

Whether the type is supported by the plugin.

create_dto_for_type(field_definition: FieldDefinition) type[SQLAlchemyDTO[Any]][source]

Given a parsed type, create a DTO class.

Parameters:

field_definition – A parsed type.

Returns:

A DTO class.

class advanced_alchemy.extensions.litestar.plugins.SQLAlchemySyncConfig[source]

Bases: SQLAlchemySyncConfig

Litestar Sync SQLAlchemy Configuration.

before_send_handler: BeforeMessageSendHookHandler | None | Literal['autocommit', 'autocommit_include_redirects'] = None

Handler to call before the ASGI message is sent.

The handler should handle closing the session stored in the ASGI scope, if it’s still open, and committing and uncommitted data.

engine_dependency_key: str = 'db_engine'

Key to use for the dependency injection of database engines.

session_dependency_key: str = 'db_session'

Key to use for the dependency injection of database sessions.

engine_app_state_key: str = 'db_engine'

Key under which to store the SQLAlchemy engine in the application State instance.

session_maker_app_state_key: str = 'session_maker_class'

Key under which to store the SQLAlchemy sessionmaker in the application State instance.

session_scope_key: str = '_sqlalchemy_db_session'

Key under which to store the SQLAlchemy scope in the application.

engine_config: EngineConfig

Configuration for the SQLAlchemy engine.

The configuration options are documented in the SQLAlchemy documentation.

set_default_exception_handler: bool = True

Sets the default exception handler on application start.

__init__(create_engine_callable: Callable[[str], Engine] = <function create_engine>, session_config: SyncSessionConfig = <factory>, session_maker_class: type[sessionmaker[Session]] = <class 'sqlalchemy.orm.session.sessionmaker'>, connection_string: str | None = None, engine_config: EngineConfig = <factory>, session_maker: Callable[[], SessionT] | None = None, engine_instance: EngineT | None = None, create_all: bool = False, metadata: MetaData | None = None, enable_touch_updated_timestamp_listener: bool = True, bind_key: str | None = None, alembic_config: AlembicSyncConfig = <factory>, before_send_handler: BeforeMessageSendHookHandler | None | Literal['autocommit', 'autocommit_include_redirects'] = None, engine_dependency_key: str = 'db_engine', session_dependency_key: str = 'db_session', engine_app_state_key: str = 'db_engine', session_maker_app_state_key: str = 'session_maker_class', session_scope_key: str = '_sqlalchemy_db_session', set_default_exception_handler: bool = True) None
create_session_maker() Callable[[], Session][source]

Get a session maker. If none exists yet, create one.

Returns:

Session factory used by the plugin.

session_config: SyncSessionConfig

Configuration options for the sessionmaker.

alembic_config: AlembicSyncConfig

Configuration for the SQLAlchemy Alembic migrations.

The configuration options are documented in the Alembic documentation.

provide_engine(state: State) Engine[source]

Create an engine instance.

Parameters:

state – The Litestar.state instance.

Returns:

An engine instance.

provide_session(state: State, scope: Scope) Session[source]

Create a session instance.

Parameters:
  • state – The Litestar.state instance.

  • scope – The current connection’s scope.

Returns:

A session instance.

property signature_namespace: dict[str, Any]

Return the plugin’s signature namespace.

Returns:

A string keyed dict of names to be added to the namespace for signature forward reference resolution.

create_all_metadata(app: Litestar) None[source]

Create all metadata

Parameters:

app (Litestar) – The Litestar instance

create_app_state_items() dict[str, Any][source]

Key/value pairs to be stored in application state.

update_app_state(app: Litestar) None[source]

Set the app state with engine and session.

Parameters:

app – The Litestar instance.