Building an asynchronous SQLAlchemy repository layer
The updated flask-sqlalchemy
extension module supports SQLAlchemy 2.x that provides API utilities, which use the asyncio
environment with greenlet
as the main library, allowing propagation of the await
keyword in the APIs’ internal processes. Our ch05-web
and ch05-api
projects have the async transactions that call these awaited SQLAlchemy Create-Read-Update-Delete (CRUD) operations using a new DB configuration in our projects’ /models/config.py
file that utilizes an asyncpg
driver to build a session for non-blocking repository transactions.
Setting up the DB connectivity
To start with the configuration, install the asyncpg
DB driver or dialect that the asyncio
-driven SQLAlchemy module requires using the pip
command:
pip install asyncpg
Also, include the greenlet library in the installation if it is not yet part of the virtual environment:
pip install greenlet
The setup also requires a connection string...