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:
sqlalchemy_config¶ (advanced_alchemy.config.sync.SQLAlchemySyncConfig | advanced_alchemy.config.asyncio.SQLAlchemyAsyncConfig) – Configuration for SQLAlchemy.
autocommit¶ (advanced_alchemy.config.types.CommitStrategy | None) – Strategy for committing transactions. Defaults to None.
counters¶ (sanic.helpers.Default | bool | None) – Enables or disables counters. Defaults to sanic.helpers._default.
session_maker_key¶ (str| None) – Key for the session maker in app context. Defaults to “sessionmaker”.
engine_key¶ (str| None) – Key for the engine in app context. Defaults to “engine”.
session_key¶ (str| None) – Key for the session in request context. Defaults to “session”.
- __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:
session¶ (sqlalchemy.orm.Session | sqlalchemy.ext.asyncio.AsyncSession) – The current session.
request¶ (sanic.Request) – The incoming request.
response¶ (sanic.HTTPResponse) – The outgoing response.
- Return type:
- get_engine()[source]
Retrieve the SQLAlchemy engine from the app context.
- Returns:
The SQLAlchemy engine.
- Return type:
- 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.
- get_engine_from_request(request)[source]
Retrieve the engine from the request context.
- 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:
- get_session_from_request(request)[source]
Retrieve the session from the request context.
- 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:
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'>)