Python

Python client for ChronDB, auto-generated from the Rust SDK via UniFFIarrow-up-right.

Requirements

  • Python 3.8+

Installation

Install directly from the latest GitHub releasearrow-up-right:

macOS (Apple Silicon):

pip install https://github.com/avelino/chrondb/releases/download/latest/chrondb-latest-py3-none-macosx_14_0_arm64.whl

Linux (x86_64):

pip install https://github.com/avelino/chrondb/releases/download/latest/chrondb-latest-py3-none-manylinux_2_35_x86_64.whl

The wheel already includes the native shared library — no extra configuration needed.

Library path (advanced)

If you need to override the bundled library (e.g., using a custom build), the binding searches in this order:

  1. CHRONDB_LIB_PATH — full path to the library file

  2. CHRONDB_LIB_DIR — directory containing the library

  3. Bundled lib/ inside the package

  4. System paths: /usr/local/lib, /usr/lib

Quick Start

Legacy API (deprecated): ChronDB("/tmp/data", "/tmp/index") still works but is deprecated. Use the single-path form instead.

API Reference

ChronDB(db_path: str, idle_timeout: float = None)

Opens a database connection using a single directory path. Data and index are stored in subdirectories automatically.

Parameter
Type
Description

db_path

str

Path for the database (data and index stored inside)

idle_timeout

Optional[float]

Seconds of inactivity before suspending the GraalVM isolate. None (default) keeps it alive for the entire lifetime.

When idle_timeout is set, the GraalVM isolate is automatically closed after the specified seconds of inactivity, freeing CPU and memory. The next operation transparently reopens it. See Idle Timeout for details.

Raises: ChronDBError if the database cannot be opened.

Implements the context manager protocol (__enter__ / __exit__), calling close() automatically on exit.

Legacy: ChronDB(data_path: str, index_path: str, idle_timeout: float = None)

Deprecated. The two-path constructor still works but is deprecated. Use the single-path form above.

Parameter
Type
Description

data_path

str

Path for the Git repository (data storage)

index_path

str

Path for the Lucene index

idle_timeout

Optional[float]

Seconds of inactivity before suspending the GraalVM isolate


put(id, doc, branch=None) -> Dict[str, Any]

Saves a document.

Parameter
Type
Description

id

str

Document ID (e.g., "user:1")

doc

Dict[str, Any]

Document data

branch

Optional[str]

Branch name (None for default)

Returns: The saved document as a dictionary.

Raises: ChronDBError on failure.


get(id, branch=None) -> Dict[str, Any]

Retrieves a document by ID.

Parameter
Type
Description

id

str

Document ID

branch

Optional[str]

Branch name

Returns: The document as a dictionary.

Raises: DocumentNotFoundError if not found, ChronDBError on failure.


delete(id, branch=None) -> bool

Deletes a document by ID.

Parameter
Type
Description

id

str

Document ID

branch

Optional[str]

Branch name

Returns: True if deleted.

Raises: DocumentNotFoundError if not found, ChronDBError on failure.


list_by_prefix(prefix, branch=None) -> List[Dict[str, Any]]

Lists documents whose IDs start with the given prefix.

Parameter
Type
Description

prefix

str

ID prefix to match

branch

Optional[str]

Branch name

Returns: List of matching documents (empty list if none).


list_by_table(table, branch=None) -> List[Dict[str, Any]]

Lists all documents in a table.

Parameter
Type
Description

table

str

Table name

branch

Optional[str]

Branch name

Returns: List of matching documents (empty list if none).


history(id, branch=None) -> List[Dict[str, Any]]

Returns the change history of a document.

Parameter
Type
Description

id

str

Document ID

branch

Optional[str]

Branch name

Returns: List of history entries (empty list if none).


query(query, branch=None) -> Dict[str, Any]

Executes a query against the Lucene index.

Parameter
Type
Description

query

Dict[str, Any]

Query in Lucene AST format

branch

Optional[str]

Branch name

Returns: Results dict with keys results, total, limit, offset.

Raises: ChronDBError on failure.


execute(sql, branch=None) -> Dict[str, Any]

Executes a SQL query directly against the database without needing a running server.

Parameter
Type
Description

sql

str

SQL query string

branch

Optional[str]

Branch name (None for default)

Returns: Result dict with keys type, columns, rows, count.

Raises: ChronDBError on failure.


close()

Closes the database connection and releases native resources. Called automatically when using the context manager.

Document ID Convention

Documents use the format table:id:

Internally, user:123 is stored as user/user_COLON_123.json in the Git repository.

Use list_by_table("user") to retrieve all documents in the user table.

Error Handling

Exception Hierarchy

ChronDBError

Base exception for all ChronDB errors (connection failures, operation errors, native library issues).

DocumentNotFoundError

Raised by get() and delete() when the target document does not exist.

Example

Examples

Full CRUD

Query

History (Time Travel)

SQL Queries

Execute SQL queries directly without needing a running server:

Idle Timeout (long-running services)

ChronDB loads a GraalVM native-image shared library whose internal threads consume CPU even when no operations are in flight. For long-running services with sporadic database access, use idle_timeout to automatically suspend the isolate when idle:

When to use:

  • Long-running daemons that write to ChronDB intermittently (e.g., audit logging, MCP servers)

  • Services where memory/CPU usage matters during idle periods

When NOT to use:

  • Short-lived scripts (just use ChronDB(...) without timeout)

  • High-throughput services with constant database access (the isolate would never go idle)

Pytest Fixture

Building from Source

Last updated

Was this helpful?