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 >
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) -> 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) -> 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
220221from 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) -> 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) -> 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) -> 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
242241own.</ 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>
285284app.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.
289288They 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) -> 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
305304uses 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) -> 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) -> 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) -> 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
337334async def new_non_ctx_session(
338335 connect: DBConnect,
339336) -> 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) -> 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
380375async def rollback_session(
381376 connection: DBConnect,
382377) -> 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
390384async def set_test_context(auto_close: bool = False) -> AsyncGenerator[None, None]:
391385</ code > </ pre >
392386< p > A context manager that creates a new context in which you can place a
393387dedicated 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
397390that 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
399392in 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 >
0 commit comments