Base¶
The sqlspec.base module defines the SQLSpec registry that owns configuration,
connection lifecycles, and session creation.
Example¶
sqlspec registry¶from sqlspec import SQLSpec
spec = SQLSpec()
spec.add_named_sql("health_check", "select 1 as ok")
query = spec.get_sql("health_check")
Core Responsibilities¶
Register database configurations.
Provide sync and async session context managers.
Manage connection pool startup and shutdown.
Track configs by bind key for multi-database setups.
Session Management¶
provide_sessionyields a session bound to a specific config.Sync and async sessions share the same registry API.
Close pools explicitly with
close_all_poolswhen needed.
API Reference¶
- class sqlspec.base.SQLSpec[source]¶
Bases:
objectConfiguration manager and registry for database connections and pools.
- async close_all_pools()[source]¶
Explicitly close all connection pools (async and sync).
This method should be called before application shutdown for proper cleanup.
- Return type:
- async __aexit__(_exc_type, _exc_val, _exc_tb)[source]¶
Async context manager exit with automatic cleanup.
- Return type:
- add_config(config)[source]¶
Add a configuration instance to the registry.
- Parameters:
config¶ (
Union[TypeVar(SyncConfigT, bound= SyncDatabaseConfig[Any, Any, Any] | NoPoolSyncConfig[Any, Any]),TypeVar(AsyncConfigT, bound= AsyncDatabaseConfig[Any, Any, Any] | NoPoolAsyncConfig[Any, Any])]) – The configuration instance to add.- Return type:
Union[TypeVar(SyncConfigT, bound= SyncDatabaseConfig[Any, Any, Any] | NoPoolSyncConfig[Any, Any]),TypeVar(AsyncConfigT, bound= AsyncDatabaseConfig[Any, Any, Any] | NoPoolAsyncConfig[Any, Any])]- Returns:
The same configuration instance (it IS the handle).
- property configs: dict[int, DatabaseConfigProtocol[TypeAliasForwardRef('typing.Any'), TypeAliasForwardRef('typing.Any'), TypeAliasForwardRef('typing.Any')]]¶
Access the registry of database configurations.
- Returns:
Dictionary mapping config instance IDs to config instances.
- event_channel(config)[source]¶
Create an event channel for the provided configuration.
Returns SyncEventChannel for sync configs, AsyncEventChannel for async configs.
- Parameters:
config¶ (
Union[type[Union[TypeVar(SyncConfigT, bound= SyncDatabaseConfig[Any, Any, Any] | NoPoolSyncConfig[Any, Any]),TypeVar(AsyncConfigT, bound= AsyncDatabaseConfig[Any, Any, Any] | NoPoolAsyncConfig[Any, Any])]],SyncDatabaseConfig[typing.Any, typing.Any, typing.Any],NoPoolSyncConfig[typing.Any, typing.Any],AsyncDatabaseConfig[typing.Any, typing.Any, typing.Any],NoPoolAsyncConfig[typing.Any, typing.Any]]) – A registered database configuration instance or type.- Return type:
SyncEventChannel|AsyncEventChannel- Returns:
The appropriate event channel type for the configuration.
- get_connection(config)[source]¶
Get a database connection for the specified configuration.
- Parameters:
config¶ (
Union[NoPoolSyncConfig[TypeVar(ConnectionT),TypeVar(DriverT, bound= SyncDriverAdapterBase | AsyncDriverAdapterBase)],SyncDatabaseConfig[TypeVar(ConnectionT),TypeVar(PoolT),TypeVar(DriverT, bound= SyncDriverAdapterBase | AsyncDriverAdapterBase)],NoPoolAsyncConfig[TypeVar(ConnectionT),TypeVar(DriverT, bound= SyncDriverAdapterBase | AsyncDriverAdapterBase)],AsyncDatabaseConfig[TypeVar(ConnectionT),TypeVar(PoolT),TypeVar(DriverT, bound= SyncDriverAdapterBase | AsyncDriverAdapterBase)]]) – The configuration instance.- Return type:
Union[TypeVar(ConnectionT),Awaitable[TypeVar(ConnectionT)]]- Returns:
A database connection or an awaitable yielding a connection.
- get_session(config)[source]¶
Get a database session (driver adapter) for the specified configuration.
- Parameters:
config¶ (
Union[NoPoolSyncConfig[TypeVar(ConnectionT),TypeVar(DriverT, bound= SyncDriverAdapterBase | AsyncDriverAdapterBase)],SyncDatabaseConfig[TypeVar(ConnectionT),TypeVar(PoolT),TypeVar(DriverT, bound= SyncDriverAdapterBase | AsyncDriverAdapterBase)],NoPoolAsyncConfig[TypeVar(ConnectionT),TypeVar(DriverT, bound= SyncDriverAdapterBase | AsyncDriverAdapterBase)],AsyncDatabaseConfig[TypeVar(ConnectionT),TypeVar(PoolT),TypeVar(DriverT, bound= SyncDriverAdapterBase | AsyncDriverAdapterBase)]]) – The configuration instance.- Return type:
Union[TypeVar(DriverT, bound= SyncDriverAdapterBase | AsyncDriverAdapterBase),Awaitable[TypeVar(DriverT, bound= SyncDriverAdapterBase | AsyncDriverAdapterBase)]]- Returns:
A driver adapter instance or an awaitable yielding one.
- provide_connection(config, *args, **kwargs)[source]¶
Create and provide a database connection from the specified configuration.
- Parameters:
config¶ (
Union[NoPoolSyncConfig[TypeVar(ConnectionT),TypeVar(DriverT, bound= SyncDriverAdapterBase | AsyncDriverAdapterBase)],SyncDatabaseConfig[TypeVar(ConnectionT),TypeVar(PoolT),TypeVar(DriverT, bound= SyncDriverAdapterBase | AsyncDriverAdapterBase)],NoPoolAsyncConfig[TypeVar(ConnectionT),TypeVar(DriverT, bound= SyncDriverAdapterBase | AsyncDriverAdapterBase)],AsyncDatabaseConfig[TypeVar(ConnectionT),TypeVar(PoolT),TypeVar(DriverT, bound= SyncDriverAdapterBase | AsyncDriverAdapterBase)]]) – The configuration instance.*args¶ (
Any) – Positional arguments to pass to the config’s provide_connection.**kwargs¶ (
Any) – Keyword arguments to pass to the config’s provide_connection.
- Return type:
AbstractContextManager[TypeVar(ConnectionT)] |AbstractAsyncContextManager[TypeVar(ConnectionT)]- Returns:
A sync or async context manager yielding a connection.
- provide_session(config, *args, **kwargs)[source]¶
Create and provide a database session from the specified configuration.
- Parameters:
config¶ (
Union[NoPoolSyncConfig[TypeVar(ConnectionT),TypeVar(DriverT, bound= SyncDriverAdapterBase | AsyncDriverAdapterBase)],SyncDatabaseConfig[TypeVar(ConnectionT),TypeVar(PoolT),TypeVar(DriverT, bound= SyncDriverAdapterBase | AsyncDriverAdapterBase)],NoPoolAsyncConfig[TypeVar(ConnectionT),TypeVar(DriverT, bound= SyncDriverAdapterBase | AsyncDriverAdapterBase)],AsyncDatabaseConfig[TypeVar(ConnectionT),TypeVar(PoolT),TypeVar(DriverT, bound= SyncDriverAdapterBase | AsyncDriverAdapterBase)]]) – The configuration instance.*args¶ (
Any) – Positional arguments to pass to the config’s provide_session.**kwargs¶ (
Any) – Keyword arguments to pass to the config’s provide_session.
- Return type:
AbstractContextManager[TypeVar(DriverT, bound= SyncDriverAdapterBase | AsyncDriverAdapterBase)] |AbstractAsyncContextManager[TypeVar(DriverT, bound= SyncDriverAdapterBase | AsyncDriverAdapterBase)]- Returns:
A sync or async context manager yielding a driver adapter instance.
- get_pool(config)[source]¶
Get the connection pool for the specified configuration.
- Parameters:
config¶ (
Union[NoPoolSyncConfig[TypeVar(ConnectionT),TypeVar(DriverT, bound= SyncDriverAdapterBase | AsyncDriverAdapterBase)],SyncDatabaseConfig[TypeVar(ConnectionT),TypeVar(PoolT),TypeVar(DriverT, bound= SyncDriverAdapterBase | AsyncDriverAdapterBase)],NoPoolAsyncConfig[TypeVar(ConnectionT),TypeVar(DriverT, bound= SyncDriverAdapterBase | AsyncDriverAdapterBase)],AsyncDatabaseConfig[TypeVar(ConnectionT),TypeVar(PoolT),TypeVar(DriverT, bound= SyncDriverAdapterBase | AsyncDriverAdapterBase)]]) – The configuration instance.- Return type:
type[TypeVar(PoolT)] |Awaitable[type[TypeVar(PoolT)]] |None- Returns:
The connection pool, an awaitable yielding the pool, or None if not supported.
- close_pool(config)[source]¶
Close the connection pool for the specified configuration.
- Parameters:
config¶ (
Union[NoPoolSyncConfig[TypeVar(ConnectionT),TypeVar(DriverT, bound= SyncDriverAdapterBase | AsyncDriverAdapterBase)],SyncDatabaseConfig[TypeVar(ConnectionT),TypeVar(PoolT),TypeVar(DriverT, bound= SyncDriverAdapterBase | AsyncDriverAdapterBase)],NoPoolAsyncConfig[TypeVar(ConnectionT),TypeVar(DriverT, bound= SyncDriverAdapterBase | AsyncDriverAdapterBase)],AsyncDatabaseConfig[TypeVar(ConnectionT),TypeVar(PoolT),TypeVar(DriverT, bound= SyncDriverAdapterBase | AsyncDriverAdapterBase)]]) – The configuration instance.- Return type:
- Returns:
None, or an awaitable if closing an async pool.
- static get_cache_config()[source]¶
Get the current global cache configuration.
- Return type:
CacheConfig- Returns:
The current cache configuration.
- static log_cache_stats()[source]¶
Log current cache statistics using the configured logger.
- Return type:
- static configure_cache(*, sql_cache_size=None, fragment_cache_size=None, optimized_cache_size=None, sql_cache_enabled=None, fragment_cache_enabled=None, optimized_cache_enabled=None)[source]¶
Update cache configuration with partial values.
- Parameters:
sql_cache_size¶ (
int|None) – Size of the statement/builder cache.fragment_cache_size¶ (
int|None) – Size of the expression/parameter/file cache.optimized_cache_size¶ (
int|None) – Size of the optimized expression cache.sql_cache_enabled¶ (
bool|None) – Enable/disable statement and builder cache.fragment_cache_enabled¶ (
bool|None) – Enable/disable expression/parameter/file cache.optimized_cache_enabled¶ (
bool|None) – Enable/disable optimized expression cache.
- Return type:
See Also¶
Configuration for configuration patterns.
Drivers and Querying for execution patterns.
Driver for driver APIs.