Skip to content

Commit 63b7eb2

Browse files
committed
update docs
1 parent 516d439 commit 63b7eb2

File tree

21 files changed

+821
-16
lines changed

21 files changed

+821
-16
lines changed

context_async_sqlalchemy/connect.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@ def __init__(
2626
before_create_session_handler: AsyncFunc | None = None,
2727
) -> None:
2828
"""
29-
host: Specify the host to connect to. The connection is lazy.
30-
3129
engine_creator: Specify a function that will return the
3230
configured AsyncEngine
3331
3432
session_maker_creator: Specify a function that will return the
3533
configured async_sessionmaker
3634
35+
host: Specify the host to connect to. The connection is lazy.
36+
3737
before_create_session_handler: You can specify a handler function that
3838
will be triggered before attempting to create a new session.
3939
You can, for example, check whether the host is alive and that

docs/404.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@
4848
<li class="toctree-l1"><a class="reference internal" href="/examples">Usage Examples</a>
4949
</li>
5050
</ul>
51+
<ul>
52+
<li class="toctree-l1"><a class="reference internal" href="/api/">API Reference</a>
53+
</li>
54+
</ul>
5155
<ul>
5256
<li class="toctree-l1"><a class="reference internal" href="/concurrent_queries/">Concurrent SQL Queries</a>
5357
</li>

docs/api/index.html

Lines changed: 404 additions & 0 deletions
Large diffs are not rendered by default.

docs/concurrent_queries/index.html

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@
5555
<li class="toctree-l1"><a class="reference internal" href="../examples">Usage Examples</a>
5656
</li>
5757
</ul>
58+
<ul>
59+
<li class="toctree-l1"><a class="reference internal" href="../api">API Reference</a>
60+
</li>
61+
</ul>
5862
<ul class="current">
5963
<li class="toctree-l1 current"><a class="reference internal current" href="#">Concurrent SQL Queries</a>
6064
<ul class="current">
@@ -180,7 +184,7 @@ <h1 id="concurrent-sql-queries">Concurrent sql queries</h1>
180184
</div>
181185
</div><footer>
182186
<div class="rst-footer-buttons" role="navigation" aria-label="Footer Navigation">
183-
<a href="../examples" class="btn btn-neutral float-left" title="Usage Examples"><span class="icon icon-circle-arrow-left"></span> Previous</a>
187+
<a href="../api" class="btn btn-neutral float-left" title="API Reference"><span class="icon icon-circle-arrow-left"></span> Previous</a>
184188
<a href="../master_replica" class="btn btn-neutral float-right" title="Master/Replica or several databases at the same time">Next <span class="icon icon-circle-arrow-right"></span></a>
185189
</div>
186190

@@ -208,7 +212,7 @@ <h1 id="concurrent-sql-queries">Concurrent sql queries</h1>
208212
</span>
209213

210214

211-
<span><a href="../examples" style="color: #fcfcfc">&laquo; Previous</a></span>
215+
<span><a href="../api" style="color: #fcfcfc">&laquo; Previous</a></span>
212216

213217

214218
<span><a href="../master_replica" style="color: #fcfcfc">Next &raquo;</a></span>

docs/examples/index.html

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,18 @@
6161
<li class="toctree-l2"><a class="reference internal" href="#manually-close-the-transaction-and-session">Manually close the transaction and session</a>
6262
</li>
6363
<li class="toctree-l2"><a class="reference internal" href="#multiple-sessions-and-concurrent-execution">Multiple sessions and concurrent execution</a>
64+
<ul>
65+
<li class="toctree-l3"><a class="reference internal" href="#rollback">Rollback</a>
66+
</li>
67+
</ul>
6468
</li>
6569
</ul>
6670
</li>
6771
</ul>
72+
<ul>
73+
<li class="toctree-l1"><a class="reference internal" href="../api">API Reference</a>
74+
</li>
75+
</ul>
6876
<ul>
6977
<li class="toctree-l1"><a class="reference internal" href="../concurrent_queries">Concurrent SQL Queries</a>
7078
</li>
@@ -373,7 +381,7 @@ <h3 id="rollback">Rollback</h3>
373381
</div><footer>
374382
<div class="rst-footer-buttons" role="navigation" aria-label="Footer Navigation">
375383
<a href="../getting_started" class="btn btn-neutral float-left" title="Getting Started"><span class="icon icon-circle-arrow-left"></span> Previous</a>
376-
<a href="../concurrent_queries" class="btn btn-neutral float-right" title="Concurrent SQL Queries">Next <span class="icon icon-circle-arrow-right"></span></a>
384+
<a href="../api" class="btn btn-neutral float-right" title="API Reference">Next <span class="icon icon-circle-arrow-right"></span></a>
377385
</div>
378386

379387
<hr/>
@@ -403,7 +411,7 @@ <h3 id="rollback">Rollback</h3>
403411
<span><a href="../getting_started" style="color: #fcfcfc">&laquo; Previous</a></span>
404412

405413

406-
<span><a href="../concurrent_queries" style="color: #fcfcfc">Next &raquo;</a></span>
414+
<span><a href="../api" style="color: #fcfcfc">Next &raquo;</a></span>
407415

408416
</span>
409417
</div>

docs/getting_started/index.html

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,16 @@
5555
<li class="toctree-l2"><a class="reference internal" href="#manage-database-connection-lifecycle">Manage Database connection lifecycle</a>
5656
</li>
5757
<li class="toctree-l2"><a class="reference internal" href="#setup-middleware">Setup middleware</a>
58+
<ul>
59+
<li class="toctree-l3"><a class="reference internal" href="#pure-asgi">Pure ASGI</a>
60+
</li>
61+
<li class="toctree-l3"><a class="reference internal" href="#fastapi">FastAPI</a>
62+
</li>
63+
<li class="toctree-l3"><a class="reference internal" href="#starlette">Starlette</a>
64+
</li>
65+
<li class="toctree-l3"><a class="reference internal" href="#write-own">Write own</a>
66+
</li>
67+
</ul>
5868
</li>
5969
<li class="toctree-l2"><a class="reference internal" href="#use-it">Use it</a>
6070
</li>
@@ -67,6 +77,10 @@
6777
<li class="toctree-l1"><a class="reference internal" href="../examples">Usage Examples</a>
6878
</li>
6979
</ul>
80+
<ul>
81+
<li class="toctree-l1"><a class="reference internal" href="../api">API Reference</a>
82+
</li>
83+
</ul>
7084
<ul>
7185
<li class="toctree-l1"><a class="reference internal" href="../concurrent_queries">Concurrent SQL Queries</a>
7286
</li>
@@ -147,11 +161,15 @@ <h2 id="configure-the-connection-to-the-database">Configure the connection to th
147161

148162

149163
connection = DBConnect(
150-
host=&quot;127.0.0.1&quot;,
151164
engine_creator=create_engine,
152165
session_maker_creator=create_session_maker,
166+
host=&quot;127.0.0.1&quot;, # optional
153167
)
154168
</code></pre>
169+
<p>The <strong>host</strong> parameter is optional if you use a handler
170+
before creating a session - <code>before_create_session_handler</code>.
171+
In that case, you can dynamically set the host.</p>
172+
<p>Read more in <a href="../master_replica">Master/Replica or several databases at the same time</a></p>
155173
<h2 id="manage-database-connection-lifecycle">Manage Database connection lifecycle</h2>
156174
<p>Close resources at the end of your application's lifecycle.</p>
157175
<p>Example for FastAPI:</p>
@@ -172,7 +190,7 @@ <h2 id="setup-middleware">Setup middleware</h2>
172190
<p>Middleware handles the most important and complex part -
173191
managing context and sessions.</p>
174192
<p>You can use the ready-made middleware components:</p>
175-
<h3 id="asgi-middleware">Pure ASGI</h3>
193+
<h3 id="pure-asgi">Pure ASGI</h3>
176194
<pre><code class="language-python">from context_async_sqlalchemy import ASGIHTTPDBSessionMiddleware
177195

178196
app.add_middleware(ASGIHTTPDBSessionMiddleware)

docs/how_middleware_works/index.html

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@
5555
<li class="toctree-l1"><a class="reference internal" href="../examples">Usage Examples</a>
5656
</li>
5757
</ul>
58+
<ul>
59+
<li class="toctree-l1"><a class="reference internal" href="../api">API Reference</a>
60+
</li>
61+
</ul>
5862
<ul>
5963
<li class="toctree-l1"><a class="reference internal" href="../concurrent_queries">Concurrent SQL Queries</a>
6064
</li>
@@ -70,6 +74,8 @@
7074
<ul class="current">
7175
<li class="toctree-l1 current"><a class="reference internal current" href="#">How middleware works</a>
7276
<ul class="current">
77+
<li class="toctree-l2"><a class="reference internal" href="#code-example">Code example</a>
78+
</li>
7379
</ul>
7480
</li>
7581
</ul>
@@ -95,11 +101,33 @@
95101
<div class="section" itemprop="articleBody">
96102

97103
<h1 id="how-middleware-works">How middleware works</h1>
98-
<p>Most of the work - and the “magic” - happens inside the middleware.
99-
The library aims to provide ready-made solutions so that you don’t have to
104+
<p>Most of the work - and the “magic” - happens inside the middleware.</p>
105+
<p>Here is a diagram describing how it works.</p>
106+
<p><img alt="middleware_schema.png" src="../img/middleware_schema.png" /></p>
107+
<p>At the beginning of a request, the middleware initializes a new
108+
asynchronous context.
109+
This asynchronous context is implemented using a contextvar.
110+
It holds a mutable container that stores sessions.</p>
111+
<p>A mutable container is used so that any coroutine, at any level, can
112+
create, modify, or close sessions, and those changes will
113+
affect the execution of the entire request.</p>
114+
<p>Whenever your code accesses the library’s functionality, it interacts with
115+
this container.</p>
116+
<p>Finally, the middleware checks the container for any active sessions and
117+
open transactions.
118+
If transactions are open, they are either committed when the query
119+
execution is successful or rolled back if it fails.
120+
After that, all sessions are closed.</p>
121+
<p>That’s precisely why you can safely close transactions and sessions early.
122+
The middleware simply works with whatever is in the container:
123+
if there’s anything left, it will close it properly; if you’ve
124+
already handled it yourself, the middleware only needs to reset the context.</p>
125+
<h2 id="code-example">Code example</h2>
126+
<p>The library aims to provide ready-made solutions so that you don’t have to
100127
worry about these details - but they’re not always available.</p>
101128
<p>So, let’s take a look at how Starlette middleware works.
102129
You can use this example as a reference to implement your own.</p>
130+
<p><a href="https://github.com/krylosov-aa/context-async-sqlalchemy/blob/main/context_async_sqlalchemy/starlette_utils/http_middleware.py#L34">CODE</a></p>
103131
<pre><code class="language-python">from starlette.middleware.base import ( # type: ignore[attr-defined]
104132
Request,
105133
Response,

docs/img/middleware_schema.png

98.8 KB
Loading

docs/index.html

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@
6161
<li class="toctree-l1"><a class="reference internal" href="examples">Usage Examples</a>
6262
</li>
6363
</ul>
64+
<ul>
65+
<li class="toctree-l1"><a class="reference internal" href="api">API Reference</a>
66+
</li>
67+
</ul>
6468
<ul>
6569
<li class="toctree-l1"><a class="reference internal" href="concurrent_queries">Concurrent SQL Queries</a>
6670
</li>
@@ -214,5 +218,5 @@ <h2 id="how-it-works">How it works</h2>
214218

215219
<!--
216220
MkDocs version : 1.6.1
217-
Build Date UTC : 2025-11-23 21:26:35.871042+00:00
221+
Build Date UTC : 2025-11-25 20:53:03.512231+00:00
218222
-->

docs/master_replica/index.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@
5555
<li class="toctree-l1"><a class="reference internal" href="../examples">Usage Examples</a>
5656
</li>
5757
</ul>
58+
<ul>
59+
<li class="toctree-l1"><a class="reference internal" href="../api">API Reference</a>
60+
</li>
61+
</ul>
5862
<ul>
5963
<li class="toctree-l1"><a class="reference internal" href="../concurrent_queries">Concurrent SQL Queries</a>
6064
</li>

0 commit comments

Comments
 (0)