FastAPI + AioSQLite Application

Serve SQLSpec-backed content through FastAPI with the AioSQLite adapter.

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

Source

 1"""FastAPI application backed by SQLSpec and AioSQLite."""
 2
 3import asyncio
 4from typing import Annotated, Any
 5
 6from fastapi import Depends, FastAPI
 7
 8from docs.examples.shared.configs import aiosqlite_registry
 9from docs.examples.shared.data import ARTICLES, CREATE_ARTICLES
10from sqlspec.adapters.aiosqlite import AiosqliteConfig, AiosqliteDriver
11from sqlspec.core import SQL
12
13__all__ = ("get_session", "list_articles", "main", "on_startup", "seed_database")
14
15
16registry = aiosqlite_registry()
17config = registry.get_config(AiosqliteConfig)
18app = FastAPI()
19
20
21async def seed_database() -> None:
22    """Create the demo schema and seed rows."""
23    async with config.provide_session() as session:
24        await session.execute(CREATE_ARTICLES)
25        for row in ARTICLES:
26            await session.execute(
27                SQL(
28                    """
29                    INSERT OR REPLACE INTO articles (id, title, body)
30                    VALUES (:id, :title, :body)
31                    """
32                ),
33                row,
34            )
35
36
37@app.on_event("startup")
38async def on_startup() -> None:
39    """Seed data when the FastAPI app starts."""
40    await seed_database()
41
42
43async def get_session() -> "AiosqliteDriver":
44    """Yield an AioSQLite session for request handlers."""
45    async with config.provide_session() as session:
46        yield session
47
48
49@app.get("/articles")
50async def list_articles(db_session: Annotated["AiosqliteDriver", Depends(get_session)]) -> "list[dict[str, Any]]":
51    """Return all demo articles sorted by ID."""
52    result = await db_session.execute(SQL("SELECT id, title, body FROM articles ORDER BY id"))
53    return result.all()
54
55
56def main() -> None:
57    """Seed the database without running the ASGI server."""
58    asyncio.run(seed_database())
59
60
61if __name__ == "__main__":
62    main()