korbase¶
An async ORM for Postgres and SQLite, built the Korvex way. You describe tables with plain Python classes, build queries in Python, and read rows back as the same classes. Its SQL migrations are files you read before they run.
It is for everyone who needs a Postgres or SQLite database in Python: people who have never used a database, and experienced developers who want a small, typed and fast layer with no surprises.
Status¶
Alpha. The API may change before 1.0, with no deprecation step. Each change is in the changelog, with how to update.
Install¶
Python 3.10 and later. Only alphas exist for now, so pass --pre. For
Postgres, with the asyncpg driver:
pip install --pre "korbase[asyncpg]" msgspec
For SQLite, which needs no driver:
pip install --pre korbase msgspec
Example¶
This runs on a scratch database that already has an authors table. The
tutorial creates tables with a migration.
from __future__ import annotations
import asyncio
from typing import Annotated
import msgspec
from korbase import PK, Database, insert, select, table
@table("authors")
class Author(msgspec.Struct):
id: Annotated[int, PK()]
name: str
async def main() -> None:
async with await Database.connect("postgresql://user:password@localhost/scratch") as db:
[ada] = await db.run(insert(Author).values(name="Ada")) # the new row, as an Author
found = await db.run(select(Author).where(name="Ada")) # list[Author]
print(ada, found) # Author(id=1, name='Ada') [Author(id=1, name='Ada')]
asyncio.run(main())
Key ideas¶
- Plain models. msgspec Structs, dataclasses and TypedDicts work with
@table("name"). Markers such asPK(),FK(...)andRel(...)go insideAnnotated[...]. - Typed results.
run(select(Post))is alist[Post]andfirst(...)isPost | None, so your editor knows every field. An insert returns its row; an update or delete returns the number of rows changed. - Plain queries.
.where(author_id=1),.where(cols(Post).title.like("H%")). Every value is sent as a$nparameter, never pasted into the SQL. A write with nowhere()is refused. - Queries compile once. Build one at module level with
param("name")for the values that change, and reuse it. - Relations load on request.
.load(...)adds one query per relation. - Reviewed SQL migrations.
makewrites the SQL, you read it,applyruns it. A change that drops data is refused until you review it.
Where to start¶
New to databases or to korbase? Follow the tutorial. You build a small blog database from zero. The glossary explains each word.
Experienced developer? Read the guide pages you need:
- Models: the three model kinds, keys, defaults, indexes.
- Queries: select, insert, update, delete and upsert.
- Relations:
Reland.load. - Transactions: commit, rollback, savepoints, raw SQL.
- Migrations:
make,apply,statusand review. - Korvex: use korbase in a web app.
- SQLite: the SQLite backend, its DSN and its differences.
- Testing: run your tests on a real Postgres.
Expert? Each guide page ends with a "Going further" section: limits,
internals and edge cases. The reference lists every
public name, built from the docstrings. AI covers llms.txt and
the agent skill. To work on korbase itself, read
AGENTS.md.