Starlette + AioSQLite Application

Expose SQLSpec data through Starlette routes backed by AioSQLite.

uv run python docs/examples/frameworks/starlette/aiosqlite_app.py

Source

 1"""Starlette routes serving SQLSpec-backed AioSQLite data."""
 2
 3import asyncio
 4
 5from starlette.applications import Starlette
 6from starlette.requests import Request
 7from starlette.responses import JSONResponse
 8from starlette.routing import Route
 9
10from docs.examples.shared.configs import aiosqlite_registry
11from docs.examples.shared.data import ARTICLES, CREATE_ARTICLES
12from sqlspec.adapters.aiosqlite import AiosqliteConfig
13from sqlspec.core import SQL
14
15__all__ = ("list_articles", "main", "seed_database")
16
17
18registry = aiosqlite_registry()
19config = registry.get_config(AiosqliteConfig)
20
21
22async def seed_database() -> None:
23    """Create tables and seed rows for the Starlette example."""
24    async with config.provide_session() as session:
25        await session.execute(CREATE_ARTICLES)
26        for row in ARTICLES:
27            await session.execute(
28                SQL(
29                    """
30                    INSERT OR REPLACE INTO articles (id, title, body)
31                    VALUES (:id, :title, :body)
32                    """
33                ),
34                row,
35            )
36
37
38async def list_articles(_request: "Request") -> "JSONResponse":
39    """Return JSON payload describing the article dataset."""
40    async with config.provide_session() as session:
41        result = await session.execute(SQL("SELECT id, title, body FROM articles ORDER BY id"))
42        return JSONResponse(result.all())
43
44
45routes = [Route("/articles", list_articles)]
46app = Starlette(debug=True, routes=routes, on_startup=[seed_database])
47
48
49def main() -> None:
50    """Seed the database outside of ASGI startup."""
51    asyncio.run(seed_database())
52
53
54if __name__ == "__main__":
55    main()