Flask Extension¶
from sqlspec.extensions.flask import (
FlaskConfigState,
SQLSpecPlugin,
set_request_tenant,
tenant_bypass,
)
Flask WSGI extension managing flask.g request-scoped sessions, deferred tenant leases, automatic commit/rollback teardown hooks, and background event-loop portal bridging for async adapters (SQLSpecSyncService and SQLSpecAsyncService are documented in Service Layer). See the Flask Integration Guide for usage examples.
Plugin & Configuration State¶
SQLSpecPlugin
¶
Flask extension for SQLSpec database integration.
Provides request-scoped session management, automatic transaction handling, and async adapter support via portal pattern.
init_app
¶
Initialize Flask application with SQLSpec.
Validates configuration, creates portal if needed, creates pools, and registers hooks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
app
|
Flask
|
Flask application to initialize. |
required |
Raises:
| Type | Description |
|---|---|
ImproperConfigurationError
|
If extension already registered or keys not unique. |
get_config
¶
Return a registered database configuration by identifier, or the sole configuration when omitted.
set_request_tenant
¶
Store an authorized tenant receipt on flask.g for the active request.
get_session
¶
Get or create database session for current request.
Sessions are cached per request for consistency, and the request connection
is checked out on first access. An asynchronous configuration returns a
blocking session whose calls run on the plugin's portal event loop, so a
synchronous view calls session.select(...) without await.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
Any
|
Optional session key, bind key, config type, or config instance. |
None
|
Returns:
| Type | Description |
|---|---|
Any
|
Database session (driver instance). |
get_connection
¶
get_db_replica_session
¶
Return the request's replica session, bridging async drivers through the portal.
get_replica_connection
¶
Return the request-owned raw replica connection.
get_db_routing_session
¶
Return the request's lazy automatic routing facade.
FlaskConfigState
¶
Internal state for each database configuration in Flask extension.
Holds configuration, state keys, commit settings, and transaction logic.
tenant_receipt_keys
¶
should_commit
¶
should_rollback
¶
Determine if HTTP status code should trigger rollback.
Manual mode always rolls back uncommitted handler work, and autocommit modes roll back any status code that does not trigger a commit.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
status_code
|
int
|
HTTP response status code. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if status should trigger rollback, False otherwise. |
Request Tenant Helpers¶
set_request_tenant
¶
Store an authorized tenant receipt on Flask g for the active request.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tenant_id
|
str | UUID | TenantContextPayload
|
Verified tenant identifier or immutable payload. |
required |
variables
|
Mapping[str, str] | None
|
Extra session variables merged into the stored payload. |
None
|
key
|
str | None
|
Optional configuration |
None
|
Returns:
| Type | Description |
|---|---|
TenantContextPayload
|
The immutable |
tenant_bypass
¶
Mark a route handler as an explicit tenant bypass.
Framework plugins resolve a bypass payload for marked routes, so public routes such as login or health checks can use tenant-enabled sessions without a tenant.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
handler
|
Any
|
Route function or handler object. |
required |
Returns:
| Type | Description |
|---|---|
Any
|
The same handler. |