Metadata-Version: 2.5
Name: korbase
Version: 0.1.0a5
Summary: Async ORM for Postgres and SQLite, built the Korvex way.
Project-URL: Homepage, https://3d3n-pyc.github.io/korbase/
Project-URL: Documentation, https://3d3n-pyc.github.io/korbase/
Project-URL: Source, https://github.com/3d3n-pyc/korbase
Project-URL: Changelog, https://github.com/3d3n-pyc/korbase/blob/main/CHANGELOG.md
Author: Maxime Lefevre
License: MIT
License-File: LICENSE
Keywords: async,database,orm,postgres,sql,sqlite
Classifier: Development Status :: 3 - Alpha
Classifier: Framework :: AsyncIO
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Topic :: Database
Classifier: Typing :: Typed
Requires-Python: >=3.10
Provides-Extra: asyncpg
Requires-Dist: asyncpg>=0.30; extra == 'asyncpg'
Provides-Extra: korpg
Provides-Extra: korvex
Requires-Dist: korvex>=1.0.0rc5; extra == 'korvex'
Description-Content-Type: text/markdown

# korbase

An async ORM for Postgres and SQLite, built the Korvex way. It is pure Python.
Models are msgspec Structs, dataclasses or TypedDicts. The driver is
asyncpg. It has SQL migrations, written for you and applied after review.

Alpha: the API may change before 1.0. Python 3.10 and later.

## Install

```sh
pip install --pre "korbase[asyncpg]"
```

SQLite needs no extra. Documentation: https://3d3n-pyc.github.io/korbase/

## Example

This runs on a scratch database. The first statement drops the tables
`users` and `posts`.

```python
from __future__ import annotations
import asyncio
from typing import Annotated, Optional

import msgspec
from korbase import FK, PK, Database, Rel, cols, insert, select, table

DSN = "postgresql://user:password@localhost:5432/scratch"


@table("users")
class User(msgspec.Struct):
    id: Annotated[int, PK()]
    name: str


@table("posts")
class Post(msgspec.Struct):
    id: Annotated[int, PK()]
    author_id: Annotated[int, FK("users.id")]
    title: str
    author: Annotated[Optional[User], Rel("author_id")] = None


async def main() -> None:
    async with await Database.connect(DSN) as db:
        await db.execute(
            """
            DROP TABLE IF EXISTS posts; DROP TABLE IF EXISTS users;
            CREATE TABLE users (id bigint GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, name text NOT NULL);
            CREATE TABLE posts (id bigint GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
                                author_id bigint NOT NULL REFERENCES users(id), title text NOT NULL);
            """
        )
        [ada] = await db.run(insert(User).values(name="Ada"))  # insert returns the new row
        await db.run(insert(Post).values(author_id=ada.id, title="Hello"))

        users = await db.run(select(User).where(name="Ada"))
        print(users)  # [User(id=1, name='Ada')]

        posts = await db.run(select(Post).load(cols(Post).author))
        print([(x.title, x.author.name if x.author else None) for x in posts])


asyncio.run(main())
```

## Docs

The guide is in `docs/` in the repository. Start with
[`docs/tutorial.md`](docs/tutorial.md): it builds a small blog database
with migrations, from zero. The docs site is at
https://3d3n-pyc.github.io/korbase/, with `llms.txt` for AI assistants.

## Status

Working today: models, queries, transactions, relations loaded with
`.load()`, the Korvex integration (`pip install --pre "korbase[korvex]"`) and
SQL migrations with `python -m korbase.migrate`. SQLite works through the
standard library, with a `sqlite:///app.db` DSN: see [`docs/sqlite.md`](docs/sqlite.md).

Not yet: the korpg driver (its extra is empty). `.load()` raises `KorbaseError` on tables with a composite
primary key.

## Tests

```sh
eval "$(bash tests/pg/start.sh | grep '^export')"   # Postgres in Docker, port 55433
uv run pytest
```

Contributor rules are in [AGENTS.md](AGENTS.md). Changes are listed in
[CHANGELOG.md](CHANGELOG.md).
