Skip to main content

Air SQLModel

Right now we have verified this project supports PostgreSQL and SQLite. It is unclear if other databases are supported, but they may be. Please open an issue if you have success or failure with other databases.

Air plus SQLModel Made Easy!

SQLModel is a wrapper around the venerable and proven SQLAlchemy library. Like Typer, FastAPI, pydantic, and Air, SQLModel allows for definition of critical objects with type annotations - in this case database tables. SQLModel makes SQLAlchemy a bit easier to use, although it's possible to drop down to the raw power of SQLAlchemy at any time.

Using Air's SQL module requires an understanding of SQLModel.

Installation

You can install AirSQLModel with UV. Here's how to install it with PostgreSQL support:

uv add "airsqlmodel[postgresql]"

And now for SQLite support:

uv add "airsqlmodel[sqlite]"

[!NOTE]
Currently AirSQLModel only supports PostgreSQL and SQLite. If you try to use another database, submit a pull request or open an issue.

Configuring Air for SQL

While not strictly required, it's highly recommended to use the DATABASE_URL environment variable to configure your database connection. This is a common convention used by many web frameworks and libraries.

To ensure the database remains connected to Air, we configure a lifespan function, and pass that to the Air app upon instantiation. If you don't do this, then the connection will eventually expire and your application will start throwing errors.

So when instantiating your project's root 'app':

import air
import airsqlmodel as sql

app = air.Air(lifespan=sql.async_db_lifespan)

Making SQL Queries inside Air Views

Most of the time, you'll be using SQLModel inside your Air views. The easiest way to do this is to use the air_sqlmodel.async_session_dependency dependency, which requires that the DATABASE_URL environment variable be set. This will provide you with an asynchronous session connected to your database.

import air
import airsqlmodel as sql
from sqlmodel import select

app = air.Air(lifespan=airsqlmodel.async_db_lifespan)

@app.page
async def index(request: Request, session: sql.AsyncSession = air.Depends(sql.async_session_dependency)):
    # Use the session to interact with the database
    result = await session.execute(select(User).where(User.name == "John"))
    user = result.scalars().first()
    
    return air.Main(
        air.H1("User Info"),
        air.P(f"Name: {user.name}"),
        air.P(f"Email: {user.email}"),
    )

Making SQLModel Queries Outside Air Views

Sometimes you may want to make SQL queries outside of Air views, for example in background tasks or other parts of your application. In these cases, you can use the airsqlmodel.get_async_session function to get an asynchronous session.

import air
import airsqlmodel as sql
from sqlmodel import select

async def some_background_task():
    async with sql.get_async_session() as session:
        result = await session.execute(select(User).where(User.active == True))
        active_users = result.scalars().all()
        # Do something with active_users

Release files for airsqlmodel 0.1.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for airsqlmodel 0.1.4
File Size Uploaded
airsqlmodel-0.1.4.tar.gz 4.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for airsqlmodel 0.1.4
File Interpreter ABI Platform
airsqlmodel-0.1.4-py3-none-any.whl Python 3 none any Details

Total release size: 9.9 kB

Release files / airsqlmodel-0.1.4.tar.gz

Download URL airsqlmodel-0.1.4.tar.gz
Size 4.6 kB
Tags Source
SHA-256 checksum
How to use checksums
79c376820e996547d2c008148e37c42e9f983b162f909f0c8e6096f271b0938f
BLAKE2b-256 checksum
How to use checksums
f1d9cd1c395416495dd04df31d40abb9995f655ea1cf3ee6b3c6db9489c60602
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.9.7

Release files / airsqlmodel-0.1.4-py3-none-any.whl

Download URL airsqlmodel-0.1.4-py3-none-any.whl
Size 5.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
38aa98623c49d8224a6fbbb34dbdf6df040fa9a952d1b2c14b787c27f6033ab4
BLAKE2b-256 checksum
How to use checksums
128c929cd5eb6adf67d05c6244057a33b5accc9c21ed282f42b0c519f7ae41ca
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.9.7

Release history Release notifications | RSS feed

This release

0.1.4 This release

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page