-
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adds a
get_session
context manager to the config (#83)
Generally most apps end up creating some sort of context manager to help with sessions. While the Litestar plugin handles this for us, I still find the need to get to a context manager outside of requests. See here for an (example)[https://github.com/litestar-org/litestar-fullstack/blob/main/src/app/lib/db/base.py#L103]. This PR enables quick access to a `Session`, `AsyncSession` context manager from the SQL Alchemy configuration.
- Loading branch information
Showing
4 changed files
with
85 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
from __future__ import annotations | ||
|
||
from typing import cast | ||
|
||
from sqlalchemy.ext.asyncio import AsyncSession | ||
from sqlalchemy.orm import Session | ||
|
||
from advanced_alchemy.extensions.litestar.plugins import SQLAlchemyPlugin | ||
from advanced_alchemy.extensions.litestar.plugins.init.config.asyncio import SQLAlchemyAsyncConfig | ||
from advanced_alchemy.extensions.litestar.plugins.init.config.sync import SQLAlchemySyncConfig | ||
|
||
|
||
async def test_sync_db_session(sync_sqlalchemy_plugin: SQLAlchemyPlugin) -> None: | ||
config = cast("SQLAlchemySyncConfig", sync_sqlalchemy_plugin._config) | ||
|
||
with config.get_session() as session: | ||
assert isinstance(session, Session) | ||
|
||
|
||
async def test_async_db_session(async_sqlalchemy_plugin: SQLAlchemyPlugin) -> None: | ||
config = cast("SQLAlchemyAsyncConfig", async_sqlalchemy_plugin._config) | ||
|
||
async with config.get_session() as session: | ||
assert isinstance(session, AsyncSession) |