sanic

API Reference for the Sanic extensions module

Note

Private methods and attributes are not included in the API reference.


class advanced_alchemy.extensions.sanic.AdvancedAlchemy[source]

Bases: Generic[EngineT, SessionT, SessionMakerT], Extension

Sanic extension for integrating Advanced Alchemy with SQLAlchemy.

Parameters:
__init__(*, sqlalchemy_config, autocommit=None, counters=sanic.helpers._default, session_maker_key='sessionmaker', engine_key='engine', session_key='session')[source]
async session_handler(session, request, response)[source]

Handle the session lifecycle based on the commit strategy.

Parameters:
Return type:

None

get_engine()[source]

Retrieve the SQLAlchemy engine from the app context.

Returns:

The SQLAlchemy engine.

Return type:

EngineT

get_sessionmaker()[source]

Retrieve the session maker.

Returns:

A callable that returns a new session.

Return type:

Callable[[], SessionT]

get_session(request)[source]

Retrieve or create a session for the current request.

Parameters:

request (sanic.Request) – The incoming request.

Returns:

The session associated with the request.

Return type:

SessionT

get_engine_from_request(request)[source]

Retrieve the engine from the request context.

Parameters:

request (sanic.Request) – The incoming request.

Returns:

The SQLAlchemy engine.

Return type:

EngineT

get_sessionmaker_from_request(request)[source]

Retrieve the session maker from the request context.

Parameters:

request (sanic.Request) – The incoming request.

Returns:

The session maker.

Return type:

SessionMakerT

get_session_from_request(request)[source]

Retrieve the session from the request context.

Parameters:

request (sanic.Request) – The incoming request.

Returns:

The session associated with the request.

Return type:

SessionT

startup(bootstrap)[source]

Advanced Alchemy Sanic extension startup hook.

Parameters:

bootstrap (sanic_ext.Extend) – The Sanic extension bootstrap.

Return type:

None

class advanced_alchemy.extensions.sanic.AlembicAsyncConfig[source]

Bases: GenericAlembicConfig

Configuration for an Async Alembic’s Config class.

__init__(script_config='alembic.ini', version_table_name='alembic_versions', version_table_schema=None, script_location='migrations', user_module_prefix='sa.', render_as_batch=True, compare_type=False, template_path='/home/runner/work/advanced-alchemy/advanced-alchemy/advanced_alchemy/alembic/templates')
class advanced_alchemy.extensions.sanic.AlembicSyncConfig[source]

Bases: GenericAlembicConfig

Configuration for Alembic’s synchronous migrations.

For details see: https://alembic.sqlalchemy.org/en/latest/api/config.html

__init__(script_config='alembic.ini', version_table_name='alembic_versions', version_table_schema=None, script_location='migrations', user_module_prefix='sa.', render_as_batch=True, compare_type=False, template_path='/home/runner/work/advanced-alchemy/advanced-alchemy/advanced_alchemy/alembic/templates')
class advanced_alchemy.extensions.sanic.AsyncSessionConfig[source]

Bases: GenericSessionConfig[AsyncConnection, AsyncEngine, AsyncSession]

SQLAlchemy async session config.

sync_session_class

alias of Empty

__init__(autobegin=<class 'advanced_alchemy.utils.dataclass.Empty'>, autoflush=<class 'advanced_alchemy.utils.dataclass.Empty'>, bind=<class 'advanced_alchemy.utils.dataclass.Empty'>, binds=<class 'advanced_alchemy.utils.dataclass.Empty'>, class_=<class 'advanced_alchemy.utils.dataclass.Empty'>, expire_on_commit=<class 'advanced_alchemy.utils.dataclass.Empty'>, info=<class 'advanced_alchemy.utils.dataclass.Empty'>, join_transaction_mode=<class 'advanced_alchemy.utils.dataclass.Empty'>, query_cls=<class 'advanced_alchemy.utils.dataclass.Empty'>, twophase=<class 'advanced_alchemy.utils.dataclass.Empty'>, sync_session_class=<class 'advanced_alchemy.utils.dataclass.Empty'>)
class advanced_alchemy.extensions.sanic.CommitStrategyExecutor[source]

