Skip to content

Commit d59a52e

Browse files
authored
docs: added information about accessing sessions (#325)
1 parent cf1caab commit d59a52e

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed

docs/usage/frameworks/litestar.rst

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ Finally, configure your Litestar application with the plugin and dependencies:
224224
.. code-block:: python
225225
226226
from litestar import Litestar
227+
from litestar.di import Provide
227228
from litestar.plugins.sqlalchemy.filters import FilterTypes, LimitOffset
228229
from litestar.plugins.sqlalchemy import (
229230
AsyncSessionConfig,
@@ -259,6 +260,81 @@ Finally, configure your Litestar application with the plugin and dependencies:
259260
dependencies={"limit_offset": Provide(provide_limit_offset_pagination, sync_to_thread=False)},
260261
)
261262
263+
Database Sessions
264+
-----------------
265+
266+
Sessions in Controllers
267+
^^^^^^^^^^^^^^^^^^^^^^^
268+
269+
You can access the database session from the controller by using the `db_session` parameter, which is automatically injected by the SQLAlchemy plugin. The session is automatically committed at the end of the request. If an exception occurs, the session is rolled back:
270+
271+
.. code-block:: python
272+
273+
from litestar import Litestar, get
274+
from advanced_alchemy.extensions.litestar import (
275+
AsyncSessionConfig,
276+
SQLAlchemyAsyncConfig,
277+
SQLAlchemyPlugin,
278+
)
279+
280+
session_config = AsyncSessionConfig(expire_on_commit=False)
281+
sqlalchemy_config = SQLAlchemyAsyncConfig(
282+
connection_string="sqlite+aiosqlite:///test.sqlite",
283+
before_send_handler="autocommit",
284+
session_config=session_config,
285+
create_all=True,
286+
) # Create 'db_session' dependency.
287+
alchemy = SQLAlchemyPlugin(config=sqlalchemy_config)
288+
289+
@get("/my-endpoint")
290+
async def my_controller(db_session: AsyncSession) -> str:
291+
# Access the database session here.
292+
return "Hello, World!"
293+
294+
app = Litestar(
295+
route_handlers=[my_controller],
296+
plugins=[alchemy],
297+
)
298+
299+
Sessions in Middleware
300+
^^^^^^^^^^^^^^^^^^^^^^
301+
302+
Dependency injection is not available in middleware. Instead, you can create a new session using the `provide_session` method:
303+
304+
.. code-block:: python
305+
306+
from litestar import Litestar
307+
from litestar.types import ASGIApp, Scope, Receive, Send
308+
from advanced_alchemy.extensions.litestar import (
309+
AsyncSessionConfig,
310+
SQLAlchemyAsyncConfig,
311+
SQLAlchemyPlugin,
312+
)
313+
314+
session_config = AsyncSessionConfig(expire_on_commit=False)
315+
sqlalchemy_config = SQLAlchemyAsyncConfig(
316+
connection_string="sqlite+aiosqlite:///test.sqlite",
317+
before_send_handler="autocommit",
318+
session_config=session_config,
319+
create_all=True,
320+
)
321+
alchemy = SQLAlchemyPlugin(config=sqlalchemy_config)
322+
323+
def middleware_factory(app: ASGIApp) -> ASGIApp:
324+
async def my_middleware(scope: Scope, receive: Receive, send: Send) -> None:
325+
# NOTE: You can also access the app state from `ASGIConnection`.
326+
db_session = await alchemy.provide_session(scope["app"].state, scope)
327+
# Access the database session here.
328+
await db_session.close()
329+
...
330+
await app(scope, receive, send)
331+
return my_middleware
332+
333+
app = Litestar(
334+
route_handlers=[...],
335+
middleware=[middleware_factory],
336+
plugins=[alchemy]
337+
)
262338
263339
Database Migrations
264340
-------------------

0 commit comments

Comments
 (0)