Skip to main content

NADB

NADB is a Python key-value store for application data that needs more than a dictionary: persistence, tags, TTL, transactions, backups, indexing, typed values, streaming, optional encryption, and multiple storage backends.

Tests codecov

Current release: 0.3.0

Highlights

  • Text, bytes, JSON, bytearray, and memoryview values.
  • StoredValue return mode with bytes, text, metadata, content type, TTL, and ETag/checksum.
  • Backends: filesystem, Redis, in-memory, SQLite, and S3-compatible object storage.
  • Hierarchical namespaces and paginated key scanning.
  • Batch operations, conditional writes, compare-and-set, counters, and touch.
  • TTL by seconds, datetime, or timedelta, plus persist_ttl().
  • Query builder, custom secondary indexes, ordering, filtering, and tag queries.
  • Watchers/events, validation hooks, and transformation hooks.
  • Optional at-rest encryption envelope.
  • Streaming readers/writers and chunked blobs for large binary values.
  • Full, incremental, JSONL stream, and tar backup exports with retention pruning.
  • CLI: nadb set/get/keys/tags/export/import/compact/verify/cleanup-expired.
  • Optional OpenTelemetry spans plus lightweight built-in operation counters.
  • Safer behavior: strict backend loading, checksums, redacted logs, owner-only filesystem permissions, Redis SCAN paths.

Install

pip install nadb

Optional extras:

pip install "nadb[redis]"
pip install "nadb[s3]"
pip install "nadb[otel]"
pip install "nadb[dev,redis]"

Quick Start

from nadb import open_store

with open_store(data_folder_path="./data", db="app", namespace="prod") as store:
    store.set_text("user:1:name", "Alice", tags=["user"])
    store.set_json("user:1:settings", {"theme": "dark"}, tags=["config"])
    store.set_bytes("user:1:avatar", b"\x89PNG...", tags=["asset"])

    assert store.get_text("user:1:name") == "Alice"
    assert store.get_json("user:1:settings") == {"theme": "dark"}

Rich Values

from nadb import KeyValueStore

store = KeyValueStore(return_type="stored")
store.set_text("message", "hello")

value = store.get("message")
assert value.text == "hello"
print(value.content_type, value.etag, value.metadata)

Backends

KeyValueStore(storage_backend="fs", data_folder_path="./data")
KeyValueStore(storage_backend="memory")
KeyValueStore(storage_backend="sqlite", storage_options={"database_name": "app.sqlite3"})
KeyValueStore(storage_backend="redis", storage_options={"host": "localhost", "key_prefix": "myapp:nadb"})
KeyValueStore(storage_backend="s3", storage_options={"bucket": "my-bucket", "prefix": "nadb"})

S3 uses boto3 when installed. Without boto3, it uses a local S3-shaped development fallback.

Data Operations

store.set_many({"a": "1", "b": "2"})
store.get_many(["a", "b"])
store.exists_many(["a", "missing"])
store.delete_many(["a", "b"])

etag = store.get_with_metadata("key")["metadata"]["checksum"]
store.compare_and_set("key", etag, "new-value")
store.set_if_absent("lock", "owner")
store.set_if_exists("lock", "new-owner")
store.incr("counter")
store.decr("counter")

TTL

from datetime import datetime, timedelta

store.set_with_ttl("cache:item", "fresh", ttl_seconds=60)
store.set_with_expires_at("session", "data", datetime.now() + timedelta(hours=1))
store.set_with_timedelta("job", "queued", timedelta(minutes=15))
store.ttl("job")
store.persist_ttl("job")

Querying

store.set_json("user:1", {"status": "active", "plan": "pro"}, tags=["user"])
store.create_index("status")

active_keys = store.query_index("status", "active")
query_keys = store.query().tag("user").where("content_type", "application/json").limit(50).keys()
pages = list(store.scan_keys(prefix="user:", page_size=100))

Hooks, Events, Encryption

store = KeyValueStore(encryption_key="local-secret")

store.watch("*", lambda **event: print(event["event"], event["key"]))
store.add_validator(lambda key, value, meta: None)
store.add_transformer(lambda key, value, meta: value.strip())

Large Values

with store.open_writer("video:raw", chunk_size=1024 * 1024) as writer:
    writer.write(b"...large payload...")

payload = store.get_chunked("video:raw")

Backups

backup = store.create_backup("daily")
store.create_incremental_backup(backup.backup_id, "daily-inc")
store.export_backup_stream("backup.jsonl")
store.export_backup_tar("backup.tar")
store.prune_backups(keep_last=10, keep_days=30)

CLI

nadb --data ./data --db app --namespace prod set hello world
nadb --data ./data --db app --namespace prod get hello
nadb --data ./data --db app --namespace prod keys
nadb --data ./data --db app --namespace prod export backup.jsonl

Testing

make test
make test-advanced
make test-redis      # requires Redis
make test-all        # requires Redis

Redis tests skip when Redis is unavailable unless NADB_REQUIRE_REDIS=1 is set.

Documentation

See docs/index.md.

License

MIT. See LICENSE.

Metadata

Release files for nadb 0.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for nadb 0.3.0
File Size Uploaded Provenance Actions
nadb-0.3.0.tar.gz 153.5 kB Without provenance Details for nadb-0.3.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for nadb 0.3.0
File Interpreter ABI Platform Actions
nadb-0.3.0-py3-none-any.whl Python 3 none any Details for nadb-0.3.0-py3-none-any.whl

Total release size: 215.6 kB

Release files / nadb-0.3.0.tar.gz

Download URL nadb-0.3.0.tar.gz
Size 153.5 kB
Tags Source
SHA-256 checksum
How to use checksums
8183481d12701fcefe2c2cd1191df5a8721fa4d6f56777da3cb7d8cbe4edc477
BLAKE2b-256 checksum
How to use checksums
cd62628452320d18cf0d131929e5812c7d6f949383d36240866cddb1475a8126
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.8

Release files / nadb-0.3.0-py3-none-any.whl

Download URL nadb-0.3.0-py3-none-any.whl
Size 62.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
988cd1caf0f4f49600d49b26d7955e8b03530584eb2d53188489e37c1b5563a3
BLAKE2b-256 checksum
How to use checksums
e4f84ca425f9114bc9aaf3b078149659f11e5eebb43f06bdbedc2a6565fb5b42
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.8

Security for nadb 0.3.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page