fastapi-async-sqlalchemy 0.7.0.dev2__py3-none-any.whl → 0.7.0.dev4__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.
@@ -1,5 +1,9 @@
1
- from fastapi_async_sqlalchemy.middleware import SQLAlchemyMiddleware, db
1
+ from fastapi_async_sqlalchemy.middleware import (
2
+ SQLAlchemyMiddleware,
3
+ db,
4
+ create_middleware_and_session_proxy,
5
+ )
2
6
 
3
- __all__ = ["db", "SQLAlchemyMiddleware"]
7
+ __all__ = ["db", "SQLAlchemyMiddleware", "create_middleware_and_session_proxy"]
4
8
 
5
- __version__ = "0.7.0.dev2"
9
+ __version__ = "0.7.0.dev4"
@@ -21,9 +21,6 @@ def create_middleware_and_session_proxy():
21
21
  _Session: Optional[async_sessionmaker] = None
22
22
  _session: ContextVar[Optional[AsyncSession]] = ContextVar("_session", default=None)
23
23
  _multi_sessions_ctx: ContextVar[bool] = ContextVar("_multi_sessions_context", default=False)
24
- _task_session_ctx: ContextVar[Optional[AsyncSession]] = ContextVar(
25
- "_task_session_ctx", default=None
26
- )
27
24
  _commit_on_exit_ctx: ContextVar[bool] = ContextVar("_commit_on_exit_ctx", default=False)
28
25
  # Usage of context vars inside closures is not recommended, since they are not properly
29
26
  # garbage collected, but in our use case context var is created on program startup and
@@ -92,25 +89,22 @@ def create_middleware_and_session_proxy():
92
89
  ```
93
90
  """
94
91
  commit_on_exit = _commit_on_exit_ctx.get()
95
- session = _task_session_ctx.get()
96
- if session is None:
97
- session = _Session()
98
- _task_session_ctx.set(session)
99
-
100
- async def cleanup():
101
- try:
102
- if commit_on_exit:
103
- await session.commit()
104
- except Exception:
105
- await session.rollback()
106
- raise
107
- finally:
108
- await session.close()
109
- _task_session_ctx.set(None)
110
-
111
- task = asyncio.current_task()
112
- if task is not None:
113
- task.add_done_callback(lambda t: asyncio.create_task(cleanup()))
92
+ # Always create a new session for each access when multi_sessions=True
93
+ session = _Session()
94
+
95
+ async def cleanup():
96
+ try:
97
+ if commit_on_exit:
98
+ await session.commit()
99
+ except Exception:
100
+ await session.rollback()
101
+ raise
102
+ finally:
103
+ await session.close()
104
+
105
+ task = asyncio.current_task()
106
+ if task is not None:
107
+ task.add_done_callback(lambda t: asyncio.create_task(cleanup()))
114
108
  return session
115
109
  else:
116
110
  session = _session.get()
@@ -126,7 +120,6 @@ def create_middleware_and_session_proxy():
126
120
  multi_sessions: bool = False,
127
121
  ):
128
122
  self.token = None
129
- self.multi_sessions_token = None
130
123
  self.commit_on_exit_token = None
131
124
  self.session_args = session_args or {}
132
125
  self.commit_on_exit = commit_on_exit
@@ -1,6 +1,6 @@
1
- Metadata-Version: 2.1
1
+ Metadata-Version: 2.2
2
2
  Name: fastapi-async-sqlalchemy
3
- Version: 0.7.0.dev2
3
+ Version: 0.7.0.dev4
4
4
  Summary: SQLAlchemy middleware for FastAPI
5
5
  Home-page: https://github.com/h0rn3t/fastapi-async-sqlalchemy.git
6
6
  Author: Eugene Shershen
@@ -28,8 +28,19 @@ Classifier: Topic :: Software Development :: Libraries :: Python Modules
28
28
  Requires-Python: >=3.7
29
29
  Description-Content-Type: text/markdown
30
30
  License-File: LICENSE
31
- Requires-Dist: starlette >=0.13.6
32
- Requires-Dist: SQLAlchemy >=1.4.19
31
+ Requires-Dist: starlette>=0.13.6
32
+ Requires-Dist: SQLAlchemy>=1.4.19
33
+ Dynamic: author
34
+ Dynamic: author-email
35
+ Dynamic: classifier
36
+ Dynamic: description
37
+ Dynamic: description-content-type
38
+ Dynamic: home-page
39
+ Dynamic: license
40
+ Dynamic: project-url
41
+ Dynamic: requires-dist
42
+ Dynamic: requires-python
43
+ Dynamic: summary
33
44
 
34
45
  # SQLAlchemy FastAPI middleware
35
46
 
@@ -48,7 +59,7 @@ Provides SQLAlchemy middleware for FastAPI using AsyncSession and async engine.
48
59
  ### Install
49
60
 
50
61
  ```bash
