Session Stores

SQLSpec provides session store backends for Litestar’s session middleware. Store user sessions in your database instead of cookies or external caches.

Why SQL-backed Sessions?

  • No external dependencies - use your existing database instead of Redis or Memcached.

  • Persistence - sessions survive server restarts.

  • Querying - inspect or clean up sessions with standard SQL.

Basic Setup

Pass a SQLSpec store to Litestar’s SessionMiddleware:

session store
from sqlspec.adapters.aiosqlite import AiosqliteConfig
from sqlspec.adapters.aiosqlite.litestar import AiosqliteStore

config = AiosqliteConfig(connection_config={"database": ":memory:"})
store = AiosqliteStore(config)

Available Stores

SQLSpec provides stores for async adapters:

  • AsyncpgStore - PostgreSQL via asyncpg

  • AiosqliteStore - SQLite via aiosqlite

Each store automatically creates its session table on first use if it doesn’t exist.

Session Expiry

Configure session lifetime through Litestar’s SessionMiddleware settings. Expired sessions are cleaned up automatically based on the max_age parameter.