Skip to main content

Add your description here

Project description

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

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.

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

airsqlmodel-0.1.1.tar.gz (4.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

airsqlmodel-0.1.1-py3-none-any.whl (5.1 kB view details)

Uploaded Python 3

File details

Details for the file airsqlmodel-0.1.1.tar.gz.

File metadata

  • Download URL: airsqlmodel-0.1.1.tar.gz
  • Upload date:
  • Size: 4.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.1

File hashes

Hashes for airsqlmodel-0.1.1.tar.gz
Algorithm Hash digest
SHA256 7af87d937e40233423a20fab86f061ca5e4f0b51f8d850de13adb9001333b45a
MD5 403692d884d1f28ea337ea5e68d58078
BLAKE2b-256 fa1e15b9bb72f46427b978e48a1cd8418cc986f8d0da53d331f1b37e1d17858a

See more details on using hashes here.

File details

Details for the file airsqlmodel-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for airsqlmodel-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 0c541b0f771b71a8f4ddbfe1fe2762aea12843894544ef7b28ee9bbc515099af
MD5 4380915f6337d1b741f696983c07161f
BLAKE2b-256 a6253d6da1ec7c49b35bcfe6cccc8747a0afd5feecd6b6a23398a1d426277af4

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page