mirror of
https://github.com/fastapi-practices/fastapi_best_architecture.git
synced 2026-03-13 09:31:31 +08:00
* define the basic architecture * Update script and deployment file locations * Update the route registration * Fix CI download dependencies * Updated ruff to 0.3.3 * Update app subdirectory naming * Update the model import * fix pre-commit pdm lock * Update the service directory naming * Add CRUD method documents * Fix the issue of circular import * Update the README document * Update the SQL statement for create tables * Update docker scripts and documentation * Fix docker scripts * Update the backend README.md * Add the security folder and move the redis client * Update the configuration item * Fix environment configuration reads * Update the default configuration * Updated README description * Updated the user registration API * Fix test cases * Update the celery configuration * Update and fix celery configuration * Updated the celery structure * Update celery tasks and api * Add celery flower * Update the import style * Update contributors
63 lines
1.7 KiB
Python
63 lines
1.7 KiB
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
import sys
|
|
|
|
from typing import Annotated
|
|
from uuid import uuid4
|
|
|
|
from fastapi import Depends
|
|
from sqlalchemy import URL
|
|
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine
|
|
|
|
from backend.common.log import log
|
|
from backend.common.msd.model import MappedBase
|
|
from backend.core.conf import settings
|
|
|
|
|
|
def create_engine_and_session(url: str | URL):
|
|
try:
|
|
# 数据库引擎
|
|
engine = create_async_engine(url, echo=settings.MYSQL_ECHO, future=True, pool_pre_ping=True)
|
|
# log.success('数据库连接成功')
|
|
except Exception as e:
|
|
log.error('❌ 数据库链接失败 {}', e)
|
|
sys.exit()
|
|
else:
|
|
db_session = async_sessionmaker(bind=engine, autoflush=False, expire_on_commit=False)
|
|
return engine, db_session
|
|
|
|
|
|
SQLALCHEMY_DATABASE_URL = (
|
|
f'mysql+asyncmy://{settings.MYSQL_USER}:{settings.MYSQL_PASSWORD}@{settings.MYSQL_HOST}:'
|
|
f'{settings.MYSQL_PORT}/{settings.MYSQL_DATABASE}?charset={settings.MYSQL_CHARSET}'
|
|
)
|
|
|
|
async_engine, async_db_session = create_engine_and_session(SQLALCHEMY_DATABASE_URL)
|
|
|
|
|
|
async def get_db() -> AsyncSession:
|
|
"""session 生成器"""
|
|
session = async_db_session()
|
|
try:
|
|
yield session
|
|
except Exception as se:
|
|
await session.rollback()
|
|
raise se
|
|
finally:
|
|
await session.close()
|
|
|
|
|
|
# Session Annotated
|
|
CurrentSession = Annotated[AsyncSession, Depends(get_db)]
|
|
|
|
|
|
async def create_table():
|
|
"""创建数据库表"""
|
|
async with async_engine.begin() as coon:
|
|
await coon.run_sync(MappedBase.metadata.create_all)
|
|
|
|
|
|
def uuid4_str() -> str:
|
|
"""数据库引擎 UUID 类型兼容性解决方案"""
|
|
return str(uuid4())
|