File size: 1,207 Bytes
a3d5b1d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker, declarative_base
from app.config import DB_HOST, DB_PORT, DB_NAME, DB_USER, DB_PASSWORD


# ===============================
# DATABASE URL
# ===============================
DB_PORT = DB_PORT or 3306
DATABASE_URL = (
    f"mysql+pymysql://{DB_USER}:{DB_PASSWORD}@{DB_HOST}:{DB_PORT}/{DB_NAME}"
)

# ===============================
# ENGINE CONFIGURATION
# ===============================
engine = create_engine(
    DATABASE_URL,
    pool_pre_ping=True,     # auto reconnect if connection drops
    pool_size=5,            # number of persistent connections
    max_overflow=10,        # extra connections if needed
    echo=False              # NEVER True in production
)


# ===============================
# SESSION
# ===============================
SessionLocal = sessionmaker(
    autocommit=False,
    autoflush=False,
    bind=engine
)


# ===============================
# BASE MODEL
# ===============================
Base = declarative_base()


# ===============================
# DEPENDENCY
# ===============================
def get_db():
    db = SessionLocal()
    try:
        yield db
    finally:
        db.close()