-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
27 lines (17 loc) 路 960 Bytes
/
Copy pathdatabase.py
File metadata and controls
27 lines (17 loc) 路 960 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine
from sqlalchemy.orm import DeclarativeBase
from auth.config import settings
# DB_URL_SQLITE = "sqlite+aiosqlite:///./blog.db"
# engine = create_async_engine(DB_URL_SQLITE, connect_args={"check_same_thread": False},)
import sys, asyncio
if sys.platform == 'win32':
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
engine = create_async_engine(settings.database_url, echo=True)
# SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
AsyncSessionLocal = async_sessionmaker(bind=engine, class_=AsyncSession, expire_on_commit=False)
#when an object expires(after commit), sqlite tryies to reload it lazily. But async sqlite does not support lazy loads, hence expire_on_commit=False
class Base(DeclarativeBase):
pass
async def get_db():
async with AsyncSessionLocal() as db_session:
yield db_session