Первый коммит
This commit is contained in:
29
app/modules/shared/db.py
Normal file
29
app/modules/shared/db.py
Normal file
@@ -0,0 +1,29 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
|
||||
from sqlalchemy import create_engine
|
||||
from sqlalchemy.engine import Engine
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
from sqlalchemy.pool import NullPool
|
||||
|
||||
_ENGINE: Engine | None = None
|
||||
_SESSION_FACTORY: sessionmaker | None = None
|
||||
|
||||
|
||||
def database_url() -> str:
|
||||
return os.getenv("DATABASE_URL", "postgresql+psycopg://agent:agent@db:5432/agent")
|
||||
|
||||
|
||||
def get_engine() -> Engine:
|
||||
global _ENGINE
|
||||
if _ENGINE is None:
|
||||
_ENGINE = create_engine(database_url(), poolclass=NullPool, future=True)
|
||||
return _ENGINE
|
||||
|
||||
|
||||
def get_session_factory() -> sessionmaker:
|
||||
global _SESSION_FACTORY
|
||||
if _SESSION_FACTORY is None:
|
||||
_SESSION_FACTORY = sessionmaker(bind=get_engine(), autoflush=False, autocommit=False)
|
||||
return _SESSION_FACTORY
|
||||
Reference in New Issue
Block a user