Skip to main content

A lightweight document storage system with SQLAlchemy dialect support. Where your data or honey, hibernates.

Project description

🐻📚 Bear-Shelf

pypi version

The shelf where your data hibernates. 🐻💤

A lightweight document storage system with SQLAlchemy dialect support. Store your data in JSONL, JSON, TOML, YAML, XML, MessagePack, or in-memory formats with a clean, type-safe interface — no SQLite required.

Features

  • 🔌 SQLAlchemy Integration: A real SQLAlchemy dialect — use familiar ORM syntax over plain files
  • 📦 Multiple Backends: JSONL (default), JSON, TOML, YAML, XML, MessagePack, or in-memory
  • 🧰 Two Front Doors: Use the raw bearshelf:// dialect, or the batteries-included DatabaseManager API
  • 🔒 Type-Safe: Full type hints, Pydantic models, and custom column types (Mapped[Path], Mapped[EpochTimestamp])
  • 📝 Write-Ahead Logging: Optional WAL with buffered/immediate flush modes for durability and bulk-write speed
  • Compiled Core: Hot paths implemented in C++23 (pybind11) and Cython (Goal is to only have it in C++)

Installation

With uv:

uv add bear-shelf
# or
uv pip install bear-shelf

Minimum Python: 3.12.

Quick Start

Bear-Shelf gives you two equally first-class ways to work with your data. Pick whichever fits how you think.

Option A — Raw SQLAlchemy dialect

If you already know SQLAlchemy, just point an engine at a file. The backend is chosen by the file extension.

from sqlalchemy import create_engine
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column, Session

engine = create_engine("bearshelf:///path/to/users.jsonl")

class Base(DeclarativeBase): ...

class User(Base):
    __tablename__ = "users"
    id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
    name: Mapped[str]
    email: Mapped[str] = mapped_column(unique=True)

Base.metadata.create_all(engine)

with Session(engine) as session:
    session.add(User(name="Bear", email="bear@shelf.com"))
    session.commit()

Option B — DatabaseManager API

A higher-level wrapper that handles the engine, sessions, and table registration for you, and returns convenient per-table handles.

from pathlib import Path
from sqlalchemy.orm import Mapped, mapped_column
from bear_shelf.database import BearShelfDB

# get_base() returns a declarative base wired with Bear-Shelf's custom types
Base = BearShelfDB.get_base()

class User(Base):
    __tablename__ = "users"
    id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
    name: Mapped[str]
    config_path: Mapped[Path]            # stored as text, returned as a Path

db = BearShelfDB(path="users.jsonl")

# Build only the tables you want; bare create_tables() builds them all.
db.include(User).create_tables()

with db.open_session() as session:
    session.add(User(name="Bear", config_path=Path("~/cfg.toml")))

users = db.get_all(User)                 # [User(name='Bear', ...)]
count = db.count(User)                   # 1
db.close()

include(*models) is handy when one declarative base defines many tables but a given database should only create a subset (e.g. test isolation). Creating a subset whose foreign keys reference tables you left out fails loudly with a message telling you what to add.

🎨 Custom Column Types

Annotate columns with rich Python types and Bear-Shelf converts them at the storage boundary — no manual (de)serialization:

Annotation Stored as Returned as
Mapped[Path] text pathlib.Path
Mapped[EpochTimestamp] integer bear_epoch_time.EpochTimestamp

These are registered on the base returned by BearShelfDB.get_base().

🎯 Storage Backends

The backend is auto-detected from the file extension (or pass storage=/schema=):

Backend URL / extension
JSONL (default) bearshelf:///data.jsonl
JSON bearshelf:///data.json
TOML bearshelf:///data.toml
YAML bearshelf:///data.yaml
XML bearshelf:///data.xml
MessagePack bearshelf:///data.msgpack
Memory bearshelf:///:memory:

📝 Write-Ahead Logging

For bulk writes and crash safety, enable WAL. In BUFFERED mode, inserts append to a log and a background thread checkpoints to the main file — turning thousands of slow full-file rewrites into fast appends.

