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_request_query_stats()

Get query statistics for the current request.

init_db(app)

Initialize the database with the Flask application.

Classes

Base(**kwargs)

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.

metadata: ClassVar[MetaData] = MetaData()

Refers to the _schema.MetaData collection that will be used for new _schema.Table objects.

registry: ClassVar[registry] = <sqlalchemy.orm.decl_api.registry object>

Refers to the _orm.registry in use where new _orm.Mapper objects will be associated.

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:

None

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.

Return type:

dict[str, Any]

Returns:

Dictionary with ‘query_count’ and ‘total_query_time’ keys

Example

>>> stats = get_request_query_stats()
>>> print(f"Queries: {stats['query_count']}, Time: {stats['total_query_time']:.3f}s")