mirror of
https://git.sr.ht/~tsileo/microblog.pub
synced 2024-11-19 21:24:27 +00:00
32 lines
1 KiB
Python
32 lines
1 KiB
Python
import datetime
|
|
from typing import Any
|
|
from typing import AsyncGenerator
|
|
|
|
from sqlalchemy import create_engine
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
from sqlalchemy.ext.asyncio import create_async_engine
|
|
from sqlalchemy.ext.declarative import declarative_base
|
|
from sqlalchemy.orm import sessionmaker
|
|
|
|
from app.config import DB_PATH
|
|
from app.config import SQLALCHEMY_DATABASE_URL
|
|
|
|
engine = create_engine(
|
|
SQLALCHEMY_DATABASE_URL, connect_args={"check_same_thread": False}
|
|
)
|
|
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
|
|
|
DATABASE_URL = f"sqlite+aiosqlite:///{DB_PATH}"
|
|
async_engine = create_async_engine(DATABASE_URL, future=True, echo=False)
|
|
async_session = sessionmaker(async_engine, class_=AsyncSession, expire_on_commit=False)
|
|
|
|
Base: Any = declarative_base()
|
|
|
|
|
|
def now() -> datetime.datetime:
|
|
return datetime.datetime.now(datetime.timezone.utc)
|
|
|
|
|
|
async def get_db_session() -> AsyncGenerator[AsyncSession, None]:
|
|
async with async_session() as session:
|
|
yield session
|