GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • FasterApiWeb/FasterAPI
  • Home
  • Getting Started
  • Tutorial
  • Advanced User Guide
  • Security
  • Databases
  • Deployment
  • Concepts
  • How-To Recipes
  • Reference
  • FAQ & Troubleshooting
  • Path Parameters
  • Query Parameters

This view

  • Original page
  • New address

GeezerVison is showing fasterapiweb.github.io — Overview - FasterAPI

Database Integration¶

FasterAPI is database-agnostic — use any Python library that works with async I/O. This section covers the most common choices.

Pages¶

Topic What you learn
SQL with SQLAlchemy Async SQLAlchemy 2, sessions, models, migrations
NoSQL — MongoDB Motor (async MongoDB), documents, indices
Async Database Usage Pattern guide: sessions, pools, connection lifecycle

Choosing a database library¶

Use case Recommended
PostgreSQL / MySQL / SQLite SQLAlchemy 2 (async)
PostgreSQL (lightweight) asyncpg directly
MongoDB Motor
Redis (cache/queue) redis-py (async)
Key-value / embedded aiosqlite for SQLite

General pattern¶

  1. Open a pool / session factory at startup (see Lifespan Events).
  2. Inject a session per request using Depends().
  3. Close the session after the response (use try/finally in the dependency).
  4. Close the pool at shutdown.
# Generic pattern sketch
@app.on_startup
async def startup():
    app_state.pool = await create_pool(DATABASE_URL)

@app.on_shutdown
async def shutdown():
    await app_state.pool.close()

async def get_session():
    async with app_state.pool.acquire() as conn:
        yield conn

@app.get("/items")
async def list_items(db = Depends(get_session)):
    return await db.fetch("SELECT * FROM items")

Environment variables¶

Store the connection string in DATABASE_URL:

# PostgreSQL
DATABASE_URL=postgresql+asyncpg://user:pass@localhost/mydb

# SQLite (development)
DATABASE_URL=sqlite+aiosqlite:///./dev.db

# MongoDB
MONGODB_URL=mongodb://localhost:27017

Next steps¶

  • Settings & Environment Variables — managing DATABASE_URL.
  • Lifespan Events — startup / shutdown hooks for pools.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.