Bases: Protocol

__init__(*args, **kwargs)
class advanced_alchemy.extensions.sanic.SQLAlchemyAsyncConfig[source]

Bases: GenericSQLAlchemyConfig[AsyncEngine, AsyncSession, async_sessionmaker[AsyncSession]]

Async SQLAlchemy Configuration.

Note

The alembic configuration options are documented in the Alembic documentation.

create_engine_callable(**kw)

Callable that creates an AsyncEngine instance or instance of its subclass.

Return type:

sqlalchemy.ext.asyncio.AsyncEngine

session_config: AsyncSessionConfig

Configuration options for the async_sessionmaker.

session_maker_class

alias of async_sessionmaker

__init__(create_engine_callable=<function create_async_engine>, session_config=<factory>, session_maker_class=<class 'sqlalchemy.ext.asyncio.session.async_sessionmaker'>, connection_string=None, engine_config=<factory>, session_maker=None, engine_instance=None, create_all=False, metadata=None, enable_touch_updated_timestamp_listener=True, bind_key=None, alembic_config=<factory>)
alembic_config: AlembicAsyncConfig

Configuration for the SQLAlchemy Alembic migrations.

The configuration options are documented in the Alembic documentation.

get_session()[source]

Get a session from the session maker.

Returns:

An async context manager that yields an AsyncSession.

Return type:

AsyncGenerator[AsyncSession, None]

class advanced_alchemy.extensions.sanic.SQLAlchemySyncConfig[source]

Bases: GenericSQLAlchemyConfig[Engine, Session, sessionmaker[Session]]

Synchronous SQLAlchemy Configuration.

Note

The alembic configuration options are documented in the Alembic documentation.

create_engine_callable(**kwargs)

Callable that creates an Engine instance or instance of its subclass.

Return type:

sqlalchemy.engine.Engine

session_config: SyncSessionConfig

Configuration options for the sessionmaker.

session_maker_class

alias of sessionmaker

__init__(create_engine_callable=<function create_engine>, session_config=<factory>, session_maker_class=<class 'sqlalchemy.orm.session.sessionmaker'>, connection_string=None, engine_config=<factory>, session_maker=None, engine_instance=None, create_all=False, metadata=None, enable_touch_updated_timestamp_listener=True, bind_key=None, alembic_config=<factory>)
alembic_config: AlembicSyncConfig

Configuration for the SQLAlchemy Alembic migrations.

The configuration options are documented in the Alembic documentation.

get_session()[source]

Get a session context manager.

Yields:

Generator[sqlalchemy.orm.Session, None, None] – A context manager yielding an active SQLAlchemy Session.

Return type:

Generator[Session, None, None]

Examples

Using the session context manager:

>>> with config.get_session() as session:
...     session.execute(...)
class advanced_alchemy.extensions.sanic.SyncSessionConfig[source]

Bases: GenericSessionConfig[Connection, Engine, Session]

Configuration for synchronous SQLAlchemy sessions.

__init__(autobegin=<class 'advanced_alchemy.utils.dataclass.Empty'>, autoflush=<class 'advanced_alchemy.utils.dataclass.Empty'>, bind=<class 'advanced_alchemy.utils.dataclass.Empty'>, binds=<class 'advanced_alchemy.utils.dataclass.Empty'>, class_=<class 'advanced_alchemy.utils.dataclass.Empty'>, expire_on_commit=<class 'advanced_alchemy.utils.dataclass.Empty'>, info=<class 'advanced_alchemy.utils.dataclass.Empty'>, join_transaction_mode=<class 'advanced_alchemy.utils.dataclass.Empty'>, query_cls=<class 'advanced_alchemy.utils.dataclass.Empty'>, twophase=<class 'advanced_alchemy.utils.dataclass.Empty'>)