Skip to main content

SQLAlchemyLiteProxy

ClassSource

db_proxy = SQLAlchemyLiteProxy[_SQLAlchemyDB_co](
db: _SQLAlchemyLiteDB_co,
)

Proxy class of flask_sqlalchemy_lite.SQLAlchemy.

This class is a wrapper of the regular SQLAlchemy. By adjusting the behavior of the flask_sqlalchemy.SQLAlchemy, it can mimic the usage of flask_sqlalchemy_lite.SQLAlchemy.

Note that not all the functionalities of this proxy can be exactly the same as the those of flask_sqlalchemy_lite.SQLAlchemy. In specific, this proxy will do the following things:

  • Provide a regular session like that of flask_sqlalchemy_lite.SQLAlchemy. This regular session is managed by the instance of this proxy class.
  • Provide other basic methods in flask_sqlalchemy_lite.SQLAlchemy. The usages would be equivalent but the implmentation is based on flask_sqlalchemy.
  • Any functionality that cannot be used will raise a NotImplementedError.

Aliases

This class can be acquired by

import flask_sqlalchemy_compat as fsc


fsc.SQLAlchemyLiteProxy
fsc.flask_sa_lite_api.SQLAlchemyLiteProxy

Arguments

ArgumentTypeRequired
Description
db_SQLAlchemyDB_co

The database instance exporting the session and engine instances.

It is provided by the Flask extensions like flask_sqlachemy. This wrapper will mimic the behaviors of flask_sqlalchemy_lite even if the given instance is from the not-lite version.

Methods

init_app

db_proxy.init_app(
app: Flask,
)

Register the extension on an application, creating engines from its Flask.config.

Requires

ArgumentTypeRequired
Description
appFlaskThe application to register.

get_engine

db_proxy.get_engine(
name: str = "default"
)

Get a specific engine associated with the current application.

The engine attribute is a shortcut for calling this without an argument to get the default engine.

Requires

ArgumentTypeRequired
Description
namestrThe name associated with the engine.

Returns

ArgumentType
Description
enginesa.engine.EngineThe engine gotten by the name.

get_session

sess: sa.orm.Session = db_proxy.get_session(
name: str = "default"
)

Create a sa.orm.Session that will be closed at the end of the application context. Repeated calls with the same name within the same application context will return the same session.

The session attribute is a shortcut for calling this without an argument to get the default session.

Requires

ArgumentTypeRequired
Description
namestrA unique name for caching the session.

Returns

ArgumentType
Description
sesssa.orm.SessionThe session gotten by the name.

get_async_engines

db_proxy.get_async_engines(*args: Any, **kwargs: Any)
danger

This method is not implemented by the proxy.


get_async_session

db_proxy.get_async_session(*args: Any, **kwargs: Any)
danger

This method is not implemented by the proxy.

Properties

db

db: _SQLAlchemyDB_co = db_proxy.db

The db instance provided by the Flask SQLAlchemy extension.


sessionmaker

sessionmaker: sa.orm.sessionmaker[sa.orm.Session] = db_proxy.sessionmaker

The same as flask_sqlalchemy_lite.SQLAlchemy().sessionmaker.

The session factory configured for the current application. This can be used to create sessions directly, but they will not be closed automatically at the end of the application context. Use session and get_session for that.

This can also be used to update the session options after self.init_app, by calling its sa.orm.sessionmaker.configure method.


session

sess: sa.orm.Session = db_proxy.session

The same as flask_sqlalchemy_lite.SQLAlchemy().session.

The default session for the current application context. It will be closed when the context ends.


engines

engines: dict[str, sa.engine.Engine] = db_proxy.engines

The same as flask_sqlalchemy_lite.SQLAlchemy().engines.

The dictionary of named engines registered to this extension.


engine

engine: sa.engine.Engine = db_proxy.engine

The same as flask_sqlalchemy_lite.SQLAlchemy().engine.

The default engine associated with the current application.


async_engines

db_proxy.async_engines
danger

This property is not implemented by the proxy.


async_engine

db_proxy.async_engine
danger

This property is not implemented by the proxy.


async_sessionmaker

db_proxy.async_sessionmaker
danger

This property is not implemented by the proxy.


async_session

db_proxy.async_session
danger

This property is not implemented by the proxy.