mirror of
https://git.sr.ht/~tsileo/microblog.pub
synced 2024-11-20 05:34:28 +00:00
30 lines
713 B
Python
30 lines
713 B
Python
|
import datetime
|
||
|
from typing import Any
|
||
|
from typing import Generator
|
||
|
|
||
|
from sqlalchemy import create_engine
|
||
|
from sqlalchemy.ext.declarative import declarative_base
|
||
|
from sqlalchemy.orm import Session
|
||
|
from sqlalchemy.orm import sessionmaker
|
||
|
|
||
|
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)
|
||
|
|
||
|
Base: Any = declarative_base()
|
||
|
|
||
|
|
||
|
def now() -> datetime.datetime:
|
||
|
return datetime.datetime.now(datetime.timezone.utc)
|
||
|
|
||
|
|
||
|
def get_db() -> Generator[Session, None, None]:
|
||
|
db = SessionLocal()
|
||
|
try:
|
||
|
yield db
|
||
|
finally:
|
||
|
db.close()
|