1818 </ script >
1919
2020 <!--[if lt IE 9]>
21- <script src="../js/html5shiv.min.js"></script>
21+ <script src="../js/html5shiv.min.js"></script>
2222 <![endif]-->
2323 < script src ="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.8.0/highlight.min.js "> </ script >
2424 < script > hljs . highlightAll ( ) ; </ script >
4444 </ li >
4545 </ ul >
4646 < ul >
47- < li class ="toctree-l1 "> < a class ="reference internal " href ="../problem "> The Problem It Solves</ a >
47+ < li class ="toctree-l1 "> < a class ="reference internal " href ="../problem/ "> The Problem It Solves</ a >
4848 </ li >
4949 </ ul >
5050 < ul >
51- < li class ="toctree-l1 "> < a class ="reference internal " href ="../getting_started "> Getting Started</ a >
51+ < li class ="toctree-l1 "> < a class ="reference internal " href ="../getting_started/ "> Getting Started</ a >
5252 </ li >
5353 </ ul >
5454 < ul >
55- < li class ="toctree-l1 "> < a class ="reference internal " href ="../examples "> Usage Examples</ a >
55+ < li class ="toctree-l1 "> < a class ="reference internal " href ="../examples/ "> Usage Examples</ a >
5656 </ li >
5757 </ ul >
5858 < ul class ="current ">
122122 </ li >
123123 </ ul >
124124 < ul >
125- < li class ="toctree-l1 "> < a class ="reference internal " href ="../concurrent_queries "> Concurrent SQL Queries</ a >
125+ < li class ="toctree-l1 "> < a class ="reference internal " href ="../concurrent_queries/ "> Concurrent SQL Queries</ a >
126126 </ li >
127127 </ ul >
128128 < ul >
129- < li class ="toctree-l1 "> < a class ="reference internal " href ="../master_replica "> Master/Replica or several databases at the same time</ a >
129+ < li class ="toctree-l1 "> < a class ="reference internal " href ="../master_replica/ "> Master/Replica or several databases at the same time</ a >
130130 </ li >
131131 </ ul >
132132 < ul >
133- < li class ="toctree-l1 "> < a class ="reference internal " href ="../testing "> Testing</ a >
133+ < li class ="toctree-l1 "> < a class ="reference internal " href ="../testing/ "> Testing</ a >
134134 </ li >
135135 </ ul >
136136 < ul >
137- < li class ="toctree-l1 "> < a class ="reference internal " href ="../how_middleware_works "> How middleware works</ a >
137+ < li class ="toctree-l1 "> < a class ="reference internal " href ="../how_middleware_works/ "> How middleware works</ a >
138138 </ li >
139139 </ ul >
140140 </ div >
152152 < ul class ="wy-breadcrumbs ">
153153 < li > < a href =".. " class ="icon icon-home " aria-label ="Docs "> </ a > </ li >
154154 < li class ="breadcrumb-item active "> API Reference</ li >
155+ < li class ="wy-breadcrumbs-aside ">
156+ < a href ="https://github.com/krylosov-aa/context-async-sqlalchemy/edit/master/docs/api.md " class ="icon icon-github "> Edit on GitHub</ a >
157+ </ li >
155158 </ ul >
156159 < hr />
157160</ div >
160163
161164 < h1 id ="api-reference "> API Reference</ h1 >
162165< h2 id ="dbconnect "> DBConnect</ h2 >
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 >
166+ < p > DBConnect is responsible for managing the engine and < code > session_maker</ code >
167+ You need to define two factories:</ p >
168+ < ul >
169+ < li > Specify host to which you want to connect (optional).</ li >
170+ < li > Specify a handler that runs before a session is created.
171+ It will be used to connect to the host for the first time or to reconnect to a new one (optional).</ li >
172+ </ ul >
171173< h3 id ="init "> init</ h3 >
172174< pre > < code class ="language-python "> def __init__(
173175 self: DBConnect,
@@ -211,20 +213,20 @@ <h3 id="connect">connect</h3>
211213</ code > </ pre >
212214< p > Establishes a connection to the specified host.
213215It doesn’t need to be called explicitly.
214- The first session request automatically
216+ If you don't use the call, the first session request will automatically
215217establishes the connection.</ p >
216218< hr />
217219< h3 id ="change_host "> change_host</ h3 >
218220< pre > < code class ="language-python "> async def change_host(self: DBConnect, host: str) -> None:
219221</ code > </ pre >
220- Establishes a connection to the specified host. It validates that the currently connected host is different
222+ < p > Establishes a connection to the specified host. It then validates that the currently connected host is different
221223from the target host.</ p >
222224< hr />
223225< h3 id ="create_session "> create_session</ h3 >
224226< pre > < code class ="language-python "> async def create_session(self: DBConnect) -> AsyncSession:
225227</ code > </ pre >
226228< p > Creates a new session the library uses internally.
227- You never need to call it directly.</ p >
229+ You never need to call it directly. (Only maybe in some special cases.) </ p >
228230< hr />
229231< h3 id ="session_maker "> session_maker</ h3 >
230232< pre > < code class ="language-python "> async def session_maker(self: DBConnect) -> async_sessionmaker[AsyncSession]:
@@ -237,8 +239,8 @@ <h3 id="close">close</h3>
237239< p > Closes and cleans up all resources, freeing the connection pool.
238240Use this call at the end of your application’s life cycle.</ p >
239241< h2 id ="middlewares "> Middlewares</ h2 >
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
241- own.</ p >
242+ < p > Most of the work the “magic” happens inside the middleware. Check out
243+ < a href =" ../how_middleware_works/ " > how it works </ a > and implement your own if the ready-made ones don't fit .</ p >
242244< h3 id ="fastapi "> FastAPI</ h3 >
243245< pre > < code class ="language-python "> from context_async_sqlalchemy.fastapi_utils import (
244246 fastapi_http_db_session_middleware,
@@ -290,7 +292,7 @@ <h3 id="db_session">db_session</h3>
290292< pre > < code class ="language-python "> async def db_session(connect: DBConnect) -> AsyncSession:
291293</ code > </ pre >
292294< p > The most important function for obtaining a session in your code.
293- When called for the first time, it returns a new session ; subsequent
295+ Returns a new session when you call it for the first time ; subsequent
294296calls return the same session.</ p >
295297< hr />
296298< h3 id ="atomic_db_session "> atomic_db_session</ h3 >
@@ -300,7 +302,7 @@ <h3 id="atomic_db_session">atomic_db_session</h3>
300302 current_transaction: Literal["commit", "rollback", "append", "raise"] = "commit",
301303) -> AsyncGenerator[AsyncSession, None]:
302304</ code > </ pre >
303- < p > A context manager that can be used to wrap another function which
305+ < p > A context manager you can use to wrap another function which
304306uses a context session, making that call isolated within its own transaction.</ p >
305307< p > Several options define how a function handles an open transaction.</ p >
306308< p > current_transaction:</ p >
@@ -325,27 +327,29 @@ <h3 id="close_db_session">close_db_session</h3>
325327< pre > < code class ="language-python "> async def close_db_session(connect: DBConnect) -> None:
326328</ code > </ pre >
327329< 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 >
330+ If you close an uncommitted transaction, the connection rolls back</ p >
331+ < p > This is useful when you need to run a database query at the start of the
332+ handler, then continue working over time without keeping the connection open.</ p >
331333< hr />
332334< h3 id ="new_non_ctx_session "> new_non_ctx_session</ h3 >
333335< pre > < code class ="language-python "> @asynccontextmanager
334336async def new_non_ctx_session(
335337 connect: DBConnect,
336338) -> AsyncGenerator[AsyncSession, None]:
337339</ code > </ pre >
338- < p > A context manager that allows you to create a new session without placing it in a context.</ p >
340+ < p > A context manager that allows you to create a new session without
341+ placing it in a context.</ p >
339342< hr />
340343< h3 id ="new_non_ctx_atomic_session "> new_non_ctx_atomic_session</ h3 >
341344< pre > < code class ="language-python "> @asynccontextmanager
342345async def new_non_ctx_atomic_session(
343346 connect: DBConnect,
344347) -> AsyncGenerator[AsyncSession, None]:
345348</ code > </ pre >
346- < p > A context manager that allows you to create a new session with
347- a new transaction that isn't placed in a context. It's used for manual
348- session management when you don't want to use a context.</ p >
349+ < p > Runs a function in a new context with new session(s) that have a separate connection.</ p >
350+ < p > It commits the transaction automatically if < code > callable_func</ code > does not
351+ raise exceptions. Otherwise, the transaction rolls back.</ p >
352+ < p > It is intended to allow you to run multiple database queries concurrently.</ p >
349353< h2 id ="context "> Context</ h2 >
350354< h3 id ="run_in_new_ctx "> run_in_new_ctx</ h3 >
351355< pre > < code class ="language-python "> async def run_in_new_ctx(
@@ -355,9 +359,9 @@ <h3 id="run_in_new_ctx">run_in_new_ctx</h3>
355359) -> AsyncCallableResult:
356360</ code > </ pre >
357361< p > Runs a function in a new context with new session(s) that have a
358- separate connection.</ p >
362+ separate connection.</ p >
359363< p > It commits the transaction automatically if < code > callable_func</ code > does not
360- raise exceptions. Otherwise, the transaction rolls back.</ p >
364+ raise exceptions. Otherwise, the transaction rolls back.</ p >
361365< p > It is intended to allow you to run multiple database queries concurrently.</ p >
362366< p > example of use:</ p >
363367< pre > < code class ="language-python "> await asyncio.gather(
@@ -369,7 +373,6 @@ <h3 id="run_in_new_ctx">run_in_new_ctx</h3>
369373)
370374</ code > </ pre >
371375< h2 id ="testing "> Testing</ h2 >
372-
373376< h3 id ="rollback_session "> rollback_session</ h3 >
374377< pre > < code class ="language-python "> @asynccontextmanager
375378async def rollback_session(
@@ -388,7 +391,7 @@ <h3 id="set_test_context">set_test_context</h3>
388391It is intended to test the application when it shares a single transaction.</ p >
389392< p > Use < code > auto_close=False</ code > if you’re using a test session and transaction
390393that you close elsewhere in your code.</ p >
391- < p > Use < code > auto_close=True</ code > to call a function
394+ < p > Use < code > auto_close=True</ code > if you want to call a function
392395in a test that uses a context while the middleware is not
393396active. All sessions close automatically.</ p >
394397< hr />
@@ -401,17 +404,15 @@ <h3 id="put_savepoint_session_in_ctx">put_savepoint_session_in_ctx</h3>
401404< p > Sets the context to a session that uses a save point instead of creating
402405 a transaction. You need to pass the session you're using inside
403406 your tests to attach a new session to the same connection.</ p >
404-
405407< pre > < code > It is important to use this function inside set_test_context.
406408</ code > </ pre >
407-
408- < p > Learn more about < a href ="../testing "> testing</ a > .</ p >
409+ < p > Learn more about < a href ="../testing/ "> testing</ a > </ p >
409410
410411 </ div >
411412 </ div > < footer >
412413 < div class ="rst-footer-buttons " role ="navigation " aria-label ="Footer Navigation ">
413- < a href ="../examples " class ="btn btn-neutral float-left " title ="Usage Examples "> < span class ="icon icon-circle-arrow-left "> </ span > Previous</ a >
414- < a href ="../concurrent_queries " class ="btn btn-neutral float-right " title ="Concurrent SQL Queries "> Next < span class ="icon icon-circle-arrow-right "> </ span > </ a >
414+ < a href ="../examples/ " class ="btn btn-neutral float-left " title ="Usage Examples "> < span class ="icon icon-circle-arrow-left "> </ span > Previous</ a >
415+ < a href ="../concurrent_queries/ " class ="btn btn-neutral float-right " title ="Concurrent SQL Queries "> Next < span class ="icon icon-circle-arrow-right "> </ span > </ a >
415416 </ div >
416417
417418 < hr />
@@ -438,10 +439,10 @@ <h3 id="put_savepoint_session_in_ctx">put_savepoint_session_in_ctx</h3>
438439 </ span >
439440
440441
441- < span > < a href ="../examples " style ="color: #fcfcfc "> « Previous</ a > </ span >
442+ < span > < a href ="../examples/ " style ="color: #fcfcfc "> « Previous</ a > </ span >
442443
443444
444- < span > < a href ="../concurrent_queries " style ="color: #fcfcfc "> Next »</ a > </ span >
445+ < span > < a href ="../concurrent_queries/ " style ="color: #fcfcfc "> Next »</ a > </ span >
445446
446447 </ span >
447448</ div >
0 commit comments