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¶
- Open a pool / session factory at startup (see Lifespan Events).
- Inject a session per request using
Depends(). - Close the session after the response (use
try/finallyin the dependency). - 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.