51
- pip install fastapi-async-sqlalchemy
62
+ pip install fastapi-async-sqlalchemy
52
63
  ```
53
64
 
54
65
 
@@ -0,0 +1,9 @@
1
+ fastapi_async_sqlalchemy/__init__.py,sha256=7Ac5_oyQGoV5gcLGyEx83lWaT0kgJDYAxx9nlEGiZPU,236
2
+ fastapi_async_sqlalchemy/exceptions.py,sha256=dH3xjPE7B6kgq8L3LvxK-MsGZ_ZnW2WH2drLkK2oD-4,887
3
+ fastapi_async_sqlalchemy/middleware.py,sha256=0jrZrrH9YsimOqsx7hFtO-BvKiNLFeDuDcqYLSS7E_g,6577
4
+ fastapi_async_sqlalchemy/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
5
+ fastapi_async_sqlalchemy-0.7.0.dev4.dist-info/LICENSE,sha256=OhSfEMY0WqZhq41M6mLmkuFloAG7ZJYKfryF2jyxkfY,1108
6
+ fastapi_async_sqlalchemy-0.7.0.dev4.dist-info/METADATA,sha256=CGyu0yOvcj3JUJ_TsbUa8UiwxaYOxzWXmuBesxjOS0I,7068
7
+ fastapi_async_sqlalchemy-0.7.0.dev4.dist-info/WHEEL,sha256=In9FTNxeP60KnTkGw7wk6mJPYd_dQSjEZmXdBdMCI-8,91
8
+ fastapi_async_sqlalchemy-0.7.0.dev4.dist-info/top_level.txt,sha256=po-P5Tif35GmAaUR0mgZr5uBwN91iAgszaKI_r4ZTZA,25
9
+ fastapi_async_sqlalchemy-0.7.0.dev4.dist-info/RECORD,,
@@ -1,5 +1,5 @@
1
1
  Wheel-Version: 1.0
2
- Generator: setuptools (75.2.0)
2
+ Generator: setuptools (75.8.0)
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
5
5
 
@@ -1,9 +0,0 @@
1
- fastapi_async_sqlalchemy/__init__.py,sha256=uBiMzBBYVuNC52ekhbIasBTLsIhSvVyXY2KXD8nOWuQ,143
2
- fastapi_async_sqlalchemy/exceptions.py,sha256=dH3xjPE7B6kgq8L3LvxK-MsGZ_ZnW2WH2drLkK2oD-4,887
3
- fastapi_async_sqlalchemy/middleware.py,sha256=z3lPmnIihKyRfy34qE2z-38KuxFSXnVVcy_rg_S6IwM,6900
4
- fastapi_async_sqlalchemy/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
5
- fastapi_async_sqlalchemy-0.7.0.dev2.dist-info/LICENSE,sha256=OhSfEMY0WqZhq41M6mLmkuFloAG7ZJYKfryF2jyxkfY,1108
6
- fastapi_async_sqlalchemy-0.7.0.dev2.dist-info/METADATA,sha256=ANbR70mznxKwilOJ4OmYPVQ0OjmntfTdXr-SFcbL2eU,6831
7
- fastapi_async_sqlalchemy-0.7.0.dev2.dist-info/WHEEL,sha256=OVMc5UfuAQiSplgO0_WdW7vXVGAt9Hdd6qtN4HotdyA,91
8
- fastapi_async_sqlalchemy-0.7.0.dev2.dist-info/top_level.txt,sha256=po-P5Tif35GmAaUR0mgZr5uBwN91iAgszaKI_r4ZTZA,25
9
- fastapi_async_sqlalchemy-0.7.0.dev2.dist-info/RECORD,,