23 lines
578 B
Python
Raw Normal View History

2024-01-22 23:46:27 +08:00
from config.database import *
from utils.log_util import logger
async def get_db():
2024-01-22 23:46:27 +08:00
"""
每一个请求处理完毕后会关闭当前连接不同的请求使用不同的连接
:return:
"""
async with AsyncSessionLocal() as current_db:
2024-01-22 23:46:27 +08:00
yield current_db
async def init_create_table():
"""
应用启动时初始化数据库连接
:return:
"""
logger.info("初始化数据库连接...")
async with async_engine.begin() as conn:
await conn.run_sync(Base.metadata.create_all)
2024-01-22 23:46:27 +08:00
logger.info("数据库连接成功")