Metadata-Version: 2.4
Name: datastreamdb
Version: 0.2.0
Summary: Python bindings for datastreamdb (embedded graph database)
License: Apache-2.0
Requires-Python: >=3.10
Description-Content-Type: text/markdown; charset=UTF-8; variant=GFM

# datastreamdb

Python bindings for DataStreamDB — embedded, multi-instance graph database.
This package ships an abi3 wheel for Python 3.10+ on Linux x64 (glibc).

```sh
pip install datastreamdb
```

Full developer guide: <https://fab679.github.io/datastreamdb/embedding/python/>

## Quick start

```python
import json
from datastreamdb import Instance, version

print(version())  # "0.2.0"

db = Instance("{}")  # isolated in-memory instance
db.put("user:1", json.dumps({"name": "Ada", "age": 36}))

raw = db.get("user:1")
print(None if raw is None else json.loads(raw)["name"])  # Ada
```

Persistent instance:

```python
db = Instance(json.dumps({
    "data_dir": "/var/lib/app/graph-a",
    "tenant": "app",
}))
```

`data_dir` takes an exclusive lock; a second open of the same directory fails
until the first closes. `tenant` is a label, not an isolation boundary.

## Query with prefix scans

```python
result = json.loads(db.execute(json.dumps({
    "op": "scan",
    "prefix": "user:",
    "limit": 100,
})))

for pair in result["pairs"]:
    print(pair["key"], json.loads(pair["value"]))
```

Keys are returned in lexicographic order. `limit` defaults to 1000 and is
clamped to 100,000. There is no continuation cursor in 0.2.0.

## Atomic writes

```python
result = json.loads(db.execute(json.dumps({
    "op": "batch",
    "puts": [
        {"key": "user:1", "value": json.dumps({"name": "Ada"})},
        {"key": "user:2", "value": json.dumps({"name": "Bob"})},
    ],
    "deletes": ["user:3"],
})))

print(result["applied"])  # 3
```

## API

| Method | Description |
|---|---|
| `Instance(config_json)` | Open a memory or persistent instance. |
| `id()` | Unique instance id. |
| `backend()` | `"memory"` or `"fjall"`. |
| `put(key, value)` | Upsert a UTF-8 value. |
| `get(key)` | Read a UTF-8 value, or `None`. |
| `execute(request)` | `ping`, `put`, `get`, `delete`, `scan`, `batch`, `flush`. |
| `version()` | Binding/engine version. |

Values are strings in the raw KV API. Serialize JSON documents with
`json.dumps`; base64-encode binary data. The GIL is released around every
engine call.

## DQL and GraphQL

```python
db.dql_alter("name: string @index(exact) .\nfriend: [uid] @reverse .\ntype Person {\n name\n friend\n}")
db.dql_mutate('{ set { _:a <name> "Ada" .\n_:b <name> "Bob" .\n_:a <friend> _:b .\n} }')
print(db.dql_query('{ people(func: eq(name, "Ada")) { name friend { name } } }'))

db.set_graphql_schema("type Person { id: ID! name: String @search(by: [term]) }")
print(db.graphql("{ queryPerson { name } }"))
```

GraphQL subscriptions are poll-based via `graphql_subscribe`, `graphql_next`,
and `graphql_unsubscribe`. Embedded graph mode is single-node; see the full
guide at <https://fab679.github.io/datastreamdb/embedding/queries/>.

## Build and test

```sh
uv venv .venv
uv pip install --python .venv/bin/python maturin pytest
.venv/bin/maturin develop
.venv/bin/pytest -q
```

Build a release wheel with `.venv/bin/maturin build --release`. The extension
uses `abi3-py310`, so one wheel covers Python 3.10 and newer.

