Skip to main content

snekql

Write database queries in Python. Get results with types you can check.

snekql is an async query builder for SQLite and MariaDB. You choose the columns, write the joins, and decide where transactions begin and end. It checks your Python values and turns your queries into parameterized SQL.

async with db.transaction() as tx:
    users = await tx.fetch_all(
        sqlite.select(User).where(User.email.like("%@example.com")),
    )
    # users: list[User[sqlite.Row]]

This uses the User model in the example below. Select User.email instead, and the result is list[str]. Join another model, and each result includes both rows. Those types carry through to your application code.

There is no session tracking changes to your objects, and reading an attribute never loads a relationship. Want to change a row? Write an update. How this differs from an ORM →

Python 3.15+ · SQLite and MariaDB · Type checking with ty

Try it

uv add 'snekql[aiosqlite]'

Save this as app.py and run uv run python app.py. It creates an in-memory database, inserts a user, and prints alice@example.com. You can run it again without cleaning up any files.

A complete, runnable example
import asyncio
from typing import ClassVar

from snekql import sqlite


class User[State = sqlite.Pending](sqlite.Model[State]):
    __row_type__: ClassVar[sqlite.ReadType[User[sqlite.Row]]]
    id: sqlite.GenCol[int] = sqlite.Integer(
        primary_key=True,
        auto_increment=True,
        default=sqlite.PENDING_GENERATION,
    )
    email: sqlite.Col[str] = sqlite.Text()


MIGRATIONS = {
    "001_create_user": """
        CREATE TABLE "user" (
            "id" INTEGER PRIMARY KEY AUTOINCREMENT,
            "email" TEXT NOT NULL
        ) STRICT
    """,
}


async def main() -> None:
    async with await sqlite.Database.initialize(
        sqlite.Config(database=":memory:"),
    ) as db:
        await db.migrate(MIGRATIONS)
        await db.verify_migrations(MIGRATIONS)
        await db.verify([User])

        async with db.transaction() as tx:
            await tx.execute(sqlite.insert(User(email="alice@example.com")))
            user = await tx.fetch_one(
                sqlite.select(User).where(User.email.eq("alice@example.com")),
            )
            print(user.email)


asyncio.run(main())

The model describes the Python values and database columns. The migration creates the table. Opening a connection does not create or change your schema.

The getting-started guide walks through each part, including why the model has Pending and Row states.

For MariaDB, install snekql[aiomysql] and use the snekql.mariadb imports. See connecting to MariaDB for a setup example. You can install plain snekql if you only need to build and inspect SQL.

What you can do

  • Read and write rows. Select columns, join tables, group results, insert batches, and handle unique-key conflicts. Queries
  • Keep Python types at the database boundary. Validate values with Pydantic and preserve result types through reads and writes. Models
  • Use SQL when you need it. Inspect generated SQL or run your own statement on the same transaction. SQL inspection · Raw SQL
  • Make schema changes deliberately. Commit migration SQL, apply it during deployment, and check the live schema against your models. Migrations

For longer queries, the builder supports CTEs, UNION, and recursive queries. Backend differences are documented in the support table.

Is it a fit?

Use snekql if you want async database access and typed queries without handing relationship loading or schema changes to an ORM. You still need to understand SQL, write migrations, and choose your transaction boundaries.

It does not provide synchronous access, automatic migrations, or database backends other than SQLite and MariaDB. ty 0.0.84 is the only supported checker. Typing support and limits

snekql is on 0.x. Minor releases can break compatibility, so pin the minor version and read the changelog before upgrading. Moving to 0.8? Start with the migration guide.

Where to go next

I want to… Read this
Run my first query Getting started
Define tables and choose column types Models · Storage
Write queries and understand their results Queries · Typing
Add snekql to a web app or worker Transactions · Service recipes
Work with an existing database Adoption recipes
Look up a method or supported feature API reference · Backend support

Browse all documentation →

Using a coding agent? Install the included skill or add consumer instructions so it consults the installed, version-matched guides.

Installed packages include curated guides under snekql/docs/, matched to the installed release. Read the offline index with:

python -c 'from importlib.resources import files; print(files("snekql").joinpath("docs", "README.md").read_text())'

The bundle includes user guides, API and typing references, upgrade guides, and linked recipes. Historical designs and internal reviews are not included; their references are marked repository-only.

Examples and a compact guide are also available through the CLI:

snekql --examples
snekql --example basic
snekql --agent-docs

To work on snekql itself, see contributing and local checks.

Metadata

Release files for snekql 0.9.0

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

Source distribution (sdist)

Source distribution for snekql 0.9.0
File Size Uploaded Provenance Actions
snekql-0.9.0.tar.gz 463.2 kB Without provenance Details for snekql-0.9.0.tar.gz

Built distribution (wheel)

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

Total release size: 985.1 kB

Release files / snekql-0.9.0.tar.gz

Download URL snekql-0.9.0.tar.gz
Size 463.2 kB
Tags Source
SHA-256 checksum
How to use checksums
7022a9c5022be5c67ba49e2efc5c56ed769cd3e8d122f1ea31b77accbeb553e0
BLAKE2b-256 checksum
How to use checksums
07bc8dddd856e4af73b947fedf79e2405e25e47859ba1097379e023c85ddabc5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.13.0 {"installer":{"name":"uv","version":"0.13.0","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Omarchy","version":"4.0.4","id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / snekql-0.9.0-py3-none-any.whl

Download URL snekql-0.9.0-py3-none-any.whl
Size 521.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e8069d844285ce75fc4ccd469d33b9a2a372a025d500b2569c941ac5c1ea8962
BLAKE2b-256 checksum
How to use checksums
3e6799f7e6f8ec5d57c41bd0b7d80b56031d02a97ce7f6167b71b9624e58bd3b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.13.0 {"installer":{"name":"uv","version":"0.13.0","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Omarchy","version":"4.0.4","id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Security for snekql 0.9.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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