flask_more_smorest.sqla.database
Database configuration and utilities for flask-more-smorest.
This module provides the core SQLAlchemy setup and utilities for configuring custom User models and other database-related functionality.
Functions
Get query statistics for the current request. |
|
|
Initialize the database with the Flask application. |
Classes
|
Base class for all SQLAlchemy models. |
- class flask_more_smorest.sqla.database.Base(**kwargs)[source]
Base class for all SQLAlchemy models.
This serves as the declarative base for all models in the application. It provides the foundation for SQLAlchemy’s ORM functionality.
- __init__(**kwargs)
A simple constructor that allows initialization from kwargs.
Sets attributes on the constructed instance using the names and values in
kwargs.Only keys that are present as attributes of the instance’s class are allowed. These could be, for example, any mapped columns or relationships.
- flask_more_smorest.sqla.database.init_db(app)[source]
Initialize the database with the Flask application.
This function binds the SQLAlchemy database instance to the Flask application, making it available throughout the application context.
- Parameters:
app (
Flask) – Flask application instance to initialize the database with- Return type:
Example
>>> from flask import Flask >>> from flask_more_smorest.sqla import init_db >>> >>> app = Flask(__name__) >>> app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///app.db' >>> init_db(app)
- flask_more_smorest.sqla.database.get_request_query_stats()[source]
Get query statistics for the current request.
Returns a dictionary with query count and total time for the current request. When called outside an application context, or when SQLALCHEMY_PERFORMANCE_MONITORING is disabled (so no query stats have been collected), this function returns zeros for both values.
Example
>>> stats = get_request_query_stats() >>> print(f"Queries: {stats['query_count']}, Time: {stats['total_query_time']:.3f}s")