from bear_shelf.datastore import BearBase, Columns
from bear_shelf.datastore.wal.config import WALConfig

db = BearBase(
    "events.json",
    storage="json",
    enable_wal=True,
    wal_config=WALConfig.high_throughput(),   # or .immediate() for fsync-per-op
)

db.create_table("events", columns=[
    Columns(name="id", type="int", primary_key=True),
    Columns(name="event_type", type="str"),
])

table = db.table("events")
table.insert_all([{"id": i, "event_type": "click"} for i in range(5000)])
db.close()

See examples/wal_example.py for buffered/immediate modes, custom tuning, manual checkpointing, and crash recovery, and examples/foreign_key_example.py for relationships.

🐻 About

Built with ❤️ by Bear. Part of the Bear-verse ecosystem:

  • funcy_bear: functional programming & type introspection utilities
  • bear-epoch-time: precision-aware timestamp handling
  • codec_cub: Codec utilties for the various storages used in Bear Shelf

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.

bear_shelf-0.3.34-cp314-cp314-win_amd64.whl (157.1 kB view details)

Uploaded CPython 3.14Windows x86-64

bear_shelf-0.3.34-cp314-cp314-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (2.7 MB view details)

Uploaded CPython 3.14manylinux: glibc 2.27+ x86-64manylinux: glibc 2.28+ x86-64

bear_shelf-0.3.34-cp314-cp314-manylinux_2_26_aarch64.manylinux_2_28_aarch64.whl (2.7 MB view details)

Uploaded CPython 3.14manylinux: glibc 2.26+ ARM64manylinux: glibc 2.28+ ARM64

bear_shelf-0.3.34-cp314-cp314-macosx_11_0_arm64.whl (385.0 kB view details)

Uploaded CPython 3.14macOS 11.0+ ARM64

bear_shelf-0.3.34-cp313-cp313-win_amd64.whl (157.3 kB view details)

Uploaded CPython 3.13Windows x86-64

