modelstore/Utils/DataBase/SqlAlchemyUtils.py

26 lines
598 B
Python
Raw Permalink Normal View History

2022-11-15 13:47:52 +08:00
import json
import os
2022-11-14 01:36:08 +08:00
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
2022-11-15 13:47:52 +08:00
# Import DBConfig
with open(os.path.abspath(os.path.dirname(__file__) + '/DBConfig.json')) as f:
db_configs = json.load(f)
this_mysql_cfg = db_configs['Mysql']["wr_model_store"]
2022-11-14 01:36:08 +08:00
# Sqlalchemy Export
2022-11-15 13:47:52 +08:00
engine = create_engine(this_mysql_cfg)
2022-11-14 01:36:08 +08:00
Session = sessionmaker(autocommit=False, autoflush=False, bind=engine)
Base = declarative_base()
def get_db():
try:
db = Session()
yield db
finally:
db.close()