LeoVasanko de2476c59c Add revision numbers: snapshot r field, kanta.rev, CLI rN selectors, k1 meta migration
Revisions are absolute state positions between diffs: the empty initial
state is revision 0 and each change record advances it by one.  Snapshots
persist the revision of the state they contain in a new r field, stamped
with a meta format version k (kanta.structs.META_FORMAT, currently 1);
change records carry no revision of their own.

- kanta.rev exposes the current revision (raises before open)
- replay restores the revision, trusting r on k-marked snapshots and
  counting change records from the start of the file for legacy (k0) ones
- rotation preserves absolute revisions, counted from record positions
- meta migration k1 rewrites a legacy file in full on writable open:
  leading snapshot preserved and re-stamped, all change records kept,
  closing snapshot with the total revision; read-only opens compute the
  same revision without touching the file
- CLI range selectors accept rN bounds: rN is the record that produced
  state N with its state (the change, or the snapshot materializing it);
  single indexes must exist, ranges truncate Python-style
- CLI snapshot indicator lines show the snapshot revision
2026-09-28 00:25:52 +00:00
2026-06-12 19:15:46 +00:00
2026-09-02 17:34:20 +00:00

Kanta database

Kanta is a small embedded NoSQL store for async Python apps. It keeps live state in memory, writes transactional diffs to an append-only log, and supports versioned schema migrations.

Why Kanta

  • Fast synchronous reads and modifications on native Python objects
  • Durable writes with append-only file and periodic snapshots
  • Transaction semantics with rollback on failure
  • Explicit schema evolution via migrate_vN functions
  • Line-based JSON or binary MessagePack, or bring your own serializer
  • Even in JSON we can use non-string keys, bytes, datetimes, UUID and other types

This design is often preferable when you want low-latency local persistence without operating a separate database service. You get straightforward deployment, auditable history, and deterministic replay while keeping application state ergonomic to work with.

Queries and updates on native data are far faster than over an SQL server connection, and we can can provide fully synchronous operation. The limitation is that you can only use the same database within a single process at a time, but this is well suited for async programming.

Quick Start

import asyncio
import msgspec
from uuid import UUID, uuid7

from kanta import Kanta


class User(msgspec.Struct):
    name: str = ""
    email: str | None = None


class Data(msgspec.Struct):
    users: dict[UUID, User] = {}


async def main() -> None:
    async with Kanta("data.kantadb", Data()) as kanta:
        user_id = uuid7()
        with kanta.transaction(action="create_user") as data:
            data.users[user_id] = User(name="Alice")


asyncio.run(main())

Core Concepts

  1. Define your schema as a msgspec.Struct root object.
  2. Mutate data inside with kanta.transaction(...):.
  3. Let Kanta flush queued changes to disk in the background.
  4. Use snapshots and replay for fast startup and full history.

Documentation

Kanta in JSON mode (default) stores newline-delimited records, so transaction history is viewable in any text editor; MsgPack mode uses binary records with length and checksum to guard against corruption.

S
Description
Kanta No-SQL database for async Python and frameworks such as FastAPI and Sanic
Readme
1,020 KiB
0 Stars 1 Watchers 0 Forks
Languages
Python 100%