|
10 | 10 | from lfx.services.schema import ServiceType |
11 | 11 |
|
12 | 12 | if TYPE_CHECKING: |
| 13 | + from collections.abc import AsyncGenerator |
| 14 | + from sqlmodel.ext.asyncio.session import AsyncSession |
13 | 15 | from lfx.services.interfaces import ( |
14 | 16 | CacheServiceProtocol, |
15 | 17 | ChatServiceProtocol, |
@@ -52,10 +54,15 @@ def get_service(service_type: ServiceType, default=None): |
52 | 54 | return None |
53 | 55 |
|
54 | 56 |
|
55 | | -def get_db_service() -> DatabaseServiceProtocol | None: |
| 57 | +def get_db_service(*, use_organisation: bool = True) -> DatabaseServiceProtocol | None: |
56 | 58 | """Retrieves the database service instance.""" |
57 | 59 | from lfx.services.schema import ServiceType |
58 | 60 |
|
| 61 | + from langflow.services.database.organisation import OrganizationService |
| 62 | + |
| 63 | + if use_organisation and get_settings_service().auth_settings.CLERK_AUTH_ENABLED: |
| 64 | + return OrganizationService.get_db_service_for_request() |
| 65 | + |
59 | 66 | return get_service(ServiceType.DATABASE_SERVICE) |
60 | 67 |
|
61 | 68 |
|
@@ -102,13 +109,13 @@ def get_tracing_service() -> TracingServiceProtocol | None: |
102 | 109 |
|
103 | 110 |
|
104 | 111 | @asynccontextmanager |
105 | | -async def session_scope(): |
| 112 | +async def session_scope(*, use_organisation: bool = True) -> AsyncGenerator[AsyncSession, None]: |
106 | 113 | """Session scope context manager. |
107 | 114 |
|
108 | 115 | Returns a real session if database service is available, otherwise a NoopSession. |
109 | 116 | This ensures code can always call session methods without None checking. |
110 | 117 | """ |
111 | | - db_service = get_db_service() |
| 118 | + db_service = get_db_service(use_organisation=use_organisation) |
112 | 119 | if db_service is None or inspect.isabstract(type(db_service)): |
113 | 120 | from lfx.services.session import NoopSession |
114 | 121 |
|
|
0 commit comments