1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199
|
====================
Litestar Integration
====================
.. seealso::
:external+litestar:doc:`Litestar's documentation for SQLAlchemy integration <usage/databases/sqlalchemy/index>`
Advanced Alchemy provides first-class integration with Litestar through its SQLAlchemy plugin, which re-exports many of the modules within Advanced Alchemy.
This guide demonstrates building a complete CRUD API for a book management system.
Key Features
------------
- SQLAlchemy plugin for session and transaction management
- Repository pattern for database operations
- Service layer for business logic and data transformation
- Built-in pagination and filtering
- CLI tools for database migrations
Basic Setup
-----------
First, configure the SQLAlchemy plugin with Litestar. The plugin handles database connection, session management, and dependency injection:
.. code-block:: python
from litestar import Litestar
from advanced_alchemy.extensions.litestar import (
AsyncSessionConfig,
SQLAlchemyAsyncConfig,
SQLAlchemyPlugin,
)
session_config = AsyncSessionConfig(expire_on_commit=False)
alchemy_config = SQLAlchemyAsyncConfig(
connection_string="sqlite+aiosqlite:///test.sqlite",
before_send_handler="autocommit",
session_config=session_config,
create_all=True,
)
alchemy = SQLAlchemyPlugin(config=alchemy_config)
SQLAlchemy Models
-----------------
Define your SQLAlchemy models using Advanced Alchemy's enhanced base classes:
.. code-block:: python
import datetime
from typing import Optional
from uuid import UUID
from sqlalchemy import ForeignKey
from sqlalchemy.orm import Mapped, mapped_column, relationship
from advanced_alchemy.extensions.litestar import base
class AuthorModel(base.UUIDBase):
__tablename__ = "author"
name: Mapped[str]
dob: Mapped[Optional[datetime.date]]
books: Mapped[list[BookModel]] = relationship(back_populates="author", lazy="selectin")
class BookModel(base.UUIDAuditBase):
__tablename__ = "book"
title: Mapped[str]
author_id: Mapped[UUID] = mapped_column(ForeignKey("author.id"))
author: Mapped[AuthorModel] = relationship(lazy="joined", innerjoin=True, viewonly=True)
Using Properties with DTOs
---------------------------
SQLAlchemyDTO includes Python ``@property`` and ``@functools.cached_property`` decorated methods as read-only fields.
.. code-block:: python
from functools import cached_property
from sqlalchemy.orm import Mapped, mapped_column, MappedAsDataclass
from advanced_alchemy.extensions.litestar import base, SQLAlchemyDTO
class UserModel(base.UUIDAuditBase, MappedAsDataclass):
__tablename__ = "user"
first_name: Mapped[str]
last_name: Mapped[str]
@property
def full_name(self) -> str:
return f"{self.first_name} {self.last_name}"
@cached_property
def name_length(self) -> int:
return len(self.full_name)
# DTO includes: id, created_at, updated_at, first_name, last_name,
# full_name (read-only), name_length (read-only)
UserDTO = SQLAlchemyDTO[UserModel]
Property handling characteristics:
- Detected from model class and mixins
- Marked as ``READ_ONLY`` (cannot be set via DTO)
- Type inferred from return type annotations
- Private properties (starting with ``_``) excluded
- Skipped if already handled by SQLAlchemy descriptors (e.g., ``hybrid_property``)
.. note::
Properties with setters (``@property.setter``) are marked ``READ_ONLY``. Setter support is not implemented.
Pydantic Schemas
----------------
Define Pydantic schemas for input validation and response serialization:
.. code-block:: python
import datetime
from pydantic import BaseModel
from uuid import UUID
from typing import Optional
class Author(BaseModel):
"""Author response schema."""
id: Optional[UUID] = None
name: str
dob: Optional[datetime.date] = None
class AuthorCreate(BaseModel):
"""Schema for creating authors."""
name: str
dob: Optional[datetime.date] = None
class AuthorUpdate(BaseModel):
"""Schema for updating authors."""
name: Optional[str] = None
dob: Optional[datetime.date] = None
Repository and Service Layer
----------------------------
Create repository and service classes to interact with the model:
.. code-block:: python
from advanced_alchemy.extensions.litestar import repository, service
class AuthorService(service.SQLAlchemyAsyncRepositoryService[AuthorModel]):
"""Author service."""
class Repo(repository.SQLAlchemyAsyncRepository[AuthorModel]):
"""Author repository."""
model_type = AuthorModel
repository_type = Repo
Controllers
-----------
Create a controller class to handle HTTP endpoints. The controller uses dependency injection for services and includes built-in pagination:
.. code-block:: python
from typing import Annotated
from litestar import Controller, get, post, patch, delete
from litestar.params import Dependency, Parameter
from advanced_alchemy.extensions.litestar import filters, providers, service
class AuthorController(Controller):
"""Author CRUD endpoints."""
dependencies = providers.create_service_dependencies(
AuthorService,
"authors_service",
load=[AuthorModel.books],
filters={"pagination_type": "limit_offset", "id_filter": UUID, "search": "name", "search_ignore_case": True},
)
@get(path="/authors")
async def list_authors(
self,
authors_service: AuthorService,
filters: Annotated[list[filters.FilterTypes], Dependency(skip_validation=True)],
) -> service.OffsetPagination[Author]:
"""List all authors with pagination."""
results, total = await authors_service.list_and_count(*filters)
return authors_service.to_schema(results, total, filters=filters, schema_type=Author)
@post(path="/authors")
async def create_author(
self,
authors_service: AuthorService,
data: AuthorCreate,
) -> Author:
"""Create a new author."""
obj = await authors_service.create(data)
return authors_service.to_schema(obj, schema_type=Author)
@get(path="/authors/{author_id:uuid}")
async def get_author(
self,
authors_service: AuthorService,
author_id: UUID = Parameter(
title="Author ID",
description="The author to retrieve.",
),
) -> Author:
"""Get an existing author."""
obj = await authors_service.get(author_id)
return authors_service.to_schema(obj, schema_type=Author)
@patch(path="/authors/{author_id:uuid}")
async def update_author(
self,
authors_service: AuthorService,
data: AuthorUpdate,
author_id: UUID = Parameter(
title="Author ID",
description="The author to update.",
),
) -> Author:
"""Update an author."""
obj = await authors_service.update(data, item_id=author_id, auto_commit=True)
return authors_service.to_schema(obj, schema_type=Author)
@delete(path="/authors/{author_id:uuid}")
async def delete_author(
self,
authors_service: AuthorService,
author_id: UUID = Parameter(
title="Author ID",
description="The author to delete.",
),
) -> None:
"""Delete an author from the system."""
_ = await authors_service.delete(author_id)
Application Configuration
-------------------------
Finally, configure your Litestar application with the plugin and dependencies:
.. code-block:: python
from litestar import Litestar
from advanced_alchemy.extensions.litestar import (
AsyncSessionConfig,
SQLAlchemyAsyncConfig,
SQLAlchemyPlugin,
)
alchemy_config = SQLAlchemyAsyncConfig(
connection_string="sqlite+aiosqlite:///test.sqlite",
before_send_handler="autocommit",
session_config=AsyncSessionConfig(expire_on_commit=False),
create_all=True,
)
app = Litestar(
route_handlers=[AuthorController],
plugins=[SQLAlchemyPlugin(config=alchemy_config)],
)
Database Sessions
-----------------
Sessions in Controllers
^^^^^^^^^^^^^^^^^^^^^^^
You can access the database session from the controller by using the 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:
By default, the session key is named "db_session". You can change this by setting the `session_dependency_key` parameter in the SQLAlchemyAsyncConfig.
.. code-block:: python
from litestar import Litestar, get
from litestar.plugins.sqlalchemy import (
AsyncSessionConfig,
SQLAlchemyAsyncConfig,
SQLAlchemyPlugin,
)
session_config = AsyncSessionConfig(expire_on_commit=False)
alchemy_config = SQLAlchemyAsyncConfig(
connection_string="sqlite+aiosqlite:///test.sqlite",
before_send_handler="autocommit",
session_config=session_config,
create_all=True,
) # Auto creates 'db_session' dependency.
@get("/my-endpoint")
async def my_controller(db_session: AsyncSession) -> str:
# Access the database session here.
return "Hello, World!"
app = Litestar(
route_handlers=[my_controller],
plugins=[SQLAlchemyPlugin(config=alchemy_config)],
)
Sessions in Application
^^^^^^^^^^^^^^^^^^^^^^^
You can use either ``provide_session`` or ``get_session`` to get session instances in your application. Each of these functions are useful for providing sessions in various places within your application, whether you are in the request/response scope or not.
``provide_session`` provides a session instance from request state if it exists, or creates a new session if it doesn't, while ``get_session`` always returns a new instance from the session maker.
- ``provide_session`` is useful in places where you are already in the request/response context such as guards and middleware.
.. code-block:: python
from litestar import Litestar, get
from litestar.connection import ASGIConnection
from litestar.handlers.base import BaseRouteHandler
from litestar.plugins.sqlalchemy import (
AsyncSessionConfig,
SQLAlchemyAsyncConfig,
SQLAlchemyPlugin,
)
from sqlalchemy import text
session_config = AsyncSessionConfig(expire_on_commit=False)
alchemy_config = SQLAlchemyAsyncConfig(
connection_string="sqlite+aiosqlite:///test.sqlite",
before_send_handler="autocommit",
session_config=session_config,
create_all=True,
)
alchemy = SQLAlchemyPlugin(config=alchemy_config)
async def my_guard(connection: ASGIConnection[Any, Any, Any, Any], _: BaseRouteHandler) -> None:
db_session = alchemy_config.provide_session(connection.app.state, connection.scope)
a_value = await db_session.execute(text("SELECT 1"))
@get("/", guards=[my_guard])
async def hello() -> str:
return "Hello, world!"
app = Litestar(
route_handlers=[hello],
plugins=[alchemy],
)
- ``get_session`` is useful anywhere outside of the request lifecycle in your application. This includes command line tasks and background jobs.
.. code-block:: python
from click import Group
from litestar import Litestar, get
from litestar.plugins import CLIPluginProtocol, InitPluginProtocol
from litestar.plugins.sqlalchemy import (
AsyncSessionConfig,
SQLAlchemyAsyncConfig,
SQLAlchemyPlugin,
)
class ApplicationCore(CLIPluginProtocol):
def on_cli_init(self, cli: Group) -> None:
@cli.command('check-db-status')
def check_db_status() -> None:
import anyio
async def _check_db_status() -> None:
async with alchemy_config.get_session() as db_session:
a_value = await db_session.execute(text("SELECT 1"))
if a_value.scalar_one() == 1:
print("Database is healthy")
else:
print("Database is not healthy")
anyio.run(_check_db_status)
alchemy_config = SQLAlchemyAsyncConfig(
connection_string="sqlite+aiosqlite:///test.sqlite",
before_send_handler="autocommit",
session_config=AsyncSessionConfig(expire_on_commit=False),
create_all=True,
)
alchemy = SQLAlchemyPlugin(config=alchemy_config)
app = Litestar(plugins=[alchemy, ApplicationCore()])
Database Migrations
-------------------
Advanced Alchemy integrates with Litestar's CLI to provide database migration tools powered by Alembic. All alembic commands are integrated directly into the Litestar CLI.
Command List
^^^^^^^^^^^^
To get a listing of available commands, run the following:
.. code-block:: bash
litestar database
.. code-block:: bash
Usage: app database [OPTIONS] COMMAND [ARGS]...
Manage SQLAlchemy database components.
â•─ Options ────────────────────────────────────────────────────────────────────╮
│ --help -h Show this message and exit. │
╰──────────────────────────────────────────────────────────────────────────────╯
â•─ Commands ───────────────────────────────────────────────────────────────────╮
│ downgrade Downgrade database to a specific revision. │
│ drop-all Drop all tables from the database. │
│ dump-data Dump specified tables from the database to JSON │
│ files. │
│ init Initialize migrations for the project. │
│ make-migrations Create a new migration revision. │
│ merge-migrations Merge multiple revisions into a single new revision. │
│ show-current-revision Shows the current revision for the database. │
│ stamp-migration Mark (Stamp) a specific revision as current without │
│ applying the migrations. │
│ upgrade Upgrade database to a specific revision. │
╰──────────────────────────────────────────────────────────────────────────────╯
Initializing a new project
^^^^^^^^^^^^^^^^^^^^^^^^^^
If you would like to initial set of alembic migrations, you can easily scaffold out new templates to setup a project.
Assuming that you are using the default configuration for the SQLAlchemy configuration, you can run the following to initialize the migrations directory.
.. code-block:: shell-session
$ litestar database init ./migrations
If you use a different path than `./migrations`, be sure to also set this in your SQLAlchemy config. For instance, if you'd like to use `./alembic`:
.. code-block:: python
config = SQLAlchemyAsyncConfig(
alembic_config=AlembicAsyncConfig(
script_location="./alembic/",
),
)
And then run the following to initialize the migrations directory:
.. code-block:: shell-session
$ litestar database init ./alembic
You will now be configured to use the alternate directory for migrations.
Generate New Migrations
^^^^^^^^^^^^^^^^^^^^^^^
Once configured, you can run the following command to auto-generate new alembic migrations:
.. code-block:: shell-session
$ litestar database make-migrations
Upgrading a Database
^^^^^^^^^^^^^^^^^^^^
You can upgrade a database to the latest version by running the following command:
.. code-block:: shell-session
$ litestar database upgrade
Session Middleware
------------------
Advanced Alchemy provides SQLAlchemy-based session backends for Litestar's server-side session middleware. This allows you to store session data in your existing SQLAlchemy database instead of using external stores like Redis or file-based storage.
Overview
^^^^^^^^
The SQLAlchemy session backend provides:
- **Database persistence**: Session data is stored in your SQLAlchemy database
- **Automatic expiration**: Built-in session expiration handling
- **Both sync and async support**: Works with both sync and async SQLAlchemy configurations
- **UUID-based sessions**: Uses UUIDv7 for session identifiers
- **Timezone-aware timestamps**: Proper handling of session expiration times
Quick Setup
^^^^^^^^^^^
To use the SQLAlchemy session backend, you need to:
1. Create a session model using the provided mixin
2. Configure the SQLAlchemy session backend
3. Register the session middleware with your Litestar application
.. code-block:: python
from litestar import Litestar
from litestar.middleware.session.server_side import ServerSideSessionConfig
from litestar.plugins.sqlalchemy import SQLAlchemyAsyncConfig, SQLAlchemyPlugin
from advanced_alchemy.extensions.litestar.session import (
SessionModelMixin,
SQLAlchemyAsyncSessionBackend,
)
# 1. Create your session model
class UserSession(SessionModelMixin):
__tablename__ = "user_sessions"
# 2. Configure SQLAlchemy
alchemy_config = SQLAlchemyAsyncConfig(
connection_string="postgresql+asyncpg://user:password@localhost/mydb",
create_all=True,
)
# 3. Configure session backend
session_config = ServerSideSessionConfig(
max_age=3600, # 1 hour
)
# 4. Create the session backend
session_backend = SQLAlchemyAsyncSessionBackend(
config=session_config,
alchemy_config=alchemy_config,
model=UserSession,
)
# 5. Create your Litestar app
app = Litestar(
route_handlers=[],
plugins=[SQLAlchemyPlugin(config=alchemy_config)],
middleware=[session_config.middleware],
)
Session Model Configuration
^^^^^^^^^^^^^^^^^^^^^^^^^^^
The session model must inherit from ``SessionModelMixin``, which provides the required fields and database constraints:
.. code-block:: python
from advanced_alchemy.extensions.litestar.session import SessionModelMixin
class UserSession(SessionModelMixin):
__tablename__ = "user_sessions"
# The mixin provides these fields automatically:
# - id: UUIDv7 primary key
# - session_id: String(255) session identifier
# - data: LargeBinary session data
# - expires_at: DateTime expiration timestamp
# - created_at, updated_at: Audit timestamps
The ``SessionModelMixin`` automatically creates:
- A unique constraint on ``session_id`` (or unique index for Spanner)
- An index on ``expires_at`` for efficient cleanup
- Hybrid properties for checking expiration status
Advanced Configuration
^^^^^^^^^^^^^^^^^^^^^^
**Custom Table Arguments**
You can customize table arguments while keeping the mixin's constraints:
.. code-block:: python
from sqlalchemy import Index
from advanced_alchemy.extensions.litestar.session import SessionModelMixin
class UserSession(SessionModelMixin):
__tablename__ = "user_sessions"
@declared_attr.directive
@classmethod
def __table_args__(cls):
# Get the mixin's default constraints
base_args = super().__table_args__()
# Add your custom indexes/constraints
return base_args + (
Index("ix_user_sessions_custom", cls.session_id, cls.created_at),
)
**Sync vs Async Configuration**
For synchronous SQLAlchemy configurations, use ``SQLAlchemySyncSessionBackend``:
.. code-block:: python
from litestar.plugins.sqlalchemy import SQLAlchemySyncConfig
from advanced_alchemy.extensions.litestar.session import SQLAlchemySyncSessionBackend
# Sync configuration
alchemy_config = SQLAlchemySyncConfig(
connection_string="postgresql://user:password@localhost/mydb",
create_all=True,
)
session_backend = SQLAlchemySyncSessionBackend(
config=session_config,
alchemy_config=alchemy_config,
model=UserSession,
)
**Session Cleanup**
Both session backends provide automatic cleanup of expired sessions:
.. code-block:: python
# Clean up expired sessions
await session_backend.delete_expired() # For async backend
# or
await session_backend.delete_expired() # For sync backend (wrapped with async_)
You can set up periodic cleanup using Litestar's task system or external schedulers.
Using Sessions in Routes
^^^^^^^^^^^^^^^^^^^^^^^^
Once configured, sessions work exactly like other Litestar session backends:
.. code-block:: python
from litestar import Litestar, get, post
from litestar.connection import ASGIConnection
from litestar.response import Response
@get("/login")
async def login_form() -> str:
return "<form method='post'><input name='username'><button>Login</button></form>"
@post("/login")
async def login(request: ASGIConnection) -> Response:
form = await request.form()
username = form.get("username")
# Set session data
request.set_session({"user_id": 123, "username": username})
return Response("Logged in!", status_code=200)
@get("/profile")
async def profile(request: ASGIConnection) -> dict:
# Access session data
user_id = request.session.get("user_id")
username = request.session.get("username")
if not user_id:
return {"error": "Not logged in"}
return {"user_id": user_id, "username": username}
@post("/logout")
async def logout(request: ASGIConnection) -> str:
# Clear session
request.clear_session()
return "Logged out!"
Database Schema
^^^^^^^^^^^^^^^
The session table created by ``SessionModelMixin`` has the following structure:
.. code-block:: sql
CREATE TABLE user_sessions (
id UUID PRIMARY KEY,
session_id VARCHAR(255) NOT NULL,
data BYTEA NOT NULL,
expires_at TIMESTAMP WITH TIME ZONE,
created_at TIMESTAMP WITH TIME ZONE NOT NULL,
updated_at TIMESTAMP WITH TIME ZONE NOT NULL,
CONSTRAINT uq_user_sessions_session_id UNIQUE (session_id)
);
CREATE INDEX ix_user_sessions_expires_at ON user_sessions (expires_at);
CREATE INDEX ix_user_sessions_session_id_unique ON user_sessions (session_id);
**Session ID Handling**
- Session IDs are limited to 255 characters and automatically truncated if longer
- UUIDv7 is used for the primary key, providing time-ordered identifiers
- Expired sessions are automatically filtered out during retrieval
Security Considerations
^^^^^^^^^^^^^^^^^^^^^^^
**Session Expiration**
Configure appropriate session timeouts:
.. code-block:: python
# Sessions are automatically renewed on each request
session_config = ServerSideSessionConfig(
max_age=1800, # 30 minutes
https_only=True, # Require HTTPS in production
samesite="strict", # CSRF protection
)
**Database Security**
Ensure your database connection uses proper security:
- Use encrypted connections (SSL/TLS)
- Restrict database user permissions
- Regular security updates
- Consider encrypting session data at rest
Performance Optimization
^^^^^^^^^^^^^^^^^^^^^^^^
**Indexing Strategy**
The mixin automatically creates optimal indexes, but you can add application-specific indexes:
.. code-block:: python
class UserSession(SessionModelMixin):
__tablename__ = "user_sessions"
# Add indexes for common query patterns
__table_args__ = SessionModelMixin.__table_args__ + (
Index("ix_user_sessions_created_user", "created_at", "session_id"),
)
**Connection Pooling**
Configure appropriate connection pooling for session workloads:
.. code-block:: python
from sqlalchemy.pool import QueuePool
alchemy_config = SQLAlchemyAsyncConfig(
connection_string="postgresql+asyncpg://user:password@localhost/mydb",
engine_config=EngineConfig(
poolclass=QueuePool,
pool_size=20,
max_overflow=30,
pool_pre_ping=True,
),
)
**Cleanup Strategy**
Implement regular cleanup of expired sessions:
.. code-block:: python
from litestar import Litestar
from litestar.events import BaseEventEmitter
async def cleanup_expired_sessions():
"""Background task to clean expired sessions."""
await session_backend.delete_expired()
# Schedule cleanup every hour
app = Litestar(
# ... your configuration
on_startup=[cleanup_expired_sessions],
)
Complete Example
^^^^^^^^^^^^^^^^
Here's a complete working example:
.. code-block:: python
from litestar import Litestar, get, post
from litestar.connection import ASGIConnection
from litestar.middleware.session.server_side import ServerSideSessionConfig
from litestar.plugins.sqlalchemy import (
AsyncSessionConfig,
SQLAlchemyAsyncConfig,
SQLAlchemyPlugin,
)
from litestar.response import Template
from advanced_alchemy.extensions.litestar.session import (
SessionModelMixin,
SQLAlchemyAsyncSessionBackend,
)
# Session model
class WebSession(SessionModelMixin):
__tablename__ = "web_sessions"
# Database configuration
alchemy_config = SQLAlchemyAsyncConfig(
connection_string="sqlite+aiosqlite:///sessions.db",
session_config=AsyncSessionConfig(expire_on_commit=False),
create_all=True,
)
# Session configuration
session_config = ServerSideSessionConfig(
max_age=3600, # 1 hour
)
# Session backend
session_backend = SQLAlchemyAsyncSessionBackend(
config=session_config,
alchemy_config=alchemy_config,
model=WebSession,
)
# Routes
@get("/")
async def home(request: ASGIConnection) -> dict:
username = request.session.get("username")
return {"message": f"Hello {username}!" if username else "Hello stranger!"}
@post("/login")
async def login(request: ASGIConnection) -> dict:
form = await request.form()
username = form.get("username")
if username:
request.set_session({"username": username, "login_time": "now"})
return {"message": f"Welcome {username}!"}
return {"error": "Username required"}
@post("/logout")
async def logout(request: ASGIConnection) -> dict:
request.clear_session()
return {"message": "Logged out successfully"}
# Application
app = Litestar(
route_handlers=[home, login, logout],
plugins=[SQLAlchemyPlugin(config=alchemy_config)],
middleware=[session_config.middleware],
)
This example provides a complete session-enabled application using SQLAlchemy for session storage.
File Object Storage
-------------------
Advanced Alchemy provides built-in support for file storage with various backends. Here's how to handle file uploads and storage:
.. code-block:: python
from typing import Annotated, Any, Optional, Union
from uuid import UUID
from litestar import Controller, Litestar, delete, get, patch, post
from litestar.datastructures import UploadFile
from litestar.enums import RequestEncodingType
from litestar.params import Body, Dependency
from pydantic import BaseModel, Field, computed_field
from sqlalchemy.orm import Mapped, mapped_column
from advanced_alchemy.extensions.litestar import (
AsyncSessionConfig,
SQLAlchemyAsyncConfig,
SQLAlchemyPlugin,
base,
filters,
providers,
repository,
service,
)
from advanced_alchemy.types import FileObject, storages
from advanced_alchemy.types.file_object.backends.obstore import ObstoreBackend
from advanced_alchemy.types.file_object.data_type import StoredObject
# Configure file storage backend
s3_backend = ObstoreBackend(
key="local",
fs="s3://static-files/",
aws_endpoint="http://localhost:9000",
aws_access_key_id="minioadmin",
aws_secret_access_key="minioadmin",
)
storages.register_backend(s3_backend)
# Model with file storage
class DocumentModel(base.UUIDBase):
__tablename__ = "document"
name: Mapped[str]
file: Mapped[FileObject] = mapped_column(StoredObject(backend="local"))
# Schema with file URL generation
class Document(BaseModel):
id: Optional[UUID] = None
name: str
file: Optional[FileObject] = Field(default=None, exclude=True)
@computed_field
def file_url(self) -> Optional[Union[str, list[str]]]:
if self.file is None:
return None
return self.file.sign()
# Schema for creating and updating documents
class CreateDocument(BaseModel):
model_config = {"arbitrary_types_allowed": True}
name: str
file: Optional[UploadFile] = None
class PatchDocument(BaseModel):
model_config = {"arbitrary_types_allowed": True}
name: Optional[str] = None
file: Optional[UploadFile] = None
# Service
class DocumentService(service.SQLAlchemyAsyncRepositoryService[DocumentModel]):
"""Document repository."""
class Repo(repository.SQLAlchemyAsyncRepository[DocumentModel]):
"""Document repository."""
model_type = DocumentModel
repository_type = Repo
# Controller with file handling
class DocumentController(Controller):
path = "/documents"
dependencies = providers.create_service_dependencies(
DocumentService,
"documents_service",
load=[DocumentModel.file],
filters={
"pagination_type": "limit_offset",
"id_filter": UUID,
"search": "name",
"search_ignore_case": True
},
)
@get(path="/", response_model=service.OffsetPagination[Document])
async def list_documents(
self,
documents_service: DocumentService,
filters: Annotated[list[filters.FilterTypes], Dependency(skip_validation=True)],
) -> service.OffsetPagination[Document]:
results, total = await documents_service.list_and_count(*filters)
return documents_service.to_schema(results, total, filters=filters, schema_type=Document)
@post(path="/")
async def create_document(
self,
data: Annotated[CreateDocument, Body(media_type=RequestEncodingType.MULTI_PART)],
documents_service: DocumentService,
) -> Document:
obj = await documents_service.create(
DocumentModel(
name=data.name,
file=FileObject(
backend="local",
filename=data.file.filename or "uploaded_file",
content_type=data.file.content_type,
content=await data.file.read(),
)
if data.file
else None,
)
)
return documents_service.to_schema(obj, schema_type=Document)
@get(path="/{document_id:uuid}")
async def get_document(
self,
documents_service: DocumentService,
document_id: UUID,
) -> Document:
obj = await documents_service.get(document_id)
return documents_service.to_schema(obj, schema_type=Document)
@patch(path="/{document_id:uuid}")
async def update_document(
self,
document_id: UUID,
data: Annotated[PatchDocument, Body(media_type=RequestEncodingType.MULTI_PART)],
documents_service: DocumentService,
) -> Document:
update_data: dict[str, Any] = {}
if data.name:
update_data["name"] = data.name
if data.file:
update_data["file"] = FileObject(
backend="local",
filename=data.file.filename or "uploaded_file",
content_type=data.file.content_type,
content=await data.file.read(),
)
obj = await documents_service.update(update_data, item_id=document_id)
return documents_service.to_schema(obj, schema_type=Document)
@delete(path="/{document_id:uuid}")
async def delete_document(
self,
documents_service: DocumentService,
document_id: UUID,
) -> None:
_ = await documents_service.delete(document_id)
# Application setup
alchemy_config = SQLAlchemyAsyncConfig(
connection_string="sqlite+aiosqlite:///test.sqlite",
session_config=AsyncSessionConfig(expire_on_commit=False),
before_send_handler="autocommit",
create_all=True,
)
app = Litestar(
route_handlers=[DocumentController],
plugins=[SQLAlchemyPlugin(config=alchemy_config)]
)
File storage features:
- **Multiple backends**: Local filesystem, S3, GCS, Azure and other object storage
- **Automatic URL signing**: Generate secure, time-limited URLs for file access
- **Content type detection**: Automatic MIME type handling
- **File validation**: Built-in validation for file types and sizes
- **Metadata storage**: Store file metadata alongside binary data
**Supported Storage Backends**:
- **Local filesystem**: For development and simple deployments
- **Cloud Storage Integration**: For production object storage
- **Memory**: For testing and temporary storage
- **Custom backends**: Implement your own storage backend
Alternative Patterns
--------------------
.. collapse:: Repository-Only Pattern
If for some reason you don't want to use the service layer abstraction, you can use repositories directly. This approach removes the services abstraction, but still offers the benefits of Advanced Alchemy's repository features:
.. code-block:: python
from __future__ import annotations
import datetime
from typing import TYPE_CHECKING, Optional
from uuid import UUID
from litestar import Controller, Litestar, delete, get, patch, post
from litestar.di import Provide
from litestar.pagination import OffsetPagination
from litestar.params import Parameter
from pydantic import BaseModel, TypeAdapter
from sqlalchemy import ForeignKey
from sqlalchemy.orm import Mapped, mapped_column, relationship
from advanced_alchemy.base import UUIDAuditBase, UUIDBase
from advanced_alchemy.config import AsyncSessionConfig
from advanced_alchemy.extensions.litestar.plugins import SQLAlchemyAsyncConfig, SQLAlchemyPlugin
from advanced_alchemy.filters import LimitOffset
from advanced_alchemy.repository import SQLAlchemyAsyncRepository
if TYPE_CHECKING:
from sqlalchemy.ext.asyncio import AsyncSession
class BaseModel(BaseModel):
"""Extend Pydantic's BaseModel to enable ORM mode"""
model_config = {"from_attributes": True}
# Models
class AuthorModel(UUIDBase):
__tablename__ = "author"
name: Mapped[str]
dob: Mapped[Optional[datetime.date]]
books: Mapped[list[BookModel]] = relationship(back_populates="author", lazy="noload")
# Repository
class AuthorRepository(SQLAlchemyAsyncRepository[AuthorModel]):
"""Author repository."""
model_type = AuthorModel
# Dependency providers
async def provide_authors_repo(db_session: AsyncSession) -> AuthorRepository:
"""This provides the default Authors repository."""
return AuthorRepository(session=db_session)
async def provide_author_details_repo(db_session: AsyncSession) -> AuthorRepository:
"""Repository with eager loading for author details."""
return AuthorRepository(load=[AuthorModel.books], session=db_session)
def provide_limit_offset_pagination(
current_page: int = Parameter(ge=1, query="currentPage", default=1, required=False),
page_size: int = Parameter(query="pageSize", ge=1, default=10, required=False),
) -> LimitOffset:
"""Add offset/limit pagination."""
return LimitOffset(page_size, page_size * (current_page - 1))
# Controller
class AuthorController(Controller):
"""Author CRUD using repository pattern."""
dependencies = {"authors_repo": Provide(provide_authors_repo)}
@get(path="/authors")
async def list_authors(
self,
authors_repo: AuthorRepository,
limit_offset: LimitOffset,
) -> OffsetPagination[Author]:
"""List authors with pagination."""
results, total = await authors_repo.list_and_count(limit_offset)
type_adapter = TypeAdapter(list[Author])
return OffsetPagination[Author](
items=type_adapter.validate_python(results),
total=total,
limit=limit_offset.limit,
offset=limit_offset.offset,
)
@post(path="/authors")
async def create_author(
self,
authors_repo: AuthorRepository,
data: AuthorCreate,
) -> Author:
"""Create a new author."""
obj = await authors_repo.add(
AuthorModel(**data.model_dump(exclude_unset=True, exclude_none=True)),
)
await authors_repo.session.commit()
return Author.model_validate(obj)
@get(
path="/authors/{author_id:uuid}",
dependencies={"authors_repo": Provide(provide_author_details_repo)}
)
async def get_author(
self,
authors_repo: AuthorRepository,
author_id: UUID = Parameter(title="Author ID", description="The author to retrieve."),
) -> Author:
"""Get an existing author with details."""
obj = await authors_repo.get(author_id)
return Author.model_validate(obj)
@patch(
path="/authors/{author_id:uuid}",
dependencies={"authors_repo": Provide(provide_author_details_repo)},
)
async def update_author(
self,
authors_repo: AuthorRepository,
data: AuthorUpdate,
author_id: UUID = Parameter(title="Author ID", description="The author to update."),
) -> Author:
"""Update an author."""
raw_obj = data.model_dump(exclude_unset=True, exclude_none=True)
raw_obj.update({"id": author_id})
obj = await authors_repo.update(AuthorModel(**raw_obj))
await authors_repo.session.commit()
return Author.model_validate(obj)
@delete(path="/authors/{author_id:uuid}")
async def delete_author(
self,
authors_repo: AuthorRepository,
author_id: UUID = Parameter(title="Author ID", description="The author to delete."),
) -> None:
"""Delete an author from the system."""
_ = await authors_repo.delete(author_id)
await authors_repo.session.commit()
# Application setup
session_config = AsyncSessionConfig(expire_on_commit=False)
alchemy_config = SQLAlchemyAsyncConfig(
connection_string="sqlite+aiosqlite:///test.sqlite",
session_config=session_config,
create_all=True,
)
sqlalchemy_plugin = SQLAlchemyPlugin(config=alchemy_config)
app = Litestar(
route_handlers=[AuthorController],
plugins=[sqlalchemy_plugin],
dependencies={"limit_offset": Provide(provide_limit_offset_pagination, sync_to_thread=False)},
)
This pattern is useful when you:
- Need direct control over database transactions
- Want to avoid the service layer abstraction
- Have complex repository logic that doesn't fit the service pattern
- Are building a smaller application with simpler data access patterns
|