Skip to content

Commit 2d795c9

Browse files
API section docs suggestions (#15)
* docs edits index getting started problem it solves Base docs suggestions as per the commit message. Will open a separate PR for the other stuff I'd suggest in general. * made minor/moderate wording changes from the Usage Examples and API References sections, including the text represnted in code which has been COMMENTED OUT, NOT THE CODE ITSELF * Docs changes to API Reference section --------- Co-authored-by: annibond <anni.simpson@gmail.com>
1 parent ced494a commit 2d795c9

File tree

24 files changed

+171
-210
lines changed

24 files changed

+171
-210
lines changed

README.md

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ transactions
2525
- Not a wrapper around SQLAlchemy
2626
- Convenient for testing
2727
- Runtime host switching
28-
- Supports multiple databases and multiple sessions per database
28+
- Supports multiple databases and sessions per database
2929
- Provides tools for running concurrent SQL queries
3030
- Fully lazy initialization
3131

@@ -37,25 +37,25 @@ from context_async_sqlalchemy import db_session
3737
from sqlalchemy import insert
3838

3939
from database import connection # your configured connection to the database
40-
from models import ExampleTable # just some model for example
40+
from models import ExampleTable # a model for example
4141

4242
async def some_func() -> None:
43-
# Created a session (no connection to the database yet)
43+
# Creates a session with no connection to the database yet
4444
session = await db_session(connection)
4545

4646
stmt = insert(ExampleTable).values(text="example_with_db_session")
4747

48-
# On the first request, a connection and transaction were opened
48+
# A connection and transaction open in the first request.
4949
await session.execute(stmt)
5050

51-
# If you call db_session again, it will return the same session
51+
# If you call db_session again, it returns the same session
5252
# even in child coroutines.
5353
session = await db_session(connection)
5454

55-
# The second request will use the same connection and the same transaction
55+
# The second request uses the same connection and the same transaction
5656
await session.execute(stmt)
5757

58-
# The commit and closing of the session will occur automatically
58+
# The commit and closing of the session occurs automatically
5959
```
6060

6161
## How it works
@@ -70,5 +70,4 @@ existing ones.
7070
3. The middleware automatically commits or rolls back open
7171
transactions. It also closes open sessions and clears the context.
7272

73-
The library also provides the ability to commit, roll back, and close at any
74-
time without waiting for the end of the request.
73+
The library provides the ability to commit, roll back, and close at any

context_async_sqlalchemy/session.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,12 @@ async def atomic_db_session(
4040
current_transaction: _current_transaction_choices = "commit",
4141
) -> AsyncGenerator[AsyncSession, None]:
4242
"""
43-
A context manager that can be used to wrap another function which
44-
uses a context session, making that call isolated within its
43+
A context manager that you can use to wrap another function which
44+
uses a context session, isolating the call within its
4545
own transaction.
4646
47-
There are several options that define how the function will handle
48-
an already open transaction.
47+
There are several options that define how the function handles
48+
an open transaction.
4949
current_transaction:
5050
"commit" - commits the open transaction and starts a new one
5151
"rollback" - rolls back the open transaction and starts a new one
@@ -80,7 +80,7 @@ async def atomic_db_session(
8080

8181
async def commit_db_session(connect: DBConnect) -> None:
8282
"""
83-
Commits the active session, if there is one.
83+
Commits the active session.
8484
8585
example of use:
8686
await your_function_with_db_session()
@@ -93,7 +93,7 @@ async def commit_db_session(connect: DBConnect) -> None:
9393

9494
async def rollback_db_session(connect: DBConnect) -> None:
9595
"""
96-
Rollbacks the active session, if there is one.
96+
Rolls back the active session.
9797
9898
example of use:
9999
await your_function_with_db_session()
@@ -108,9 +108,7 @@ async def close_db_session(connect: DBConnect) -> None:
108108
"""
109109
Closes the active session (and connection), if there is one.
110110
111-
This is useful if, for example, at the beginning of the handle a
112-
database query is needed, and then there is some other long-term work
113-
and you don't want to keep the connection opened.
111+
Use if you have more work you need to complete without keeping the connection open.
114112
115113
example of use:
116114
await your_function_with_db_session()

context_async_sqlalchemy/test_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ async def put_savepoint_session_in_ctx(
6464
a transaction. You need to pass the session you're using inside
6565
your tests to attach a new session to the same connection.
6666
67-
It is important to use this function inside set_test_context.
67+
Use this function inside set_test_context.
6868
"""
6969
session_maker = await connection.session_maker()
7070
async with session_maker(

docs/api/index.html

Lines changed: 43 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@
7676
</li>
7777
<li class="toctree-l2"><a class="reference internal" href="#middlewares">Middlewares</a>
7878
<ul>
79-
<li class="toctree-l3"><a class="reference internal" href="#fastapi">Fastapi</a>
79+
<li class="toctree-l3"><a class="reference internal" href="#fastapi">FastAPI</a>
8080
</li>
8181
<li class="toctree-l3"><a class="reference internal" href="#starlette">Starlette</a>
8282
</li>
@@ -160,12 +160,14 @@
160160

161161
<h1 id="api-reference">API Reference</h1>
162162
<h2 id="dbconnect">DBConnect</h2>
163-
<p>DBConnect is responsible for managing the engine and the session_maker.
164-
You need to define two factories.
165-
Optionally, you can specify a host to connect to.
166-
You can also specify a handler that runs before a session is created -
167-
this handler can be used to connect to the host for the first time or
168-
to reconnect to a new one.</p>
163+
<p>DBConnect is responsible for managing the engine and <code>session_maker</code>.
164+
You need to define two factories:
165+
<ul>
166+
<li>Specify host to which you want to connect (optional).</li>
167+
<li>Specify a handler that runs before a session is created. It be used to connect to the host for the first time or
168+
to reconnect to a new one.</li>
169+
</ul>
170+
</p>
169171
<h3 id="init">init</h3>
170172
<pre><code class="language-python">def __init__(
171173
self: DBConnect,
@@ -208,39 +210,36 @@ <h3 id="connect">connect</h3>
208210
<pre><code class="language-python">async def connect(self: DBConnect, host: str) -&gt; None:
209211
</code></pre>
210212
<p>Establishes a connection to the specified host.
211-
This method doesn’t need to be called explicitly.
212-
If it isn’t called, the first session request will automatically
213-
establish the connection.</p>
213+
It doesn’t need to be called explicitly.
214+
The first session request automatically
215+
establishes the connection.</p>
214216
<hr />
215217
<h3 id="change_host">change_host</h3>
216218
<pre><code class="language-python">async def change_host(self: DBConnect, host: str) -&gt; None:
217219
</code></pre>
218-
<p>Establishes a connection to the specified host, but first
219-
checks under a lock that the currently connected host is different
220+
Establishes a connection to the specified host. It validates that the currently connected host is different
220221
from the target host.</p>
221222
<hr />
222223
<h3 id="create_session">create_session</h3>
223224
<pre><code class="language-python">async def create_session(self: DBConnect) -&gt; AsyncSession:
224225
</code></pre>
225-
<p>Creates a new session. Used internally by the library -
226-
you’ll probably never need to call it directly, but it’s
227-
good to know it exists.</p>
226+
<p>Creates a new session the library uses internally.
227+
You never need to call it directly.</p>
228228
<hr />
229229
<h3 id="session_maker">session_maker</h3>
230230
<pre><code class="language-python">async def session_maker(self: DBConnect) -&gt; async_sessionmaker[AsyncSession]:
231231
</code></pre>
232-
<p>Provides access to the session_maker currently used to create sessions.</p>
232+
<p>Provides access to the <code>session_maker</code> currently used to create sessions.</p>
233233
<hr />
234234
<h3 id="close">close</h3>
235235
<pre><code class="language-python">async def close(self: DBConnect) -&gt; None:
236236
</code></pre>
237-
<p>Completely closes and cleans up all resources, freeing the connection pool.
238-
This should be called at the end of your application’s lifecycle.</p>
237+
<p>Closes and cleans up all resources, freeing the connection pool.
238+
Use this call at the end of your application’s life cycle.</p>
239239
<h2 id="middlewares">Middlewares</h2>
240-
<p>Most of the work - and the “magic” - happens inside the middleware.</p>
241-
<p>You can check out <a href="../how_middleware_works">how it works</a> and implement your
240+
<p>Most of the work the “magic” happens inside the middleware. Check out <a href="../how_middleware_works">how it works</a> and implement your
242241
own.</p>
243-
<h3 id="fastapi">Fastapi</h3>
242+
<h3 id="fastapi">FastAPI</h3>
244243
<pre><code class="language-python">from context_async_sqlalchemy.fastapi_utils import (
245244
fastapi_http_db_session_middleware,
246245
add_fastapi_http_db_session_middleware,
@@ -285,7 +284,7 @@ <h3 id="pure-asgi">Pure ASGI</h3>
285284
app.add_middleware(ASGIHTTPDBSessionMiddleware)
286285
</code></pre>
287286
<h2 id="sessions">Sessions</h2>
288-
<p>Here are the functions you’ll use most often from the library.
287+
<p>Here are the library functions you will use most often.
289288
They allow you to work with sessions directly from your asynchronous code.</p>
290289
<h3 id="db_session">db_session</h3>
291290
<pre><code class="language-python">async def db_session(connect: DBConnect) -&gt; AsyncSession:
@@ -303,8 +302,7 @@ <h3 id="atomic_db_session">atomic_db_session</h3>
303302
</code></pre>
304303
<p>A context manager that can be used to wrap another function which
305304
uses a context session, making that call isolated within its own transaction.</p>
306-
<p>There are several options that define how the function will handle
307-
an already open transaction.</p>
305+
<p>Several options define how a function handles an open transaction.</p>
308306
<p>current_transaction:</p>
309307
<ul>
310308
<li><code>commit</code> - commits the open transaction and starts a new one</li>
@@ -316,31 +314,28 @@ <h3 id="atomic_db_session">atomic_db_session</h3>
316314
<h3 id="commit_db_session">commit_db_session</h3>
317315
<pre><code class="language-python">async def commit_db_session(connect: DBConnect) -&gt; None:
318316
</code></pre>
319-
<p>Commits the active session, if there is one.</p>
317+
<p>Commits the active session.</p>
320318
<hr />
321319
<h3 id="rollback_db_session">rollback_db_session</h3>
322320
<pre><code class="language-python">async def rollback_db_session(connect: DBConnect) -&gt; None:
323321
</code></pre>
324-
<p>Rollbacks the active session, if there is one.</p>
322+
<p>Rolls back an active session.</p>
325323
<hr />
326324
<h3 id="close_db_session">close_db_session</h3>
327325
<pre><code class="language-python">async def close_db_session(connect: DBConnect) -&gt; None:
328326
</code></pre>
329-
<p>Closes the current context session. The connection is returned to the pool.
330-
If you close an uncommitted transaction, the connection will be rolled back.</p>
331-
<p>This is useful if, for example, at the beginning of the handle a
332-
database query is needed, and then there is some other long-term work
333-
and you don't want to keep the connection opened.</p>
327+
<p>Closes the current context session and returns the connection to the pool.
328+
If you close an uncommitted transaction, the connection rolls back.</p>
329+
<p>This is useful if a database querly is needed at the beginning of the handle.</p>
330+
<p>You have have more work you need to complete over time without being able to keep the connection open.</p>
334331
<hr />
335332
<h3 id="new_non_ctx_session">new_non_ctx_session</h3>
336333
<pre><code class="language-python">@asynccontextmanager
337334
async def new_non_ctx_session(
338335
connect: DBConnect,
339336
) -&gt; AsyncGenerator[AsyncSession, None]:
340337
</code></pre>
341-
<p>A context manager that allows you to create a new session without placing
342-
it in a context. It's used for manual session management when you
343-
don't want to use a context.</p>
338+
<p>A context manager that allows you to create a new session without placing it in a context.</p>
344339
<hr />
345340
<h3 id="new_non_ctx_atomic_session">new_non_ctx_atomic_session</h3>
346341
<pre><code class="language-python">@asynccontextmanager
@@ -359,11 +354,11 @@ <h3 id="run_in_new_ctx">run_in_new_ctx</h3>
359354
**kwargs: Any,
360355
) -&gt; AsyncCallableResult:
361356
</code></pre>
362-
<p>Runs a function in a new context with new sessions that have their
363-
own connection.</p>
364-
<p>It will commit the transaction automatically if callable_func does not
365-
raise exceptions. Otherwise, the transaction will be rolled back.</p>
366-
<p>The intended use is to run multiple database queries concurrently.</p>
357+
<p>Runs a function in a new context with new session(s) that have a
358+
separate connection.</p>
359+
<p>It commits the transaction automatically if <code>callable_func</code> does not
360+
raise exceptions. Otherwise, the transaction rolls back.</p>
361+
<p>It is intended to allow you to run multiple database queries concurrently.</p>
367362
<p>example of use:</p>
368363
<pre><code class="language-python">await asyncio.gather(
369364
your_function_with_db_session(...),
@@ -374,30 +369,28 @@ <h3 id="run_in_new_ctx">run_in_new_ctx</h3>
374369
)
375370
</code></pre>
376371
<h2 id="testing">Testing</h2>
377-
<p>You can read more about testing here: <a href="../testing">Testing</a></p>
372+
378373
<h3 id="rollback_session">rollback_session</h3>
379374
<pre><code class="language-python">@asynccontextmanager
380375
async def rollback_session(
381376
connection: DBConnect,
382377
) -&gt; AsyncGenerator[AsyncSession, None]:
383378
</code></pre>
384-
<p>A context manager that creates a session which is automatically rolled
385-
back at the end.
386-
It’s intended for use in fixtures to execute SQL queries during tests.</p>
379+
<p>A context manager that creates a session which automatically rolls back at the end of the session.
380+
It is intended for you to use in fixtures to execute SQL queries during tests.</p>
387381
<hr />
388382
<h3 id="set_test_context">set_test_context</h3>
389383
<pre><code class="language-python">@asynccontextmanager
390384
async def set_test_context(auto_close: bool = False) -&gt; AsyncGenerator[None, None]:
391385
</code></pre>
392386
<p>A context manager that creates a new context in which you can place a
393387
dedicated test session.
394-
It’s intended for use in tests where the test and the application share
395-
a single transaction.</p>
388+
It is intended to test the application when it shares a single transaction.</p>
396389
<p>Use <code>auto_close=False</code> if you’re using a test session and transaction
397390
that you close elsewhere in your code.</p>
398-
<p>Use <code>auto_close=True</code> if you want to call a function
391+
<p>Use <code>auto_close=True</code> to call a function
399392
in a test that uses a context while the middleware is not
400-
active, and you want all sessions to be closed automatically.</p>
393+
active. All sessions close automatically.</p>
401394
<hr />
402395
<h3 id="put_savepoint_session_in_ctx">put_savepoint_session_in_ctx</h3>
403396
<pre><code class="language-python">async def put_savepoint_session_in_ctx(
@@ -408,8 +401,11 @@ <h3 id="put_savepoint_session_in_ctx">put_savepoint_session_in_ctx</h3>
408401
<p>Sets the context to a session that uses a save point instead of creating
409402
a transaction. You need to pass the session you're using inside
410403
your tests to attach a new session to the same connection.</p>
404+
411405
<pre><code>It is important to use this function inside set_test_context.
412406
</code></pre>
407+
408+
<p>Learn more about <a href="../testing">testing</a>.</p>
413409

414410
</div>
415411
</div><footer>

docs/concurrent_queries/index.html

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,10 @@ <h1 id="concurrent-sql-queries">Concurrent sql queries</h1>
126126

127127
async def handler_multiple_sessions() -&gt; None:
128128
&quot;&quot;&quot;
129-
In some situations, you need to have multiple sessions running
130-
simultaneously. For example, to run several queries concurrently.
129+
You may need to to run multiple sessions. For example, to run several queries concurrently.
131130

132-
You can also use these same techniques to create new sessions whenever you
133-
need them. Not necessarily just because of the concurrent processing.
131+
You can also use the same techniques to create new sessions whenever you
132+
need them, ot necessarily because of the concurrent processing.
134133
&quot;&quot;&quot;
135134
await asyncio.gather(
136135
_insert(), # context session
@@ -154,17 +153,16 @@ <h1 id="concurrent-sql-queries">Concurrent sql queries</h1>
154153
stmt = insert(ExampleTable).values(text=text)
155154
await session.execute(stmt)
156155

157-
# You can manually commit the transaction if you want, but it is not
158-
# necessary
156+
# manually commits the transaction (optional)
159157
await commit_db_session(connection)
160158

161-
# You can manually close the session if you want, but it is not necessary
159+
# manually closes the session (optional)
162160
await close_db_session(connection)
163161

164162

165163
async def _insert_non_ctx() -&gt; None:
166164
&quot;&quot;&quot;
167-
You don't have to use the context to work with sessions at all
165+
Using context to work with sessions is optional.
168166
&quot;&quot;&quot;
169167
async with new_non_ctx_atomic_session(connection) as session:
170168
stmt = insert(ExampleTable).values(text=&quot;example_multiple_sessions&quot;)
@@ -173,7 +171,7 @@ <h1 id="concurrent-sql-queries">Concurrent sql queries</h1>
173171

174172
async def _insert_non_ctx_manual() -&gt; None:
175173
&quot;&quot;&quot;
176-
You don't have to use the context to work with sessions at all
174+
Using context to work with sessions is optional.
177175
&quot;&quot;&quot;
178176
async with new_non_ctx_session(connection) as session:
179177
stmt = insert(ExampleTable).values(text=&quot;example_multiple_sessions&quot;)

0 commit comments

Comments
 (0)