aiteamutils 0.2.13__tar.gz → 0.2.14__tar.gz

Sign up to get free protection for your applications and to get access to all the features.
@@ -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
@@ -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):
@@ -0,0 +1,2 @@
1
+ """버전 정보"""
2
+ __version__ = "0.2.14"
@@ -1,2 +0,0 @@
1
- """버전 정보"""
2
- __version__ = "0.2.13"
File without changes
File without changes
File without changes