-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdatabase.py
More file actions
354 lines (303 loc) · 13.1 KB
/
Copy pathdatabase.py
File metadata and controls
354 lines (303 loc) · 13.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
import functools
from contextvars import ContextVar
from typing import (
Any,
AsyncGenerator,
Callable,
Generator,
Mapping,
Optional,
TypeVar,
Union,
)
from sqlalchemy.engine import URL, Connection
from sqlalchemy.ext.asyncio import (
AsyncEngine,
async_scoped_session,
create_async_engine,
)
from sqlalchemy.future import Engine, create_engine
from sqlalchemy.orm import scoped_session, sessionmaker
from typing_extensions import Awaitable, Concatenate, ParamSpec
try:
from sqlmodel import Session
from sqlmodel.ext.asyncio.session import AsyncSession
except ImportError:
from sqlalchemy.orm import Session
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy_database._abc_async_database import AbcAsyncDatabase, to_thread
_P = ParamSpec("_P")
_T = TypeVar("_T")
_R = TypeVar("_R")
class AsyncDatabase(AbcAsyncDatabase):
"""`sqlalchemy` asynchronous database client"""
def __init__(
self,
engine: AsyncEngine,
commit_on_exit: bool = True,
**session_options,
):
"""
Initialize the client through the asynchronous engine
Args:
engine: Asynchronous Engine
commit_on_exit: Whether to commit the session when the context manager or session generator exits.
**session_options: The default `session` initialization parameters
"""
self.engine: AsyncEngine = engine
"""`sqlalchemy` Asynchronous Engine
Example:
```Python
async with self.engine.begin() as conn:
await conn.run_sync(SQLModel.metadata.create_all)
```
"""
self.commit_on_exit: bool = commit_on_exit
"""Whether to commit the session when the context manager or session generator exits."""
session_options.setdefault("class_", AsyncSession)
self.session_maker: Callable[..., AsyncSession] = sessionmaker(self.engine, **session_options)
"""`sqlalchemy` session factory function
Example:
```Python
async with self.session_maker() as sesson:
await session.delete(User,1)
await session.commit()
```
"""
self._session_scope: ContextVar[Union[str, AsyncSession, None]] = ContextVar(
f"_session_context_var_{id(self)}", default=None
)
self.scoped_session: async_scoped_session = async_scoped_session(self.session_maker, scopefunc=self._session_scope.get)
super().__init__(engine)
@property
def session(self) -> AsyncSession:
"""Return an instance of Session local to the current async context.
Note: Must register middleware in fastapi application to get session in request.
Example:
```Python
app = FastAPI()
app.add_middleware(db.asgi_middleware)
@app.get('/get_user')
async def get_user(id:int):
return await db.session.get(User,id)
```
In ordinary methods, session will return None. You can get it through:
```Python
async with db():
await db.session.get(User,id)
```
"""
return self.scoped_session()
@property
def scoped(self) -> bool:
"""Whether the current context has a session. If False, the session is the default global session,
and the transaction needs to be manually submitted.
"""
return bool(self._session_scope.get())
def __call__(self, scope: Any = None):
return AsyncSessionContextVarManager(self, scope=scope)
@classmethod
def create(
cls, url: Union[str, URL], *, commit_on_exit: bool = True, session_options: Mapping[str, Any] = None, **kwargs
) -> "AsyncDatabase":
"""
Initialize the client with a database connection string
Args:
url: Asynchronous database connection string
commit_on_exit: Whether to commit the session when the context manager or session generator exits.
session_options: The default `session` initialization parameters
**kwargs: Asynchronous engine initialization parameters
Returns:
Return the client instance.
"""
kwargs.setdefault("future", True)
engine = create_async_engine(url, **kwargs)
session_options = session_options or {}
return cls(engine, commit_on_exit=commit_on_exit, **session_options)
async def session_generator(self) -> AsyncGenerator[AsyncSession, Any]:
"""AsyncSession Generator, available for FastAPI dependencies.
Example:
```Python
@router.get('/get_user')
async get_user(id:int,session:AsyncSession=Depends(db.session_generator)):
return await session.get(User,id)
```
"""
if self.scoped:
"""If the current context has a session, return it."""
yield self.session
else:
"""If the current context has no session, create a new session."""
async with self.session_maker() as session:
yield session
if self.commit_on_exit:
await session.commit()
async def run_sync(
self,
fn: Callable[[Concatenate[Union[Session, Connection], _P]], _T],
*args: _P.args,
is_session: bool = True,
**kwargs: _P.kwargs,
) -> Union[_T, _R]:
"""
Invoke the given sync callable passing sync self as the first
argument.
This method maintains the asyncio event loop all the way through
to the database connection by running the given callable in a
specially instrumented greenlet.
Args:
fn: Synchronization function
*args: Synchronization function positional argument
is_session: Session or not. If true, an `AsyncSession` is created.
If false, an `AsyncConnection` is created. The default is true.
**kwargs: Synchronization function keyword argument
Returns: Returns the result of the fn synchronization function.
Example:
```Python
def get_user(session:Session,id:int):
return session.get(User,id)
user = await db.run_sync(get_user,5)
```
None:
The provided callable is invoked inline within the asyncio event
loop, and will block on traditional IO calls. IO within this
callable should only call into SQLAlchemy's asyncio database
APIs which will be properly adapted to the greenlet context.
"""
if is_session:
return await self.session.run_sync(fn, *args, **kwargs)
async with self.engine.begin() as conn:
return await conn.run_sync(fn, *args, **kwargs)
class Database(AbcAsyncDatabase):
"""`sqlalchemy` synchronous database client"""
def __init__(self, engine: Engine, commit_on_exit: bool = True, **session_options):
self.engine: Engine = engine
self.commit_on_exit: bool = commit_on_exit
session_options.setdefault("class_", Session)
self.session_maker: Callable[..., Session] = sessionmaker(self.engine, **session_options)
self._session_scope: ContextVar[Union[str, Session, None]] = ContextVar(f"_session_context_var_{id(self)}", default=None)
self.scoped_session: scoped_session = scoped_session(self.session_maker, scopefunc=self._session_scope.get)
"""Returns the Session local instance for the current context or current thread."""
super().__init__(engine)
@property
def session(self) -> Session:
return self.scoped_session()
@property
def scoped(self) -> bool:
return bool(self._session_scope.get())
def __call__(self, scope: Any = None):
return SessionContextVarManager(self, scope=scope)
@classmethod
def create(
cls, url: Union[str, URL], *, commit_on_exit: bool = True, session_options: Optional[Mapping[str, Any]] = None, **kwargs
) -> "Database":
kwargs.setdefault("future", True)
engine = create_engine(url, **kwargs)
session_options = session_options or {}
return cls(engine, **session_options)
def session_generator(self) -> Generator[Session, Any, None]:
if self.scoped:
"""If the current context has a session, return it."""
yield self.session
else:
"""If the current context has no session, create a new session."""
with self.session_maker() as session:
yield session
if self.commit_on_exit:
session.commit()
def run_sync(
self,
fn: Callable[[Concatenate[Union[Session, Connection], _P]], _T],
*args: _P.args,
is_session: bool = True,
**kwargs: _P.kwargs,
) -> Union[_T, _R]:
if is_session:
return fn(self.session, *args, **kwargs)
with self.engine.begin() as conn:
return fn(conn, *args, **kwargs)
def asyncify(self, db: Union[AsyncSession, AsyncDatabase], fn: Callable[_P, _T]) -> Callable[_P, Awaitable[_T]]:
"""Convert the given sync function that runs in the context of a sync session to
an async function that runs in the context of an async session.
Args:
db: Async database client or session
fn: sync function
Returns:
Returns the async function.
"""
session = db if isinstance(db, AsyncSession) else db.session
@functools.wraps(fn)
async def wrapper(*args: _P.args, **kwargs: _P.kwargs) -> _T:
return await session.run_sync(self._sync_context_run, fn, *args, **kwargs)
return wrapper
def _sync_context_run(self, session: Session, fn: Callable[_P, _T], *args: _P.args, **kwargs: _P.kwargs) -> _T:
"""Run the given sync function in the context of the given sync session."""
with self(session):
return fn(*args, **kwargs)
class SessionContextVarManager:
_SessionCls = Session
def __init__(self, db: Database, scope: Any = None):
self.db = db
self._token = None
self._scope = scope
def __enter__(self):
if not self._scope:
"""If the user does not specify the scope, a new session is created by default,
and set as the current context session."""
session = self.db.session_maker()
self._token = self.db._session_scope.set(session)
self.db.scoped_session.registry.set(session)
elif isinstance(self._scope, self._SessionCls):
"""If the user specifies the current scope as a Session object,
the current Session object is set as the context session.
"""
self._token = self.db._session_scope.set(self._scope)
self.db.scoped_session.registry.set(self._scope)
else:
"""If the user specifies the scope as another type,
the scope is used as the context session variable identifier.
"""
self._token = self.db._session_scope.set(self._scope)
return self.db.session
def _close_session(self, session: Session, exc_type):
try:
if exc_type is not None:
session.rollback()
elif self.db.commit_on_exit:
session.commit()
finally:
session.close()
def __exit__(self, exc_type, exc_value, traceback):
if not (self._scope and isinstance(self._scope, self._SessionCls)):
"""If the scope is a session, it will not be closed."""
self._close_session(self.db.session, exc_type)
self.db.scoped_session.registry.clear()
self.db._session_scope.reset(self._token)
async def __aenter__(self):
return self.__enter__()
async def __aexit__(self, exc_type, exc_value, traceback):
if not (self._scope and isinstance(self._scope, self._SessionCls)):
"""If the scope is a session, it will not be closed."""
await to_thread(self._close_session, self.db.session, exc_type)
self.db.scoped_session.registry.clear()
self.db._session_scope.reset(self._token)
class AsyncSessionContextVarManager(SessionContextVarManager):
_SessionCls = AsyncSession
def __init__(self, db: AsyncDatabase, scope: Any = None):
super().__init__(db, scope) # type: ignore
def __exit__(self, exc_type, exc_val, exc_tb):
raise NotImplementedError("AsyncSessionContextVarManager does not support sync context manager.")
async def __aexit__(self, exc_type, exc_value, traceback):
self.db: AsyncDatabase
if not (self._scope and isinstance(self._scope, self._SessionCls)):
"""If the scope is a session, it will not be closed."""
session = self.db.session
try:
if exc_type is not None:
await session.rollback()
elif self.db.commit_on_exit:
await session.commit()
finally:
await session.close()
self.db.scoped_session.registry.clear()
self.db._session_scope.reset(self._token)