bear_shelf-0.3.34-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (2.7 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.27+ x86-64manylinux: glibc 2.28+ x86-64

bear_shelf-0.3.34-cp313-cp313-manylinux_2_26_aarch64.manylinux_2_28_aarch64.whl (2.7 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.26+ ARM64manylinux: glibc 2.28+ ARM64

bear_shelf-0.3.34-cp313-cp313-macosx_11_0_arm64.whl (384.7 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

File details

Details for the file bear_shelf-0.3.34-cp314-cp314-win_amd64.whl.

File metadata

  • Download URL: bear_shelf-0.3.34-cp314-cp314-win_amd64.whl
  • Upload date:
  • Size: 157.1 kB
  • Tags: CPython 3.14, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for bear_shelf-0.3.34-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 07ab4c10ab9122afc511f1a2c78dcb4c3e6d50379c46e85137bd93a46e63f757
MD5 47072d7642b06711d8b51fbec0769fd4
BLAKE2b-256 c7e91b220729b47c96079cc2b777ae3f5de22f0ea8d48c1ca0f99ec6590e1228

See more details on using hashes here.

Provenance

The following attestation bundles were made for bear_shelf-0.3.34-cp314-cp314-win_amd64.whl:

Publisher: build-wheels.yml on sicksubroutine/bear-shelf

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file bear_shelf-0.3.34-cp314-cp314-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for bear_shelf-0.3.34-cp314-cp314-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 97c1355311195cafd389402630c880ef40d74ded5789dd6592be25d3ee310a50
MD5 0de191b01ebfcbe913ce06966b8e2af8
BLAKE2b-256 bb6cf2b6b75fafa8ca9d2b03ae7f7ce5d2aeff91fd2fb36a7871883c2965d440

See more details on using hashes here.

Provenance

The following attestation bundles were made for bear_shelf-0.3.34-cp314-cp314-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl:

Publisher: build-wheels.yml on sicksubroutine/bear-shelf

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file bear_shelf-0.3.34-cp314-cp314-manylinux_2_26_aarch64.manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for bear_shelf-0.3.34-cp314-cp314-manylinux_2_26_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 c36ec379f74c63b0a57f11c7c8018652afa5a0da5e0dc743c946e744a8a11d06
MD5 716a837c4c6b2e8f7cf1645ad0bc0553
BLAKE2b-256 47e0687b9fd50d338e572c537164ff9f9e81bb8ad044c922c5197051463221c6

See more details on using hashes here.

Provenance

The following attestation bundles were made for bear_shelf-0.3.34-cp314-cp314-manylinux_2_26_aarch64.manylinux_2_28_aarch64.whl:

Publisher: build-wheels.yml on sicksubroutine/bear-shelf

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file bear_shelf-0.3.34-cp314-cp314-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for bear_shelf-0.3.34-cp314-cp314-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 7a92ca25e644e223d453696a4a8d444fea2d5d1b5d40aab140fb73ec20e54bfa
MD5 6be830d4d45a82873320d7f0133fadd9
BLAKE2b-256 98c9741d4c6e029b5bde320f374061fb809c2c28fb90ec44287871661c626f6f

See more details on using hashes here.

Provenance

The following attestation bundles were made for bear_shelf-0.3.34-cp314-cp314-macosx_11_0_arm64.whl:

Publisher: build-wheels.yml on sicksubroutine/bear-shelf

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file bear_shelf-0.3.34-cp313-cp313-win_amd64.whl.

File metadata

  • Download URL: bear_shelf-0.3.34-cp313-cp313-win_amd64.whl
  • Upload date:
  • Size: 157.3 kB
  • Tags: CPython 3.13, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for bear_shelf-0.3.34-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 ab22b24d6fb2e0ecc80fb1e9920f8aa8ad7deca555434d529a0147107638bad2
MD5 af8de757b226c586008f179cbc903fcc
BLAKE2b-256 7d590182c61ba7d1edf5d93a3ac391349cedb064e53ca9f9fe93f88a85e04ad7

See more details on using hashes here.

Provenance

The following attestation bundles were made for bear_shelf-0.3.34-cp313-cp313-win_amd64.whl:

Publisher: build-wheels.yml on sicksubroutine/bear-shelf

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file bear_shelf-0.3.34-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for bear_shelf-0.3.34-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 00016c94116f3c3f1b519a45811c78981b87ac3dac105b84c56ec44b1a40aba2
MD5 b4c32bd6d884e5afffcd418d766af767
BLAKE2b-256 c7eb06df587dd301f9bc3bb43cf4bb67cccbd6ea4433a8c51dc2653c66142f67

See more details on using hashes here.

Provenance

The following attestation bundles were made for bear_shelf-0.3.34-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl:

Publisher: build-wheels.yml on sicksubroutine/bear-shelf

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file bear_shelf-0.3.34-cp313-cp313-manylinux_2_26_aarch64.manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for bear_shelf-0.3.34-cp313-cp313-manylinux_2_26_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 4c3bb1cf866ec5fd627d426580d5e7b2be2fd744062894a5ff8c2bf76003324b
MD5 b2e057366eb947265de96521f7db0505
BLAKE2b-256 83134bb103b9d18025ef4f16e49175ae323a1cebb0721f1494a08910cc2dc84c

See more details on using hashes here.

Provenance

The following attestation bundles were made for bear_shelf-0.3.34-cp313-cp313-manylinux_2_26_aarch64.manylinux_2_28_aarch64.whl:

Publisher: build-wheels.yml on sicksubroutine/bear-shelf

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file bear_shelf-0.3.34-cp313-cp313-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for bear_shelf-0.3.34-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 86b13cd0d22239e45e9cffc155f409d78fdecbd67252195ec73daf7f77e318f2
MD5 10d1db5359d5fa514c1ea2b698da0d62
BLAKE2b-256 1eec507c248d20d186d8aa91afafa4d6019bcbcf3ba8b43d6e6e91729dcfcd59

See more details on using hashes here.

Provenance

The following attestation bundles were made for bear_shelf-0.3.34-cp313-cp313-macosx_11_0_arm64.whl:

Publisher: build-wheels.yml on sicksubroutine/bear-shelf

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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