2023-08-14 02:08:08 +08:00
|
|
|
from asyncio import current_task
|
|
|
|
|
|
|
|
from sqlalchemy import create_engine
|
2024-09-22 23:15:27 +08:00
|
|
|
from sqlalchemy.ext.asyncio import (
|
|
|
|
AsyncSession,
|
|
|
|
async_scoped_session,
|
|
|
|
async_sessionmaker,
|
|
|
|
create_async_engine,
|
|
|
|
)
|
2023-08-14 02:08:08 +08:00
|
|
|
|
2024-01-27 01:08:02 +08:00
|
|
|
from felicity.core import get_settings
|
2024-01-26 01:52:03 +08:00
|
|
|
|
|
|
|
settings = get_settings()
|
|
|
|
|
2024-07-28 03:52:31 +08:00
|
|
|
engine = create_engine(
|
|
|
|
(
|
|
|
|
settings.SQLALCHEMY_TEST_DATABASE_URI
|
|
|
|
if settings.TESTING
|
|
|
|
else settings.SQLALCHEMY_DATABASE_URI
|
|
|
|
),
|
|
|
|
)
|
2023-08-14 02:08:08 +08:00
|
|
|
async_engine = create_async_engine(
|
2024-07-28 03:52:31 +08:00
|
|
|
(
|
|
|
|
settings.SQLALCHEMY_TEST_DATABASE_URI
|
|
|
|
if settings.TESTING
|
|
|
|
else settings.SQLALCHEMY_DATABASE_URI
|
|
|
|
),
|
2023-08-14 02:08:08 +08:00
|
|
|
pool_pre_ping=True,
|
|
|
|
echo=False,
|
|
|
|
future=True,
|
|
|
|
)
|
|
|
|
# async_session_factory can be used directly using: async with async_session_factory() as session: ...
|
2024-07-21 15:06:51 +08:00
|
|
|
async_session = async_sessionmaker(
|
2024-07-28 03:52:31 +08:00
|
|
|
bind=async_engine, expire_on_commit=False, autoflush=False, class_=AsyncSession
|
2024-07-21 15:06:51 +08:00
|
|
|
)
|
2024-07-24 04:30:01 +08:00
|
|
|
AsyncSessionScoped = async_scoped_session(async_session, scopefunc=current_task)
|