aiteamutils 0.2.13__py3-none-any.whl → 0.2.14__py3-none-any.whl

Sign up to get free protection for your applications and to get access to all the features.
aiteamutils/database.py CHANGED
@@ -6,17 +6,52 @@ from sqlalchemy.exc import IntegrityError, SQLAlchemyError
6
6
  from sqlalchemy.pool import QueuePool
7
7
  from contextlib import asynccontextmanager
8
8
  from sqlalchemy import or_
9
- from fastapi import Request
9
+ from fastapi import Request, Depends
10
10
  from ulid import ULID
11
11
  from sqlalchemy.sql import Select
12
12
 
13
13
  from .exceptions import ErrorCode, CustomException
14
14
  from .base_model import Base, BaseColumn
15
15
  from .enums import ActivityType
16
- from .config import settings
17
16
 
18
17
  T = TypeVar("T", bound=BaseColumn)
19
18
 
19
+ # 전역 데이터베이스 서비스 인스턴스
20
+ _database_service: 'DatabaseService' | None = None
21
+
22
+ def get_database_service() -> 'DatabaseService':
23
+ """DatabaseService 인스턴스를 반환하는 함수
24
+
25
+ Returns:
26
+ DatabaseService: DatabaseService 인스턴스
27
+
28
+ Raises:
29
+ RuntimeError: DatabaseService가 초기화되지 않은 경우
30
+ """
31
+ if _database_service is None:
32
+ raise RuntimeError("DatabaseService not initialized. Call init_settings with db_url first.")
33
+ return _database_service
34
+
35
+ async def get_db() -> AsyncGenerator[AsyncSession, None]:
36
+ """데이터베이스 세션을 생성하고 반환하는 비동기 제너레이터."""
37
+ db_service = get_database_service()
38
+ async with db_service.get_session() as session:
39
+ try:
40
+ yield session
41
+ finally:
42
+ await session.close()
43
+
44
+ def get_database_session(db: AsyncSession = Depends(get_db)) -> 'DatabaseService':
45
+ """DatabaseService 의존성
46
+
47
+ Args:
48
+ db (AsyncSession): 데이터베이스 세션
49
+
50
+ Returns:
51
+ DatabaseService: DatabaseService 인스턴스
52
+ """
53
+ return DatabaseService(session=db)
54
+
20
55
  class DatabaseService:
21
56
  def __init__(
22
57
  self,
@@ -1,10 +1,9 @@
1
- from typing import Type, Dict, Tuple, Any, AsyncGenerator, Callable
2
- from sqlalchemy.ext.asyncio import AsyncSession
1
+ from typing import Type, Dict, Tuple, Any, Callable
3
2
  from fastapi import Depends, status
4
3
  from fastapi.security import OAuth2PasswordBearer
5
4
  from jose import JWTError, jwt
6
5
 
7
- from .database import DatabaseService
6
+ from .database import DatabaseService, get_database_service
8
7
  from .exceptions import CustomException, ErrorCode
9
8
 
10
9
  class Settings:
@@ -14,7 +13,6 @@ class Settings:
14
13
  self.JWT_ALGORITHM = jwt_algorithm
15
14
 
16
15
  _settings: Settings | None = None
17
- _database_service: DatabaseService | None = None
18
16
 
19
17
  def init_settings(
20
18
  jwt_secret: str,
@@ -38,10 +36,11 @@ def init_settings(
38
36
  db_pool_timeout (int, optional): 커넥션 풀 타임아웃
39
37
  db_pool_recycle (int, optional): 커넥션 재활용 시간
40
38
  """
41
- global _settings, _database_service
39
+ global _settings
42
40
  _settings = Settings(jwt_secret, jwt_algorithm)
43
41
 
44
42
  if db_url:
43
+ from .database import _database_service
45
44
  _database_service = DatabaseService(
46
45
  db_url=db_url,
47
46
  db_echo=db_echo,
@@ -64,39 +63,6 @@ def get_settings() -> Settings:
64
63
  raise RuntimeError("Settings not initialized. Call init_settings first.")
65
64
  return _settings
66
65
 
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
-
100
66
  class ServiceRegistry:
101
67
  """서비스 레지스트리를 관리하는 클래스"""
102
68
  def __init__(self):
aiteamutils/version.py CHANGED
@@ -1,2 +1,2 @@
1
1
  """버전 정보"""
2
- __version__ = "0.2.13"
2
+ __version__ = "0.2.14"
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: aiteamutils
3
- Version: 0.2.13
3
+ Version: 0.2.14
4
4
  Summary: AI Team Utilities
5
5
  Project-URL: Homepage, https://github.com/yourusername/aiteamutils
6
6
  Project-URL: Issues, https://github.com/yourusername/aiteamutils/issues
@@ -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=AN-84p9ytiBcuC-plliCGl9EyzZKTnEms9t80jOxN7U,30990
8
- aiteamutils/dependencies.py,sha256=M_sgSo1xmfJMMwtDdiTVxxiH2udvBjNODfxN5Uwxotw,6836
7
+ aiteamutils/database.py,sha256=iaqfPTSH3DqauLvwzFQO5XBBsKsL3KrYnu2AuWaTqgQ,32147
8
+ aiteamutils/dependencies.py,sha256=6YAR2QmGOeL5t9EyWLJHypUopX_CsLDDmmX_3YOSGQQ,5704
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=O1RGVlF9YD2O2f-fwKjZnpK60hVIiyyFd2w4lGbsbbE,44
14
- aiteamutils-0.2.13.dist-info/METADATA,sha256=c3ZTR8by8NIFXhNEHVYj8VlVKBG8olU60ZVBqmCC-p4,1718
15
- aiteamutils-0.2.13.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
16
- aiteamutils-0.2.13.dist-info/RECORD,,
13
+ aiteamutils/version.py,sha256=QQqBt1NyP0N-ZlMtrZ_b51lA-ybgTt3nzbsfJxQS0U8,44
14
+ aiteamutils-0.2.14.dist-info/METADATA,sha256=On-N3O2UklbAe7Qr8eGvkw831AyfE7iU11JPdJUa3-A,1718
15
+ aiteamutils-0.2.14.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
16
+ aiteamutils-0.2.14.dist-info/RECORD,,