-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature/sqlalchemy request scoped session (#53)
* feat: add scoped session
- Loading branch information
1 parent
49e306b
commit c61cac8
Showing
13 changed files
with
241 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
from contextvars import ContextVar | ||
|
||
from sqlalchemy.ext.asyncio import async_sessionmaker | ||
|
||
from selva.configuration.settings import Settings | ||
from selva.di.container import Container | ||
|
||
SESSION = ContextVar("sqlalchemy session") | ||
|
||
|
||
async def scoped_session(app, _settings: Settings, container: Container): | ||
sessionmaker = await container.get(async_sessionmaker) | ||
|
||
async def middleware(scope, receive, send): | ||
async with sessionmaker() as session: | ||
token = SESSION.set(session) | ||
try: | ||
await app(scope, receive, send) | ||
except: | ||
await session.rollback() | ||
raise | ||
finally: | ||
SESSION.reset(token) | ||
|
||
return middleware |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from typing import Annotated | ||
|
||
from asgikit.responses import respond_text | ||
from sqlalchemy import text | ||
|
||
from selva.di import Inject, service | ||
from selva.ext.data.sqlalchemy import ScopedSession | ||
from selva.web import get | ||
|
||
|
||
@service | ||
class MyService: | ||
session: Annotated[ScopedSession, Inject] | ||
|
||
async def select(self): | ||
return await self.session.scalar(text("select 1")) | ||
|
||
|
||
@get | ||
async def index(request, my_service: Annotated[MyService, Inject]): | ||
value = await my_service.select() | ||
await respond_text(request.response, str(value)) |
9 changes: 9 additions & 0 deletions
9
tests/ext/data/sqlalchemy/application_scoped_session_startup.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from sqlalchemy import text | ||
|
||
from selva.ext.data.sqlalchemy import ScopedSession | ||
from selva.web import startup | ||
|
||
|
||
@startup | ||
async def startup(session: ScopedSession): | ||
await session.scalar(text("select 1")) |
Oops, something went wrong.