aiteamutils 0.2.8__py3-none-any.whl → 0.2.9__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- aiteamutils/database.py +47 -22
- aiteamutils/dependencies.py +62 -14
- aiteamutils/version.py +1 -1
- {aiteamutils-0.2.8.dist-info → aiteamutils-0.2.9.dist-info}/METADATA +1 -1
- {aiteamutils-0.2.8.dist-info → aiteamutils-0.2.9.dist-info}/RECORD +6 -6
- {aiteamutils-0.2.8.dist-info → aiteamutils-0.2.9.dist-info}/WHEEL +0 -0
aiteamutils/database.py
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
from typing import Any, Dict, Optional, Type, AsyncGenerator, TypeVar, List, Union
|
2
2
|
from sqlalchemy import select, update, and_, Table
|
3
3
|
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine, AsyncEngine
|
4
|
-
from sqlalchemy.orm import sessionmaker
|
4
|
+
from sqlalchemy.orm import sessionmaker
|
5
5
|
from sqlalchemy.exc import IntegrityError, SQLAlchemyError
|
6
6
|
from sqlalchemy.pool import QueuePool
|
7
7
|
from contextlib import asynccontextmanager
|
@@ -18,32 +18,57 @@ from .config import settings
|
|
18
18
|
T = TypeVar("T", bound=BaseColumn)
|
19
19
|
|
20
20
|
class DatabaseService:
|
21
|
-
def __init__(
|
21
|
+
def __init__(
|
22
|
+
self,
|
23
|
+
db_url: str = None,
|
24
|
+
session: AsyncSession = None,
|
25
|
+
db_echo: bool = False,
|
26
|
+
db_pool_size: int = 5,
|
27
|
+
db_max_overflow: int = 10,
|
28
|
+
db_pool_timeout: int = 30,
|
29
|
+
db_pool_recycle: int = 1800
|
30
|
+
):
|
22
31
|
"""DatabaseService 초기화.
|
23
32
|
|
24
33
|
Args:
|
25
|
-
db_url: 데이터베이스 URL
|
34
|
+
db_url (str, optional): 데이터베이스 URL
|
35
|
+
session (AsyncSession, optional): 기존 세션
|
36
|
+
db_echo (bool, optional): SQL 로깅 여부
|
37
|
+
db_pool_size (int, optional): DB 커넥션 풀 크기
|
38
|
+
db_max_overflow (int, optional): 최대 초과 커넥션 수
|
39
|
+
db_pool_timeout (int, optional): 커넥션 풀 타임아웃
|
40
|
+
db_pool_recycle (int, optional): 커넥션 재활용 시간
|
26
41
|
"""
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
42
|
+
if db_url:
|
43
|
+
self.engine = create_async_engine(
|
44
|
+
db_url,
|
45
|
+
echo=db_echo,
|
46
|
+
pool_size=db_pool_size,
|
47
|
+
max_overflow=db_max_overflow,
|
48
|
+
pool_timeout=db_pool_timeout,
|
49
|
+
pool_recycle=db_pool_recycle,
|
50
|
+
pool_pre_ping=True,
|
51
|
+
poolclass=QueuePool,
|
52
|
+
)
|
53
|
+
self.async_session = sessionmaker(
|
54
|
+
bind=self.engine,
|
55
|
+
class_=AsyncSession,
|
56
|
+
expire_on_commit=False
|
57
|
+
)
|
58
|
+
self.db = None
|
59
|
+
elif session:
|
60
|
+
self.engine = session.bind
|
61
|
+
self.async_session = None
|
62
|
+
self.db = session
|
63
|
+
else:
|
64
|
+
raise ValueError("Either db_url or session must be provided")
|
65
|
+
|
44
66
|
@asynccontextmanager
|
45
|
-
async def
|
46
|
-
"""데이터베이스 세션을 생성하고 반환하는 비동기
|
67
|
+
async def get_session(self) -> AsyncGenerator[AsyncSession, None]:
|
68
|
+
"""데이터베이스 세션을 생성하고 반환하는 비동기 컨텍스트 매니저."""
|
69
|
+
if self.async_session is None:
|
70
|
+
raise RuntimeError("Session factory not initialized")
|
71
|
+
|
47
72
|
async with self.async_session() as session:
|
48
73
|
try:
|
49
74
|
yield session
|
aiteamutils/dependencies.py
CHANGED
@@ -14,16 +14,42 @@ class Settings:
|
|
14
14
|
self.JWT_ALGORITHM = jwt_algorithm
|
15
15
|
|
16
16
|
_settings: Settings | None = None
|
17
|
-
|
18
|
-
|
17
|
+
_database_service: DatabaseService | None = None
|
18
|
+
|
19
|
+
def init_settings(
|
20
|
+
jwt_secret: str,
|
21
|
+
jwt_algorithm: str = "HS256",
|
22
|
+
db_url: str = None,
|
23
|
+
db_echo: bool = False,
|
24
|
+
db_pool_size: int = 5,
|
25
|
+
db_max_overflow: int = 10,
|
26
|
+
db_pool_timeout: int = 30,
|
27
|
+
db_pool_recycle: int = 1800
|
28
|
+
):
|
19
29
|
"""설정 초기화 함수
|
20
30
|
|
21
31
|
Args:
|
22
32
|
jwt_secret (str): JWT 시크릿 키
|
23
33
|
jwt_algorithm (str, optional): JWT 알고리즘. Defaults to "HS256".
|
34
|
+
db_url (str, optional): 데이터베이스 URL
|
35
|
+
db_echo (bool, optional): SQL 로깅 여부
|
36
|
+
db_pool_size (int, optional): DB 커넥션 풀 크기
|
37
|
+
db_max_overflow (int, optional): 최대 초과 커넥션 수
|
38
|
+
db_pool_timeout (int, optional): 커넥션 풀 타임아웃
|
39
|
+
db_pool_recycle (int, optional): 커넥션 재활용 시간
|
24
40
|
"""
|
25
|
-
global _settings
|
41
|
+
global _settings, _database_service
|
26
42
|
_settings = Settings(jwt_secret, jwt_algorithm)
|
43
|
+
|
44
|
+
if db_url:
|
45
|
+
_database_service = DatabaseService(
|
46
|
+
db_url=db_url,
|
47
|
+
db_echo=db_echo,
|
48
|
+
db_pool_size=db_pool_size,
|
49
|
+
db_max_overflow=db_max_overflow,
|
50
|
+
db_pool_timeout=db_pool_timeout,
|
51
|
+
db_pool_recycle=db_pool_recycle
|
52
|
+
)
|
27
53
|
|
28
54
|
def get_settings() -> Settings:
|
29
55
|
"""현재 설정을 반환하는 함수
|
@@ -38,6 +64,39 @@ def get_settings() -> Settings:
|
|
38
64
|
raise RuntimeError("Settings not initialized. Call init_settings first.")
|
39
65
|
return _settings
|
40
66
|
|
67
|
+
def get_database_service() -> DatabaseService:
|
68
|
+
"""DatabaseService 인스턴스를 반환하는 함수
|
69
|
+
|
70
|
+
Returns:
|
71
|
+
DatabaseService: DatabaseService 인스턴스
|
72
|
+
|
73
|
+
Raises:
|
74
|
+
RuntimeError: DatabaseService가 초기화되지 않은 경우
|
75
|
+
"""
|
76
|
+
if _database_service is None:
|
77
|
+
raise RuntimeError("DatabaseService not initialized. Call init_settings with db_url first.")
|
78
|
+
return _database_service
|
79
|
+
|
80
|
+
async def get_db() -> AsyncGenerator[AsyncSession, None]:
|
81
|
+
"""데이터베이스 세션을 생성하고 반환하는 비동기 제너레이터."""
|
82
|
+
db_service = get_database_service()
|
83
|
+
async with db_service.get_session() as session:
|
84
|
+
try:
|
85
|
+
yield session
|
86
|
+
finally:
|
87
|
+
await session.close()
|
88
|
+
|
89
|
+
def get_database_session(db: AsyncSession = Depends(get_db)) -> DatabaseService:
|
90
|
+
"""DatabaseService 의존성
|
91
|
+
|
92
|
+
Args:
|
93
|
+
db (AsyncSession): 데이터베이스 세션
|
94
|
+
|
95
|
+
Returns:
|
96
|
+
DatabaseService: DatabaseService 인스턴스
|
97
|
+
"""
|
98
|
+
return DatabaseService(session=db)
|
99
|
+
|
41
100
|
class ServiceRegistry:
|
42
101
|
"""서비스 레지스트리를 관리하는 클래스"""
|
43
102
|
def __init__(self):
|
@@ -81,17 +140,6 @@ class ServiceRegistry:
|
|
81
140
|
# ServiceRegistry 초기화
|
82
141
|
service_registry = ServiceRegistry()
|
83
142
|
|
84
|
-
async def get_database_service(db: AsyncSession) -> DatabaseService:
|
85
|
-
"""DatabaseService 의존성
|
86
|
-
|
87
|
-
Args:
|
88
|
-
db (AsyncSession): 데이터베이스 세션
|
89
|
-
|
90
|
-
Returns:
|
91
|
-
DatabaseService: DatabaseService 인스턴스
|
92
|
-
"""
|
93
|
-
return DatabaseService(db)
|
94
|
-
|
95
143
|
def get_service(name: str):
|
96
144
|
"""등록된 서비스를 가져오는 의존성 함수
|
97
145
|
|
aiteamutils/version.py
CHANGED
@@ -1,2 +1,2 @@
|
|
1
1
|
"""버전 정보"""
|
2
|
-
__version__ = "0.2.
|
2
|
+
__version__ = "0.2.9"
|
@@ -4,13 +4,13 @@ aiteamutils/base_repository.py,sha256=qdwQ7Sj2fUqxpDg6cWM48n_QbwPK_VUlG9zTSem8iC
|
|
4
4
|
aiteamutils/base_service.py,sha256=E4dHGE0DvhmRyFplh46SwKJOSF_nUL7OAsCkf_ZJF_8,24733
|
5
5
|
aiteamutils/cache.py,sha256=tr0Yn8VPYA9QHiKCUzciVlQ2J1RAwNo2K9lGMH4rY3s,1334
|
6
6
|
aiteamutils/config.py,sha256=vC6k6E2-Y4mD0E0kw6WVgSatCl9K_BtTwrVFhLrhCzs,665
|
7
|
-
aiteamutils/database.py,sha256=
|
8
|
-
aiteamutils/dependencies.py,sha256=
|
7
|
+
aiteamutils/database.py,sha256=PRzdG6SiBzxCjsDjGRcfCbUHziSy1ycm9tDYsqkg1Og,30972
|
8
|
+
aiteamutils/dependencies.py,sha256=M_sgSo1xmfJMMwtDdiTVxxiH2udvBjNODfxN5Uwxotw,6836
|
9
9
|
aiteamutils/enums.py,sha256=ipZi6k_QD5-3QV7Yzv7bnL0MjDz-vqfO9I5L77biMKs,632
|
10
10
|
aiteamutils/exceptions.py,sha256=YV-ISya4wQlHk4twvGo16I5r8h22-tXpn9wa-b3WwDM,15231
|
11
11
|
aiteamutils/security.py,sha256=AZszaTxVEGi1jU1sX3QXHGgshp1lVvd0xXvZejXvs_w,12643
|
12
12
|
aiteamutils/validators.py,sha256=3N245cZFjgwtW_KzjESkizx5BBUDaJLbbxfNO4WOFZ0,7764
|
13
|
-
aiteamutils/version.py,sha256
|
14
|
-
aiteamutils-0.2.
|
15
|
-
aiteamutils-0.2.
|
16
|
-
aiteamutils-0.2.
|
13
|
+
aiteamutils/version.py,sha256=-wgeOcidZCJ0zY2W2v_U4jlTLvmpNajEoP8_LpJPn9g,43
|
14
|
+
aiteamutils-0.2.9.dist-info/METADATA,sha256=OsmuCIXUPq700xe2US-ak5LYb9fd6fz7V36ipfMOAtg,1717
|
15
|
+
aiteamutils-0.2.9.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
|
16
|
+
aiteamutils-0.2.9.dist-info/RECORD,,
|
File without changes
|