Skip to content

Commit 3bfd0be

Browse files
committed
refactoring examples
1 parent cc47ced commit 3bfd0be

32 files changed

+301
-458
lines changed
File renamed without changes.

examples/fastapi_example/routes/atomic_usage.py renamed to examples/fastapi_example/routes/atomic.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
from context_async_sqlalchemy import atomic_db_session, db_session
22
from sqlalchemy import insert
33

4-
from ..database import connection
5-
from ..models import ExampleTable
4+
from examples.database import connection
5+
from examples.models import ExampleTable
66

77

8-
async def handler_with_db_session_and_atomic() -> None:
8+
async def atomic_base_example() -> None:
99
"""
1010
Let's imagine you already have a function that works with a contextual
1111
session, and its use case calls autocommit at the end of the request.
@@ -23,7 +23,5 @@ async def handler_with_db_session_and_atomic() -> None:
2323

2424
async def _insert_1() -> None:
2525
session = await db_session(connection)
26-
stmt = insert(ExampleTable).values(
27-
text="example_with_db_session_and_atomic"
28-
)
26+
stmt = insert(ExampleTable).values()
2927
await session.execute(stmt)

examples/fastapi_example/routes/atomic_usage_2.py renamed to examples/fastapi_example/routes/atomic_prev_transaction.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
from context_async_sqlalchemy import atomic_db_session, db_session
22
from sqlalchemy import insert
33

4-
from ..database import connection
5-
from ..models import ExampleTable
4+
from examples.database import connection
5+
from examples.models import ExampleTable
66

77

8-
async def handler_with_db_session_and_atomic_2() -> None:
8+
async def atomic_and_previous_transaction() -> None:
99
"""
1010
Let's imagine you already have a function that works with a contextual
1111
session, and its use case calls autocommit at the end of the request.
@@ -43,7 +43,5 @@ async def handler_with_db_session_and_atomic_2() -> None:
4343

4444
async def _insert_1() -> None:
4545
session = await db_session(connection)
46-
stmt = insert(ExampleTable).values(
47-
text="example_with_db_session_and_atomic"
48-
)
46+
stmt = insert(ExampleTable).values()
4947
await session.execute(stmt)

examples/fastapi_example/routes/simple_with_exception.py renamed to examples/fastapi_example/routes/auto_rollback_by_exception.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
from context_async_sqlalchemy import db_session
22
from sqlalchemy import insert
33

4-
from ..database import connection
5-
from ..models import ExampleTable
4+
from examples.database import connection
5+
from examples.models import ExampleTable
66

77

8-
async def handler_with_db_session_and_exception() -> None:
8+
async def auto_rollback_by_exception() -> None:
99
"""
1010
let's imagine that an exception occurred.
1111
"""
1212
session = await db_session(connection)
13-
stmt = insert(ExampleTable).values(text="example_with_db_session")
13+
stmt = insert(ExampleTable)
1414
await session.execute(stmt)
1515

1616
raise Exception("Some exception")

examples/fastapi_example/routes/simple_with_http_exception.py renamed to examples/fastapi_example/routes/auto_rollback_by_status_code.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
from context_async_sqlalchemy import db_session
44
from sqlalchemy import insert
55

6-
from ..database import connection
7-
from ..models import ExampleTable
6+
from examples.database import connection
7+
from examples.models import ExampleTable
88

99

10-
async def handler_with_db_session_and_http_exception() -> None:
10+
async def auto_rollback_by_status_code() -> None:
1111
"""
12-
let's imagine that an http exception occurred.
12+
let's imagine that an error code was returned.
1313
"""
1414
session = await db_session(connection)
1515
stmt = insert(ExampleTable).values(text="example_with_db_session")

examples/fastapi_example/routes/multiple_session_usage.py renamed to examples/fastapi_example/routes/councurrent_queries.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
1010
)
1111
from sqlalchemy import insert
1212

13-
from ..database import connection
14-
from ..models import ExampleTable
13+
from examples.database import connection
14+
from examples.models import ExampleTable
1515

1616

