Sanic Extension¶
from sqlspec.extensions.sanic import (
SanicConfigState,
SQLSpecAsyncService,
SQLSpecPlugin,
SQLSpecSyncService,
get_connection_from_request,
get_or_create_session,
)
Sanic server listener and request/response middleware extension for pool lifecycle management and request.ctx session injection. See the Sanic Integration Guide for usage examples.
Plugin & Configuration State¶
SQLSpecPlugin
¶
SQLSpec integration for Sanic applications.
Provides Sanic-native configuration parsing and request helper methods.
Runtime listener and middleware behavior is registered by init_app.
SanicConfigState
¶
Internal state for a Sanic database configuration.
Tracks the keys and behavior needed to bind one SQLSpec config into a Sanic app and its request context.
Request Context Helpers¶
get_or_create_session
¶
Get or create database session for request.
Sessions are cached per request to return the same session instance across multiple calls in one request.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request
|
Any
|
Sanic request instance. |
required |
config_state
|
SanicConfigState
|
Configuration state for the database. |
required |
Returns:
| Type | Description |
|---|---|
Any
|
Database session driver instance. |
get_connection_from_request
¶
Get database connection from request context.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request
|
Any
|
Sanic request instance. |
required |
config_state
|
SanicConfigState
|
Configuration state for the database. |
required |
Returns:
| Type | Description |
|---|---|
Any
|
Database connection object. |
Raises:
| Type | Description |
|---|---|
ImproperConfigurationError
|
If SQLSpec request middleware has not stored a connection. |