SQLAlchemy

The SQLAlchemy module makes it easy to set up database connections, providing AsyncEngine and async_sessionmaker as services in the dependency injection context, as well as AsyncSession tied to the current request.

Usage

Install sqlalchemy extra and a database driver that supports async:

$ pip install zayt-contrib[sqlalchemy] aiosqlite asyncpg aiomysql oracledb

With database drivers are installed, we can define the connections in the configuration file:

modules = [
    "zayt_contrib.sqlalchemy" # (1)
]

[sqlalchemy.connections.default] # (2)
url = "sqlite+aiosqlite:///var/db.sqlite3"

[sqlalchemy.connections.postgres] # (3)
url = "postgresql+asyncpg://user:pass@localhost/dbname"

[sqlalchemy.connections.mysql] # (4)
url = "mysql+aiomysql://user:pass@localhost/dbname"

[sqlalchemy.connections.oracle] # (5)
url = "oracle+oracledb_async://user:pass@localhost/DBNAME"
# "oracle+oracledb_async://user:pass@localhost/?service_name=DBNAME"

Note

  1. Activate the sqlalchemy module

  2. Connection will be registered without a name

  3. Connection registered with name “postgres”

  4. Connection registered with name “mysql”

  5. Connection registered with name “oracle”

Once we define the connections, we can inject AsyncEngine into our services. For each connection, an instance of AsyncEngine will be registered, the "" connection will be registered without a name, and the others will be registered with their respective names.

from typing import Annotated
from sqlalchemy.ext.asyncio import AsyncEngine
from zayt.di import service, Inject


@service
class MyService:
    # default service
    engine: Annotated[AsyncEngine, Inject]

    # named services
    engine_postgres: Annotated[AsyncEngine, Inject("postgres")]
    engine_mysql: Annotated[AsyncEngine, Inject("mysql")]
    engine_oracle: Annotated[AsyncEngine, Inject("oracle")]

Scoped Session

In handlers, the AsyncSession can be injected as a service scoped to the current request. Each request will be injected its own session object:

from sqlalchemy import text
from sqlalchemy.ext.asyncio import AsyncSession
from asgikit import Request
from zayt.web import get


@get
async def handler(request: Request, session: AsyncSession):
    result = await session.scalar(text("select 'ok'"))
    await request.respond_text(result)

Configuration

Database connections can also be defined with username and password separated from the url, or even with individual components:

[sqlalchemy.connections.default]
drivername = "sqlite+aiosqlite"
database = "/var/db.sqlite3"

[sqlalchemy.connections.postgres] # (1)
url = "postgresql+asyncpg://localhost/dbname"
username = "user"
password = "pass"

[sqlalchemy.connections.mysql] # (2)
drivername = "mysql+aiomysql"
host = "localhost"
port = 3306
database = "dbname"
username = "user"
password = "pass"

[sqlalchemy.connections.oracle] # (3)
drivername = "oracle+oracledb_async"
host = "localhost"
port = 1521
username = "user"
password = "pass"
query = {"service_name" = "DBNAME"}

Note

  1. Username and password separated from the database url

  2. Each component defined individually

  3. Query parameters can be defined in a dict

Working with async_sessionmaker

Different from the AsyncEngine, only one async_sessionmaker is created. We can bind specific subclasses of DeclarativeBase through the sqlalchemy.session.binds configuration, otherwise it is bound to the default connection.

application/model.py
from sqlalchemy.orm import DeclarativeBase


class Base(DeclarativeBase):
    pass


class OtherBase(DeclarativeBase):
    pass
configuration/settings.toml
[sqlalchemy.connections.default]
url = "sqlite+aiosqlite://db1.sqlite3"

[sqlalchemy.connections.other]
url = "sqlite+aiosqlite://db2.sqlite3"

[sqlalchemy.session.binds]
"application.model.Base" = ""
"application.model.OtherBase" = "other"

Example

application/handler.py
from sqlalchemy import select
from sqlalchemy.ext.asyncio import async_sessionmaker, AsyncEngine, AsyncSession

from asgikit import Request
from zayt.web import get

from .model import Base, MyModel


@get
async def index(
    request: Request,
    engine: AsyncEngine,
    sessionmaker: async_sessionmaker,
    session: AsyncSession,
):
    async with engine.begin() as conn:
        await conn.run_sync(Base.metadata.create_all)

    async with sessionmaker() as session:
        my_model = MyModel(name="MyModel")
        session.add(my_model)
        await session.commit()

    my_model = await session.scalar(select(MyModel).limit(1))

    await request.respond_json({
        "id": my_model.id,
        "name": my_model.name,
    })
application/model.py
from sqlalchemy import Column, Integer, String
from sqlalchemy.orm import DeclarativeBase


class Base(DeclarativeBase):
    pass


class MyModel(Base):
    __tablename__ = 'my_model'
    id = Column(Integer, primary_key=True, autoincrement=True)
    name = Column(String(length=100))

    def __repr__(self):
        return f"<MyModel(id={self.id}, name={self.name})>"
configuration/settings.py
[sqlalchemy.connections.default]
url = "sqlite+aiosqlite://:memory:"

Configuration

The available options are shown below:

[sqlalchemy.session] # (1)
bind = "application.model.Base"
class = "sqlalchemy.ext.asyncio.AsyncSession"
autoflush = true
expire_on_commit = true
autobegin = true
twophase = false
enable_baked_queries = true
info = {}
query_cls = "path.to.QueryClass"
join_transaction_mode = "conditional_savepoint" # "rollback_only", "control_fully", "create_savepoint
close_resets_only = true

[sqlalchemy.session.binds] # (2)
"application.model.Base" = "default"
"application.model.OtherBase" = "other"

[sqlalchemy.connections.default] # (3)
host = "localhost"
port = 5432
database = "dbname"
username = "user"
password = "pass"
query = {}
drivername = ""
url = ""
connect_args = {}
echo = false
echo_pool = false
enable_from_linting = false
hide_parameters = false
insertmanyvalues_page_size = 1
isolation_level = "SERIALIZABLE" # "REPEATABLE READ", "READ COMMITTED", "READ UNCOMMITTED", "AUTOCOMMIT"
json_deserializer = "json.loads"
json_serializer = "json.dumps"
label_length = 1
logging_name = ""
max_identifier_length = 1
max_overflow = 1
module = "path.to.module"
paramstyle = "qmark" # "numeric", "named", "format", "pyformat
poolclass = "path.to.PoolClass"
pool_logging_name = ""
pool_pre_ping = false
pool_size = 1
pool_recycle = 3600
pool_reset_on_return = "rollback" # "commit
pool_timeout = 1
pool_use_lifo = false
plugins = ["plugin"]
query_cache_size = 1
use_insertmanyvalues = false

[sqlalchemy.connections.default.execution_options] # (4)
logging_token = ""
isolation_level = ""
no_parameters = false
stream_results = false
max_row_buffer = 1
yield_per = 1
insertmanyvalues_page_size = 1
schema_translate_map = {
    "" = "my_schema"
    "some_schema" = "other_schema"
}

Note

  1. session.options values are describe in sqlalchemy.ext.asyncio.async_sessionmaker

  2. Binds subclasses of DeclarativeBase to connection names defined in connections

  3. connection.options values are described in sqlalchemy.create_engine, except host, port, database, username and password

  4. execution_options values are describe in sqlalchemy.engine.Connection.execution_options