Knexpy
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 building —
select,from_, aliases,distinct, everywhere_*variant,group_by/having,order_by(withNULLS FIRST/LAST),limit/offset, joins and subqueries. - Aggregates —
count,sum,avg,min,max. - Writes —
insert,insert_or_ignore,upsert,update,delete, and batchinsert_json(accepts dicts, dataclasses and pydantic models, with chunked transactions and amappinghook). - Type checking —
Knex(..., 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 models —
db.select(...).as_(User).all()returnslist[User]from dataclasses orTypedDicts. - Pagination & inspection —
paginate()andexplain()/explain_query_plan(). - Schema & introspection — create/alter/drop tables and indexes,
tables,columns,foreign_keys, andbackup. - Migrations — a runner (
knexpy migrateCLI +Knex.run_migrations). - Hooks — observe or intercept operations (
before_*/after_*events). - Async facade —
AsyncKnexreuses the same builder overaiosqlite. - Connection factories —
from_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_atandmodified_atare added automatically.idis 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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6bdb3b6ab28c1cd90bd367f16b96283e45a303bbe2866cb56da232d72aa6ecef
|
|
| MD5 |
e8195a07fd7654fe4d7df7cf4453cbac
|
|
| BLAKE2b-256 |
9d0e259e89cf5c7ace4fa3c9cd539b22eb3804f989a6abb0df5e5a8308b471b6
|
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0019ced111ec45a6127450a0122670841f78febf1f71441792bc8b67812ff5a7
|
|
| MD5 |
38df4cdae7a230a3f21ac6413adf32b6
|
|
| BLAKE2b-256 |
ccb2823f9ebb6e0c00651f27315ceb5c09abc7c18b712aa11c301fd8fd84f816
|