API Reference¶
The API reference is automatically generated from the docstrings in the code, and is useful for finding out what methods and attributes are available for a given class.
Note
Private methods and attributes are not included in the API reference.
Overview¶
SQLSpec is organized into several core modules, each serving a specific purpose:
Core Components:
Base - Main
SQLSpecregistry and configuration managementCore - SQL processing (statements, parameters, results, compilation)
Driver - Base driver classes and mixins for database operations
Database Connectivity:
Adapters - Database-specific adapters (PostgreSQL, SQLite, DuckDB, etc.)
Query Building:
Builder - Fluent SQL builder API for programmatic query construction
Framework Integration:
Extensions - Web framework integrations (Litestar, FastAPI, Flask, etc.)
Module Organization¶
SQLSpec follows a layered architecture:
User-Facing API (
sqlspec.base,sqlspec.builder)SQLSpec- Main registry classSQL Builder - Fluent query construction
Adapter Layer (
sqlspec.adapters.*)Database-specific configurations
Driver implementations
Parameter style handling
Core Processing (
sqlspec.core)SQL statement parsing and validation
Parameter binding and conversion
Result set processing
Statement caching
Driver Foundation (
sqlspec.driver)Base sync/async drivers
Transaction management
Query execution mixins
Framework Integration (
sqlspec.extensions.*)Dependency injection
Lifecycle management
Framework-specific utilities
Available API References¶
Common Workflows¶
Setting Up a Database:
Import adapter config:
from sqlspec.adapters.asyncpg import AsyncpgConfigCreate SQLSpec instance:
sql = SQLSpec()Add configuration:
db = sql.add_config(AsyncpgConfig(...))Get session:
async with sql.provide_session(db) as session:
Building Queries:
Import builder:
from sqlspec import sqlChain methods:
query = sql.select("*").from_("users").where("active = true")Convert to SQL:
stmt = query.to_statement()Execute:
result = await session.execute(stmt)
Processing Results:
Execute query:
result = await session.execute(sql)Get all rows:
result.all()(list of dicts)Get one row:
result.one()(raises if not exactly one)Get first row:
result.get_first()(returns first or None)Map to models:
result.all(schema_type=User)orresult.one(schema_type=User)
See Also¶
Getting Started - Getting started guide
Usage - Usage documentation
SQLSpec Example Library - Code examples
Contributing - Contributing guide
Type Hints and Protocols¶
SQLSpec makes extensive use of type hints and protocols for type safety:
sqlspec.protocols- Protocol definitions for runtime type checkingsqlspec.typing- Type aliases and generic typessqlspec.utils.type_guards- Type guard functions
All public APIs are fully typed and compatible with mypy, pyright, and other type checkers.