17-
async def handler_multiple_sessions() -> None:
17+
async def concurrent_queries() -> None:
1818
"""
1919
In some situations, you need to have multiple sessions running
2020
simultaneously. For example, to run several queries concurrently.

examples/fastapi_example/routes/manual_commit.py renamed to examples/fastapi_example/routes/early_commit.py

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
)
66
from sqlalchemy import insert
77

8-
from ..database import connection
9-
from ..models import ExampleTable
8+
from examples.database import connection
9+
from examples.models import ExampleTable
1010

1111

12-
async def handler_with_db_session_and_manual_close() -> None:
12+
async def early_commit() -> None:
1313
"""
1414
An example of a handle that uses a session in context,
1515
but commits manually and even closes the session to release the
@@ -28,9 +28,7 @@ async def handler_with_db_session_and_manual_close() -> None:
2828

2929
async def _insert_1() -> None:
3030
session = await db_session(connection)
31-
stmt = insert(ExampleTable).values(
32-
text="example_with_db_session_and_manual_close"
33-
)
31+
stmt = insert(ExampleTable)
3432
await session.execute(stmt)
3533

3634
# Here we closed the transaction
@@ -39,9 +37,7 @@ async def _insert_1() -> None:
3937

4038
async def _insert_2() -> None:
4139
session = await db_session(connection)
42-
stmt = insert(ExampleTable).values(
43-
text="example_with_db_session_and_manual_close"
44-
)
40+
stmt = insert(ExampleTable)
4541
await session.execute(stmt)
4642

4743
# Here we closed the transaction
@@ -56,7 +52,5 @@ async def _insert_2() -> None:
5652

5753
async def _insert_3() -> None:
5854
session = await db_session(connection)
59-
stmt = insert(ExampleTable).values(
60-
text="example_with_db_session_and_manual_close"
61-
)
55+
stmt = insert(ExampleTable)
6256
await session.execute(stmt)

examples/fastapi_example/routes/early_connection_close.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
)
66
from sqlalchemy import insert
77

8-
from ..database import connection
9-
from ..models import ExampleTable
8+
from examples.database import connection
9+
from examples.models import ExampleTable
1010

1111

12-
async def handler_with_early_connection_close() -> None:
12+
async def early_connection_close() -> None:
1313
"""
1414
An example when you can return a connection to the connection pool for a
1515
long period of work unrelated to the database

examples/fastapi_example/routes/manual_rollback.py renamed to examples/fastapi_example/routes/early_rollback.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
from context_async_sqlalchemy import db_session, rollback_db_session
22
from sqlalchemy import insert
33

4-
from ..database import connection
5-
from ..models import ExampleTable
4+
from examples.database import connection
5+
from examples.models import ExampleTable
66

77

8-
async def handler_with_db_session_and_manual_rollback() -> None:
8+
async def early_rollback() -> None:
99
"""
1010
An example of a handle that uses a rollback
1111
"""
@@ -21,7 +21,5 @@ async def handler_with_db_session_and_manual_rollback() -> None:
2121

2222
async def _insert() -> None:
2323
session = await db_session(connection)
24-
stmt = insert(ExampleTable).values(
25-
text="example_with_db_session_and_manual_close"
26-
)
24+
stmt = insert(ExampleTable)
2725
await session.execute(stmt)

examples/fastapi_example/routes/simple_usage.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
from context_async_sqlalchemy import db_session
44

5-
from ..database import connection
6-
from ..models import ExampleTable
5+
from examples.database import connection
6+
from examples.models import ExampleTable
77

88

9-
async def handler_with_db_session() -> None:
9+
async def simple_usage() -> None:
1010
"""
1111
An example of a typical handle that uses a context session to work with
1212
a database.
@@ -18,7 +18,7 @@ async def handler_with_db_session() -> None:
1818
# even in child coroutines.
1919
session = await db_session(connection)
2020

21-
stmt = insert(ExampleTable).values(text="example_with_db_session")
21+
stmt = insert(ExampleTable)
2222

2323
# On the first request, a connection and transaction were opened
2424
await session.execute(stmt)

0 commit comments

Comments
 (0)