Skip to main content

Knexpy

Knexpy License Downloads Supported Versions Documentation Status Ruff pre-commit

A query builder for SQLite3 based on Knexjs. Knexpy compiles chainable, parameterized SQL for you, so you never interpolate a value into a query string.

Features

  • Fluent query buildingselect, from_, aliases, distinct, every where_* variant, group_by/having, order_by (with NULLS FIRST/LAST), limit/offset, joins and subqueries.
  • Aggregatescount, sum, avg, min, max.
  • Writesinsert, insert_or_ignore, upsert, update, delete, and batch insert_json (accepts dicts, dataclasses and pydantic models, with chunked transactions and a mapping hook).
  • Type checkingKnex(..., type_check=True) validates values against column types on insert/update.
  • Transactions — context-manager support, atomic batch inserts, and manual begin/commit/rollback.
  • Typed row modelsdb.select(...).as_(User).all() returns list[User] from dataclasses or TypedDicts.
  • Pagination & inspectionpaginate() and explain() / explain_query_plan().
  • Schema & introspection — create/alter/drop tables and indexes, tables, columns, foreign_keys, and backup.
  • Migrations — a runner (knexpy migrate CLI + Knex.run_migrations).
  • Hooks — observe or intercept operations (before_*/after_* events).
  • Async facadeAsyncKnex reuses the same builder over aiosqlite.
  • Connection factoriesfrom_connection, make_db, and query logging.

Installation

pip install knexpy

For the async facade, install the optional extra:

pip install "knexpy[async]"

Quick start

from Knexpy import Field, Knex

db = Knex(":memory:", type_check=True)  # pass a path to persist to a file

db.table("users", [Field.varchar("name"), Field.integer("age", {"null": True})])
db.insert_json(
    "users",
    [
        {"name": "Ada", "age": 36},
        {"name": "Lin", "age": 45},
        {"name": "Grace", "age": 40},
    ],
)

rows = db.select("id", "name").from_("users").order_by("name").query()
# [{"id": "...", "name": "Ada"}, ...]

When creating a table, id, created_at and modified_at are added automatically. id is a collision-resistant random hex string.

Examples

Runnable examples live in examples/ and cover the full feature set:

File Shows
quickstart.py connect, schema, insert, query, CRUD helpers
query_building.py aliases, aggregates, grouping, nulls ordering, raw SQL
joins_where.py joins and every where_* clause
writes.py insert, upsert, chunked/mapped inserts, update/delete
transactions.py context manager, atomic batches, manual control
schema_introspection.py DDL, introspection, backup
row_models.py typed rows via as_(Model)
pagination_explain.py paginate, explain
hooks.py observing and intercepting operations
migrations.py versioned schema changes
async_example.py the AsyncKnex facade
connection_factory.py from_connection, make_db, logging

You can report bugs and discuss features on the GitHub issues page. For the full API reference check Readthedocs.

Download files

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

Source Distribution

knexpy-1.0.0.tar.gz (681.4 kB view details)

Uploaded Source

Built Distribution

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

knexpy-1.0.0-py3-none-any.whl (53.9 kB view details)

Uploaded Python 3

File details

Details for the file knexpy-1.0.0.tar.gz.

File metadata

  • Download URL: knexpy-1.0.0.tar.gz
  • Upload date:
  • Size: 681.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.11.16

File hashes

Hashes for knexpy-1.0.0.tar.gz
Algorithm Hash digest
SHA256 6bdb3b6ab28c1cd90bd367f16b96283e45a303bbe2866cb56da232d72aa6ecef
MD5 e8195a07fd7654fe4d7df7cf4453cbac
BLAKE2b-256 9d0e259e89cf5c7ace4fa3c9cd539b22eb3804f989a6abb0df5e5a8308b471b6

See more details on using hashes here.

File details

Details for the file knexpy-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: knexpy-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 53.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.11.16

File hashes

Hashes for knexpy-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0019ced111ec45a6127450a0122670841f78febf1f71441792bc8b67812ff5a7
MD5 38df4cdae7a230a3f21ac6413adf32b6
BLAKE2b-256 ccb2823f9ebb6e0c00651f27315ceb5c09abc7c18b712aa11c301fd8fd84f816

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 files

0.1.3

1 file

0.1.1

1 file

0.1.0

1 file

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