maleo-database 0.0.4__py3-none-any.whl → 0.0.6__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.
- maleo/database/config/__init__.py +188 -15
- maleo/database/config/connection.py +46 -30
- maleo/database/config/pooling.py +171 -83
- maleo/database/dtos.py +6 -0
- maleo/database/managers/__init__.py +427 -0
- maleo/database/managers/client.py +82 -0
- maleo/database/managers/engine.py +64 -0
- maleo/database/managers/session.py +413 -137
- {maleo_database-0.0.4.dist-info → maleo_database-0.0.6.dist-info}/METADATA +23 -8
- maleo_database-0.0.6.dist-info/RECORD +26 -0
- maleo/database/managers/clients/__init__.py +0 -0
- maleo/database/managers/clients/elasticsearch.py +0 -66
- maleo/database/managers/clients/mongodb.py +0 -53
- maleo/database/managers/clients/redis.py +0 -57
- maleo/database/managers/engines/__init__.py +0 -0
- maleo/database/managers/engines/mysql.py +0 -56
- maleo/database/managers/engines/postgresql.py +0 -58
- maleo/database/managers/engines/sqlite.py +0 -55
- maleo/database/managers/engines/sqlserver.py +0 -63
- maleo_database-0.0.4.dist-info/RECORD +0 -32
- {maleo_database-0.0.4.dist-info → maleo_database-0.0.6.dist-info}/WHEEL +0 -0
- {maleo_database-0.0.4.dist-info → maleo_database-0.0.6.dist-info}/licenses/LICENSE +0 -0
- {maleo_database-0.0.4.dist-info → maleo_database-0.0.6.dist-info}/top_level.txt +0 -0
@@ -10,35 +10,70 @@ from sqlalchemy.engine import Engine
|
|
10
10
|
from sqlalchemy.exc import SQLAlchemyError
|
11
11
|
from sqlalchemy.ext.asyncio import AsyncEngine, async_sessionmaker, AsyncSession
|
12
12
|
from sqlalchemy.orm import sessionmaker, Session
|
13
|
-
from typing import
|
14
|
-
|
13
|
+
from typing import (
|
14
|
+
AsyncGenerator,
|
15
|
+
Generator,
|
16
|
+
Generic,
|
17
|
+
Literal,
|
18
|
+
Optional,
|
19
|
+
Tuple,
|
20
|
+
Union,
|
21
|
+
overload,
|
22
|
+
)
|
23
|
+
from uuid import uuid4
|
24
|
+
from maleo.dtos.authentication import GenericAuthentication
|
25
|
+
from maleo.dtos.contexts.operation import generate_operation_context
|
26
|
+
from maleo.dtos.contexts.request import RequestContext
|
27
|
+
from maleo.dtos.contexts.service import ServiceContext
|
28
|
+
from maleo.enums.operation import (
|
29
|
+
OperationType,
|
30
|
+
SystemOperationType,
|
31
|
+
Origin,
|
32
|
+
Layer,
|
33
|
+
Target,
|
34
|
+
)
|
35
|
+
from maleo.exceptions import (
|
36
|
+
MaleoException,
|
37
|
+
UnprocessableEntity,
|
38
|
+
DatabaseError,
|
39
|
+
InternalServerError,
|
40
|
+
)
|
15
41
|
from maleo.mixins.timestamp import OperationTimestamp
|
42
|
+
from maleo.schemas.operation.system import (
|
43
|
+
SystemOperationAction,
|
44
|
+
SuccessfulSystemOperation,
|
45
|
+
)
|
46
|
+
from maleo.schemas.response import NoDataResponse
|
16
47
|
from maleo.types.base.uuid import OptionalUUID
|
48
|
+
from maleo.logging.enums import Level
|
17
49
|
from maleo.logging.logger import Database
|
18
50
|
from ..enums import Connection
|
19
|
-
from ..config import
|
20
|
-
PostgreSQLDatabaseConfig,
|
21
|
-
MySQLDatabaseConfig,
|
22
|
-
SQLiteDatabaseConfig,
|
23
|
-
SQLServerDatabaseConfig,
|
24
|
-
)
|
51
|
+
from ..config import SQLConfigT
|
25
52
|
|
26
53
|
|
27
|
-
class SessionManager:
|
54
|
+
class SessionManager(Generic[SQLConfigT]):
|
28
55
|
def __init__(
|
29
56
|
self,
|
30
|
-
config:
|
31
|
-
PostgreSQLDatabaseConfig,
|
32
|
-
MySQLDatabaseConfig,
|
33
|
-
SQLiteDatabaseConfig,
|
34
|
-
SQLServerDatabaseConfig,
|
35
|
-
],
|
57
|
+
config: SQLConfigT,
|
36
58
|
engines: Tuple[AsyncEngine, Engine],
|
37
59
|
logger: Database,
|
38
|
-
|
60
|
+
service_context: Optional[ServiceContext] = None,
|
61
|
+
):
|
39
62
|
self._config = config
|
40
63
|
self._async_engine, self._sync_engine = engines
|
41
64
|
self._logger = logger
|
65
|
+
self._service_context = (
|
66
|
+
service_context
|
67
|
+
if service_context is not None
|
68
|
+
else ServiceContext.from_env()
|
69
|
+
)
|
70
|
+
self._operation_context = generate_operation_context(
|
71
|
+
origin=Origin.SERVICE,
|
72
|
+
layer=Layer.UTILITY,
|
73
|
+
target=Target.DATABASE,
|
74
|
+
target_details=self._config.model_dump(),
|
75
|
+
)
|
76
|
+
|
42
77
|
self._async_sessionmaker: async_sessionmaker[AsyncSession] = async_sessionmaker[
|
43
78
|
AsyncSession
|
44
79
|
](bind=self._async_engine, expire_on_commit=True)
|
@@ -47,163 +82,369 @@ class SessionManager:
|
|
47
82
|
)
|
48
83
|
|
49
84
|
async def _async_session_handler(
|
50
|
-
self,
|
85
|
+
self,
|
86
|
+
operation_id: OptionalUUID = None,
|
87
|
+
request_context: Optional[RequestContext] = None,
|
88
|
+
authentication: Optional[GenericAuthentication] = None,
|
51
89
|
) -> AsyncGenerator[AsyncSession, None]:
|
52
|
-
"""
|
53
|
-
|
90
|
+
"""Reusable function for managing async database session."""
|
91
|
+
operation_id = operation_id if operation_id is not None else uuid4()
|
92
|
+
operation_action = SystemOperationAction(
|
93
|
+
type=SystemOperationType.DATABASE_CONNECTION, details=None
|
94
|
+
)
|
54
95
|
session = self._async_sessionmaker()
|
96
|
+
SuccessfulSystemOperation[
|
97
|
+
Optional[GenericAuthentication], NoDataResponse[None]
|
98
|
+
](
|
99
|
+
service_context=self._service_context,
|
100
|
+
id=operation_id,
|
101
|
+
context=self._operation_context,
|
102
|
+
timestamp=OperationTimestamp.now(),
|
103
|
+
summary="Successfully created new async database session",
|
104
|
+
request_context=request_context,
|
105
|
+
authentication=authentication,
|
106
|
+
action=operation_action,
|
107
|
+
response=NoDataResponse[None](metadata=None, other=None),
|
108
|
+
).log(
|
109
|
+
self._logger, level=Level.DEBUG
|
110
|
+
)
|
111
|
+
|
112
|
+
executed_at = datetime.now(tz=timezone.utc)
|
55
113
|
try:
|
56
|
-
yield session
|
57
|
-
await session.commit()
|
114
|
+
yield session # Provide session
|
115
|
+
await session.commit() # Auto-commit on success
|
58
116
|
completed_at = datetime.now(tz=timezone.utc)
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
"config": self._config.model_dump(mode="json"),
|
64
|
-
"operation_id": operation_id,
|
65
|
-
"success": True,
|
66
|
-
"timestamp": OperationTimestamp(
|
67
|
-
executed_at=executed_at,
|
68
|
-
completed_at=completed_at,
|
69
|
-
duration=(completed_at - executed_at).total_seconds(),
|
70
|
-
).model_dump(mode="json"),
|
71
|
-
},
|
72
|
-
},
|
117
|
+
operation_timestamp = OperationTimestamp(
|
118
|
+
executed_at=executed_at,
|
119
|
+
completed_at=completed_at,
|
120
|
+
duration=(completed_at - executed_at).total_seconds(),
|
73
121
|
)
|
74
|
-
|
75
|
-
|
122
|
+
SuccessfulSystemOperation[
|
123
|
+
Optional[GenericAuthentication], NoDataResponse[None]
|
124
|
+
](
|
125
|
+
service_context=self._service_context,
|
126
|
+
id=operation_id,
|
127
|
+
context=self._operation_context,
|
128
|
+
timestamp=operation_timestamp,
|
129
|
+
summary="Successfully committed async database transaction",
|
130
|
+
request_context=request_context,
|
131
|
+
authentication=authentication,
|
132
|
+
action=operation_action,
|
133
|
+
response=NoDataResponse[None](metadata=None, other=None),
|
134
|
+
).log(
|
135
|
+
self._logger, level=Level.INFO
|
136
|
+
)
|
137
|
+
except SQLAlchemyError as se:
|
138
|
+
await session.rollback() # Rollback on error
|
76
139
|
completed_at = datetime.now(tz=timezone.utc)
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
140
|
+
operation_timestamp = OperationTimestamp(
|
141
|
+
executed_at=executed_at,
|
142
|
+
completed_at=completed_at,
|
143
|
+
duration=(completed_at - executed_at).total_seconds(),
|
144
|
+
)
|
145
|
+
error = DatabaseError[Optional[GenericAuthentication]](
|
146
|
+
OperationType.SYSTEM,
|
147
|
+
service_context=self._service_context,
|
148
|
+
operation_id=operation_id,
|
149
|
+
operation_context=self._operation_context,
|
150
|
+
operation_timestamp=operation_timestamp,
|
151
|
+
operation_summary="SQLAlchemy error occured while handling async database session",
|
152
|
+
request_context=request_context,
|
153
|
+
authentication=authentication,
|
154
|
+
operation_action=operation_action,
|
155
|
+
details={
|
156
|
+
"exc_type": type(se).__name__,
|
157
|
+
"exc_data": {
|
158
|
+
"message": str(se),
|
159
|
+
"args": se.args,
|
90
160
|
},
|
91
161
|
},
|
92
162
|
)
|
163
|
+
operation = error.generate_operation(OperationType.SYSTEM)
|
164
|
+
operation.log(self._logger, level=Level.ERROR)
|
165
|
+
raise error from se
|
166
|
+
except ValidationError as ve:
|
167
|
+
await session.rollback() # Rollback on error
|
168
|
+
completed_at = datetime.now(tz=timezone.utc)
|
169
|
+
operation_timestamp = OperationTimestamp(
|
170
|
+
executed_at=executed_at,
|
171
|
+
completed_at=completed_at,
|
172
|
+
duration=(completed_at - executed_at).total_seconds(),
|
173
|
+
)
|
174
|
+
error = UnprocessableEntity[Optional[GenericAuthentication]](
|
175
|
+
OperationType.SYSTEM,
|
176
|
+
service_context=self._service_context,
|
177
|
+
operation_id=operation_id,
|
178
|
+
operation_context=self._operation_context,
|
179
|
+
operation_timestamp=operation_timestamp,
|
180
|
+
operation_summary="Validation error occured while handling async database session",
|
181
|
+
request_context=request_context,
|
182
|
+
authentication=authentication,
|
183
|
+
operation_action=operation_action,
|
184
|
+
details=ve.errors(),
|
185
|
+
)
|
186
|
+
operation = error.generate_operation(OperationType.SYSTEM)
|
187
|
+
operation.log(self._logger, level=Level.ERROR)
|
188
|
+
raise error from ve
|
189
|
+
except MaleoException:
|
93
190
|
raise
|
94
|
-
|
95
|
-
await session.
|
191
|
+
except Exception as e:
|
192
|
+
await session.rollback() # Rollback on error
|
96
193
|
completed_at = datetime.now(tz=timezone.utc)
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
194
|
+
operation_timestamp = OperationTimestamp(
|
195
|
+
executed_at=executed_at,
|
196
|
+
completed_at=completed_at,
|
197
|
+
duration=(completed_at - executed_at).total_seconds(),
|
198
|
+
)
|
199
|
+
error = InternalServerError[Optional[GenericAuthentication]](
|
200
|
+
OperationType.SYSTEM,
|
201
|
+
service_context=self._service_context,
|
202
|
+
operation_id=operation_id,
|
203
|
+
operation_context=self._operation_context,
|
204
|
+
operation_timestamp=operation_timestamp,
|
205
|
+
operation_summary="Unexpected error occured while handling async database session",
|
206
|
+
request_context=request_context,
|
207
|
+
authentication=authentication,
|
208
|
+
operation_action=operation_action,
|
209
|
+
details={
|
210
|
+
"exc_type": type(e).__name__,
|
211
|
+
"exc_data": {
|
212
|
+
"message": str(e),
|
213
|
+
"args": e.args,
|
109
214
|
},
|
110
215
|
},
|
111
216
|
)
|
217
|
+
operation = error.generate_operation(OperationType.SYSTEM)
|
218
|
+
operation.log(self._logger, level=Level.ERROR)
|
219
|
+
raise error from e
|
220
|
+
finally:
|
221
|
+
await session.close() # Ensure session closes
|
222
|
+
completed_at = datetime.now(tz=timezone.utc)
|
223
|
+
SuccessfulSystemOperation[
|
224
|
+
Optional[GenericAuthentication], NoDataResponse[None]
|
225
|
+
](
|
226
|
+
service_context=self._service_context,
|
227
|
+
id=operation_id,
|
228
|
+
context=self._operation_context,
|
229
|
+
timestamp=OperationTimestamp.now(),
|
230
|
+
summary="Successfully closed async database session",
|
231
|
+
request_context=request_context,
|
232
|
+
authentication=authentication,
|
233
|
+
action=operation_action,
|
234
|
+
response=NoDataResponse[None](metadata=None, other=None),
|
235
|
+
).log(
|
236
|
+
self._logger, level=Level.INFO
|
237
|
+
)
|
112
238
|
|
113
239
|
def _sync_session_handler(
|
114
|
-
self,
|
240
|
+
self,
|
241
|
+
operation_id: OptionalUUID = None,
|
242
|
+
request_context: Optional[RequestContext] = None,
|
243
|
+
authentication: Optional[GenericAuthentication] = None,
|
115
244
|
) -> Generator[Session, None, None]:
|
116
|
-
"""
|
117
|
-
|
245
|
+
"""Reusable function for managing sync database session."""
|
246
|
+
operation_id = operation_id if operation_id is not None else uuid4()
|
247
|
+
operation_action = SystemOperationAction(
|
248
|
+
type=SystemOperationType.DATABASE_CONNECTION, details=None
|
249
|
+
)
|
118
250
|
session = self._sync_sessionmaker()
|
251
|
+
SuccessfulSystemOperation[
|
252
|
+
Optional[GenericAuthentication], NoDataResponse[None]
|
253
|
+
](
|
254
|
+
service_context=self._service_context,
|
255
|
+
id=operation_id,
|
256
|
+
context=self._operation_context,
|
257
|
+
timestamp=OperationTimestamp.now(),
|
258
|
+
summary="Successfully created new sync database session",
|
259
|
+
request_context=request_context,
|
260
|
+
authentication=authentication,
|
261
|
+
action=operation_action,
|
262
|
+
response=NoDataResponse[None](metadata=None, other=None),
|
263
|
+
).log(
|
264
|
+
self._logger, level=Level.DEBUG
|
265
|
+
)
|
266
|
+
|
267
|
+
executed_at = datetime.now(tz=timezone.utc)
|
119
268
|
try:
|
120
|
-
yield session
|
121
|
-
session.commit()
|
269
|
+
yield session # Provide session
|
270
|
+
session.commit() # Auto-commit on success
|
122
271
|
completed_at = datetime.now(tz=timezone.utc)
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
"config": self._config.model_dump(mode="json"),
|
128
|
-
"operation_id": operation_id,
|
129
|
-
"success": True,
|
130
|
-
"timestamp": OperationTimestamp(
|
131
|
-
executed_at=executed_at,
|
132
|
-
completed_at=completed_at,
|
133
|
-
duration=(completed_at - executed_at).total_seconds(),
|
134
|
-
).model_dump(mode="json"),
|
135
|
-
},
|
136
|
-
},
|
272
|
+
operation_timestamp = OperationTimestamp(
|
273
|
+
executed_at=executed_at,
|
274
|
+
completed_at=completed_at,
|
275
|
+
duration=(completed_at - executed_at).total_seconds(),
|
137
276
|
)
|
138
|
-
|
139
|
-
|
277
|
+
SuccessfulSystemOperation[
|
278
|
+
Optional[GenericAuthentication], NoDataResponse[None]
|
279
|
+
](
|
280
|
+
service_context=self._service_context,
|
281
|
+
id=operation_id,
|
282
|
+
context=self._operation_context,
|
283
|
+
timestamp=operation_timestamp,
|
284
|
+
summary="Successfully committed sync database transaction",
|
285
|
+
request_context=request_context,
|
286
|
+
authentication=authentication,
|
287
|
+
action=operation_action,
|
288
|
+
response=NoDataResponse[None](metadata=None, other=None),
|
289
|
+
).log(
|
290
|
+
self._logger, level=Level.INFO
|
291
|
+
)
|
292
|
+
except SQLAlchemyError as se:
|
293
|
+
session.rollback() # Rollback on error
|
140
294
|
completed_at = datetime.now(tz=timezone.utc)
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
295
|
+
operation_timestamp = OperationTimestamp(
|
296
|
+
executed_at=executed_at,
|
297
|
+
completed_at=completed_at,
|
298
|
+
duration=(completed_at - executed_at).total_seconds(),
|
299
|
+
)
|
300
|
+
error = DatabaseError[Optional[GenericAuthentication]](
|
301
|
+
OperationType.SYSTEM,
|
302
|
+
service_context=self._service_context,
|
303
|
+
operation_id=operation_id,
|
304
|
+
operation_context=self._operation_context,
|
305
|
+
operation_timestamp=operation_timestamp,
|
306
|
+
operation_summary="SQLAlchemy error occured while handling sync database session",
|
307
|
+
request_context=request_context,
|
308
|
+
authentication=authentication,
|
309
|
+
operation_action=operation_action,
|
310
|
+
details={
|
311
|
+
"exc_type": type(se).__name__,
|
312
|
+
"exc_data": {
|
313
|
+
"message": str(se),
|
314
|
+
"args": se.args,
|
154
315
|
},
|
155
316
|
},
|
156
317
|
)
|
318
|
+
operation = error.generate_operation(OperationType.SYSTEM)
|
319
|
+
operation.log(self._logger, level=Level.ERROR)
|
320
|
+
raise error from se
|
321
|
+
except ValidationError as ve:
|
322
|
+
session.rollback() # Rollback on error
|
323
|
+
completed_at = datetime.now(tz=timezone.utc)
|
324
|
+
operation_timestamp = OperationTimestamp(
|
325
|
+
executed_at=executed_at,
|
326
|
+
completed_at=completed_at,
|
327
|
+
duration=(completed_at - executed_at).total_seconds(),
|
328
|
+
)
|
329
|
+
error = UnprocessableEntity[Optional[GenericAuthentication]](
|
330
|
+
OperationType.SYSTEM,
|
331
|
+
service_context=self._service_context,
|
332
|
+
operation_id=operation_id,
|
333
|
+
operation_context=self._operation_context,
|
334
|
+
operation_timestamp=operation_timestamp,
|
335
|
+
operation_summary="Validation error occured while handling sync database session",
|
336
|
+
request_context=request_context,
|
337
|
+
authentication=authentication,
|
338
|
+
operation_action=operation_action,
|
339
|
+
details=ve.errors(),
|
340
|
+
)
|
341
|
+
operation = error.generate_operation(OperationType.SYSTEM)
|
342
|
+
operation.log(self._logger, level=Level.ERROR)
|
343
|
+
raise error from ve
|
344
|
+
except MaleoException:
|
157
345
|
raise
|
158
|
-
|
159
|
-
session.
|
346
|
+
except Exception as e:
|
347
|
+
session.rollback() # Rollback on error
|
160
348
|
completed_at = datetime.now(tz=timezone.utc)
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
349
|
+
operation_timestamp = OperationTimestamp(
|
350
|
+
executed_at=executed_at,
|
351
|
+
completed_at=completed_at,
|
352
|
+
duration=(completed_at - executed_at).total_seconds(),
|
353
|
+
)
|
354
|
+
error = InternalServerError[Optional[GenericAuthentication]](
|
355
|
+
OperationType.SYSTEM,
|
356
|
+
service_context=self._service_context,
|
357
|
+
operation_id=operation_id,
|
358
|
+
operation_context=self._operation_context,
|
359
|
+
operation_timestamp=operation_timestamp,
|
360
|
+
operation_summary="Unexpected error occured while handling sync database session",
|
361
|
+
request_context=request_context,
|
362
|
+
authentication=authentication,
|
363
|
+
operation_action=operation_action,
|
364
|
+
details={
|
365
|
+
"exc_type": type(e).__name__,
|
366
|
+
"exc_data": {
|
367
|
+
"message": str(e),
|
368
|
+
"args": e.args,
|
173
369
|
},
|
174
370
|
},
|
175
371
|
)
|
372
|
+
operation = error.generate_operation(OperationType.SYSTEM)
|
373
|
+
operation.log(self._logger, level=Level.ERROR)
|
374
|
+
raise error from e
|
375
|
+
finally:
|
376
|
+
session.close() # Ensure session closes
|
377
|
+
completed_at = datetime.now(tz=timezone.utc)
|
378
|
+
SuccessfulSystemOperation[
|
379
|
+
Optional[GenericAuthentication], NoDataResponse[None]
|
380
|
+
](
|
381
|
+
service_context=self._service_context,
|
382
|
+
id=operation_id,
|
383
|
+
context=self._operation_context,
|
384
|
+
timestamp=OperationTimestamp.now(),
|
385
|
+
summary="Successfully closed sync database session",
|
386
|
+
request_context=request_context,
|
387
|
+
authentication=authentication,
|
388
|
+
action=operation_action,
|
389
|
+
response=NoDataResponse[None](metadata=None, other=None),
|
390
|
+
).log(
|
391
|
+
self._logger, level=Level.INFO
|
392
|
+
)
|
176
393
|
|
177
394
|
@asynccontextmanager
|
178
395
|
async def _async_context_manager(
|
179
|
-
self,
|
396
|
+
self,
|
397
|
+
operation_id: OptionalUUID = None,
|
398
|
+
request_context: Optional[RequestContext] = None,
|
399
|
+
authentication: Optional[GenericAuthentication] = None,
|
180
400
|
) -> AsyncGenerator[AsyncSession, None]:
|
181
401
|
"""Async context manager implementation."""
|
182
|
-
async for session in self._async_session_handler(
|
402
|
+
async for session in self._async_session_handler(
|
403
|
+
operation_id,
|
404
|
+
request_context,
|
405
|
+
authentication,
|
406
|
+
):
|
183
407
|
yield session
|
184
408
|
|
185
409
|
@contextmanager
|
186
410
|
def _sync_context_manager(
|
187
|
-
self,
|
411
|
+
self,
|
412
|
+
operation_id: OptionalUUID = None,
|
413
|
+
request_context: Optional[RequestContext] = None,
|
414
|
+
authentication: Optional[GenericAuthentication] = None,
|
188
415
|
) -> Generator[Session, None, None]:
|
189
416
|
"""Sync context manager implementation."""
|
190
|
-
yield from self._sync_session_handler(
|
417
|
+
yield from self._sync_session_handler(
|
418
|
+
operation_id,
|
419
|
+
request_context,
|
420
|
+
authentication,
|
421
|
+
)
|
191
422
|
|
192
423
|
# Overloaded context manager methods
|
193
424
|
@overload
|
194
425
|
def get(
|
195
|
-
self,
|
426
|
+
self,
|
427
|
+
connection: Literal[Connection.ASYNC],
|
428
|
+
operation_id: OptionalUUID = None,
|
429
|
+
request_context: Optional[RequestContext] = None,
|
430
|
+
authentication: Optional[GenericAuthentication] = None,
|
196
431
|
) -> AbstractAsyncContextManager[AsyncSession]: ...
|
197
432
|
|
198
433
|
@overload
|
199
434
|
def get(
|
200
|
-
self,
|
435
|
+
self,
|
436
|
+
connection: Literal[Connection.SYNC],
|
437
|
+
operation_id: OptionalUUID = None,
|
438
|
+
request_context: Optional[RequestContext] = None,
|
439
|
+
authentication: Optional[GenericAuthentication] = None,
|
201
440
|
) -> AbstractContextManager[Session]: ...
|
202
441
|
|
203
442
|
def get(
|
204
443
|
self,
|
205
444
|
connection: Connection = Connection.ASYNC,
|
206
445
|
operation_id: OptionalUUID = None,
|
446
|
+
request_context: Optional[RequestContext] = None,
|
447
|
+
authentication: Optional[GenericAuthentication] = None,
|
207
448
|
) -> Union[
|
208
449
|
AbstractAsyncContextManager[AsyncSession], AbstractContextManager[Session]
|
209
450
|
]:
|
@@ -211,46 +452,81 @@ class SessionManager:
|
|
211
452
|
if operation_id is None:
|
212
453
|
operation_id = uuid4()
|
213
454
|
if connection is Connection.ASYNC:
|
214
|
-
return self._async_context_manager(
|
455
|
+
return self._async_context_manager(
|
456
|
+
operation_id,
|
457
|
+
request_context,
|
458
|
+
authentication,
|
459
|
+
)
|
215
460
|
else:
|
216
|
-
return self._sync_context_manager(
|
461
|
+
return self._sync_context_manager(
|
462
|
+
operation_id,
|
463
|
+
request_context,
|
464
|
+
authentication,
|
465
|
+
)
|
217
466
|
|
218
467
|
# Alternative: More explicit methods
|
219
468
|
@asynccontextmanager
|
220
469
|
async def get_async(
|
221
|
-
self,
|
470
|
+
self,
|
471
|
+
operation_id: OptionalUUID = None,
|
472
|
+
request_context: Optional[RequestContext] = None,
|
473
|
+
authentication: Optional[GenericAuthentication] = None,
|
222
474
|
) -> AsyncGenerator[AsyncSession, None]:
|
223
475
|
"""Explicit async context manager."""
|
224
|
-
|
225
|
-
operation_id
|
226
|
-
|
476
|
+
async for session in self._async_session_handler(
|
477
|
+
operation_id,
|
478
|
+
request_context,
|
479
|
+
authentication,
|
480
|
+
):
|
227
481
|
yield session
|
228
482
|
|
229
483
|
@contextmanager
|
230
484
|
def get_sync(
|
231
|
-
self,
|
485
|
+
self,
|
486
|
+
operation_id: OptionalUUID = None,
|
487
|
+
request_context: Optional[RequestContext] = None,
|
488
|
+
authentication: Optional[GenericAuthentication] = None,
|
232
489
|
) -> Generator[Session, None, None]:
|
233
490
|
"""Explicit sync context manager."""
|
234
|
-
|
235
|
-
operation_id
|
236
|
-
|
491
|
+
yield from self._sync_session_handler(
|
492
|
+
operation_id,
|
493
|
+
request_context,
|
494
|
+
authentication,
|
495
|
+
)
|
237
496
|
|
238
|
-
def as_async_dependency(
|
497
|
+
def as_async_dependency(
|
498
|
+
self,
|
499
|
+
operation_id: OptionalUUID = None,
|
500
|
+
request_context: Optional[RequestContext] = None,
|
501
|
+
authentication: Optional[GenericAuthentication] = None,
|
502
|
+
):
|
239
503
|
"""Explicit async dependency injection."""
|
240
|
-
if operation_id is None:
|
241
|
-
operation_id = uuid4()
|
242
504
|
|
243
505
|
def dependency() -> AsyncGenerator[AsyncSession, None]:
|
244
|
-
return self._async_session_handler(
|
506
|
+
return self._async_session_handler(
|
507
|
+
operation_id,
|
508
|
+
request_context,
|
509
|
+
authentication,
|
510
|
+
)
|
245
511
|
|
246
512
|
return dependency
|
247
513
|
|
248
|
-
def as_sync_dependency(
|
514
|
+
def as_sync_dependency(
|
515
|
+
self,
|
516
|
+
operation_id: OptionalUUID = None,
|
517
|
+
request_context: Optional[RequestContext] = None,
|
518
|
+
authentication: Optional[GenericAuthentication] = None,
|
519
|
+
):
|
249
520
|
"""Explicit sync dependency injection."""
|
250
|
-
if operation_id is None:
|
251
|
-
operation_id = uuid4()
|
252
521
|
|
253
|
-
def dependency() ->
|
254
|
-
return self.
|
522
|
+
def dependency() -> Generator[Session, None, None]:
|
523
|
+
return self._sync_session_handler(
|
524
|
+
operation_id,
|
525
|
+
request_context,
|
526
|
+
authentication,
|
527
|
+
)
|
255
528
|
|
256
529
|
return dependency
|
530
|
+
|
531
|
+
def dispose(self):
|
532
|
+
self._sync_sessionmaker.close_all()
|