Skip to main content

Python bindings for the Sombra graph database.

Project description

sombra

Python bindings for the Sombra graph database. The package exposes the Stage 8 fluent query builder together with lightweight CRUD helpers that forward to the Rust planner/executor through pyo3. Native failures are surfaced as typed exceptions (see wrap_native_error), and both Database and query streams provide context managers to ensure handles are released promptly.

Typed facade

For schema-aware CRUD helpers that mirror the TypeScript ergonomics, use the SombraDB wrapper under sombra.typed:

from typing import TypedDict
from typing_extensions import Literal

from sombra.typed import NodeSchema, SombraDB, TypedGraphSchema


class PersonProps(TypedDict):
    name: str
    age: int


class PersonNode(NodeSchema):
    properties: PersonProps


class GraphSchema(TypedGraphSchema):
    nodes: dict[str, PersonNode]
    edges: dict[str, TypedDict("KnowsEdge", {"from": Literal["Person"], "to": Literal["Person"], "properties": dict})]


schema: GraphSchema = {
    "nodes": {"Person": {"properties": {"name": "", "age": 0}}},
    "edges": {"KNOWS": {"from": "Person", "to": "Person", "properties": {}}},
}

db = SombraDB("/tmp/typed.db", schema=schema)
friend = db.add_node("Person", {"name": "Ada", "age": 33})

See examples/typed.py for a longer walkthrough that mirrors the Node.js demo.

Quick start

from sombra import Database
from sombra.query import eq

db = Database.open("/tmp/sombra.db")
db.seed_demo()

rows = db.query().nodes("User").where(eq("name", "Ada")).select("name").execute()
print(rows[0]["name"])

# Need request ids or feature flags? Ask for the metadata envelope:
payload = (
    db.query().nodes("User").request_id("example").select("name").execute(with_meta=True)
)
print(payload.request_id(), len(payload.rows()))

user_id = db.create_node("User", {"name": "New User"})
db.update_node(user_id, set_props={"bio": "updated"})
db.delete_node(user_id, cascade=True)

# Clean up native handles explicitly or via context managers
db.close()

async def stream_users() -> None:
    async with db.query().nodes("User").stream() as stream:
        async for row in stream:
            print(row["n0"])

Run the end-to-end example in examples/crud.py to see the workflow in action:

python examples/crud.py

Performance

The Python bindings have minimal overhead compared to the Rust core (~3-5%). Benchmark results on a typical developer machine:

Operation Throughput
Node + edge creation ~9,000 ops/sec
Point reads ~20,000 reads/sec

Tips for optimal performance:

  1. Use the builder for bulk operationsdb.create() batches all nodes and edges into a single transaction, which is significantly faster than individual create_node/create_edge calls.

  2. Use release builds – If building from source, always use maturin develop --release. Debug builds are significantly slower.

  3. Tune synchronous mode – For write-heavy workloads where durability can be relaxed, set synchronous="normal" in options. The default "full" ensures every commit is fsync'd.

  4. Use direct lookups when possibledb.get_node_record(id) is faster than running a query for single-node fetches.

Benchmarks

benchmarks/crud.py performs simple create/read/update/delete micro-benchmarks against a throwaway database:

python benchmarks/crud.py

benchmarks/realistic_bench.py runs a larger benchmark comparable to the Node.js and Rust benchmarks:

python benchmarks/realistic_bench.py

Predicate builders accept timezone-aware datetime objects directly and reject naive datetimes so callers do not need to juggle epochs. Property projections ({"var": "a", "prop": "name", "as": "alias"}) return scalar columns when you only need a subset of properties.

Development

Install the native module via maturin:

cd bindings/python
maturin develop --release
python -m pytest tests

Use maturin build to produce distributable wheels once you are ready to publish the bindings.

Release Workflow

  1. Land a feat commit that touches bindings/python/** whenever you want the PyO3 wheel to bump its minor version. Release Please maps that commit to a new sombrapy release PR.
  2. Before merging the PR, build and test the wheel locally: maturin develop, pytest -q, and maturin build --release (or maturin publish --dry-run).
  3. Merge the release PR to tag the repo; the publish-python.yml workflow uploads the wheels to PyPI. Re-run maturin publish manually only if the workflow fails.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

sombra-0.4.0-cp38-abi3-win_amd64.whl (1.1 MB view details)

Uploaded CPython 3.8+Windows x86-64

sombra-0.4.0-cp38-abi3-manylinux_2_34_x86_64.whl (1.4 MB view details)

Uploaded CPython 3.8+manylinux: glibc 2.34+ x86-64

sombra-0.4.0-cp38-abi3-macosx_11_0_arm64.whl (1.2 MB view details)

Uploaded CPython 3.8+macOS 11.0+ ARM64

File details

Details for the file sombra-0.4.0-cp38-abi3-win_amd64.whl.

File metadata

  • Download URL: sombra-0.4.0-cp38-abi3-win_amd64.whl
  • Upload date:
  • Size: 1.1 MB
  • Tags: CPython 3.8+, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for sombra-0.4.0-cp38-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 03d83f4e118cb23ee36263e39bab68f59b6cfec114dac894ef6cade312f66fca
MD5 cbb732c0498a56426d8a1ee93125662e
BLAKE2b-256 46da8ab1f8584bb749cd2ea7085a8f15ceb1b395739b72b57fa829998b16b0f2

See more details on using hashes here.

File details

Details for the file sombra-0.4.0-cp38-abi3-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for sombra-0.4.0-cp38-abi3-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 681d5ad8cd49a99d1431a7d659abbede2968a0441d6dfc04b81a583b4f50729d
MD5 bfa79bcdb4b1f6522b44c0f36118d4ea
BLAKE2b-256 8874e4ed0a19ecada1f50dad31007916a9319ff850b5c7a3c7201b705eca6277

See more details on using hashes here.

File details

Details for the file sombra-0.4.0-cp38-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for sombra-0.4.0-cp38-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 8bdbde0a896efb34a41b6cb42466d16fab0a21653fede3399d08ac23d8525521
MD5 210589ac5c405b6739ae01390c8a0953
BLAKE2b-256 b6e67a01cc0c2adab5855984bb6ea7316301f8c8878051123cc7165ed6eb2e36

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page