Skip to main content

A Python-native object database

Project description

YourDB

YourDB is a lightweight, Python-native object database designed to persist and query Python objects with schema validation and SQL-like querying capabilities.

It allows developers to define entities using Python dictionaries (or class-like schemas), insert objects, and perform filtering, updating, or deleting โ€” all using native Python.


โžก๏ธ View the live documentation site deployed on Vercel: https://vercel.com/aayushman-guptas-projects/your-db-official-docs

๐Ÿ” Features

  • ๐Ÿš€ Object-Native Persistence: Your classes are the database. Use the @register_class decorator on your Python classes and store/retrieve instances directly. yourdb handles serialization automatically.
  • ๐Ÿงฌ Hybrid Schema Evolution: Effortlessly evolve your data models over time without risky migration scripts!
    • Lazy Read: Automatically upgrades old data objects in memory on-the-fly using simple @register_upgrade functions. Your application code only ever sees the latest version.
    • Eager Migration: An optional db.optimize_entity() tool safely rewrites data files on disk to the latest version for performance tuning.
  • โšก High-Performance & Thread-Safe: Built on an append-only log for fast writes and an in-memory cache for rapid reads. A robust writer-preference lock ensures data integrity under high concurrency.
  • ๐Ÿง  Advanced Querying: Go beyond simple lookups. Use a filter_dict with MongoDB-style operators like $gt, $lt, $gte, $lte, and $ne for expressive queries.
  • ๐Ÿ” Indexing: Define indexes on specific fields within your schema to significantly accelerate lookups for common queries.
  • โš™๏ธ Automatic Compaction: A background process automatically cleans up log files, removing redundant data to save space and speed up load times.
  • ๐ŸŒ Zero-Dependency: Pure Python. No external database servers to install or manage. Perfect for serverless, edge, desktop apps, or simplifying your stack.

๐Ÿ“ฆ Installation

git clone https://github.com/Dhruv251004/yourdb
cd yourdb
pip install .

๐Ÿ Follow the Quickstart guide

from yourdb import YourDB, register_class, register_upgrade

# --- 1. Define Your Data Model (Initial Version) ---
@register_class
class User:
    __version__ = 1
    def __init__(self, name, email):
        self.user_id = None
        self.name = name
        self.email = email
    def __repr__(self):
        return f"User(v{self.__version__}, id={self.user_id}, name='{self.name}', email='{self.email}')"

# --- 2. Initialize Database & Entity ---
db = YourDB("my_app_db")
user_schema = {
    'primary_key': 'user_id',
    'user_id': "int",
    'name': "str",
    'email': "str",
    'indexes': ['email'] # Index the email field
}
# Creates entity if it doesn't exist, loads if it does
db.create_entity("users", user_schema)

# --- 3. Insert Data ---
alice = User(name="Alice Smith", email="alice@example.com")
alice.user_id = 101
db.insert_into("users", alice)

# --- 4. Select Data ---
retrieved_alice = db.select_from("users", {'email': 'alice@example.com'})[0]
print(f"Found: {retrieved_alice}")

# --- 5. Evolve Schema (Example) ---
# Imagine you deploy new code with User v2 and an upgrader

@register_upgrade("User", from_version=1, to_version=2)
def upgrade_v1_to_v2(data):
    data['status'] = 'active' # Add a new field with a default
    return data

@register_class
class User: # Redefine the class in your new code
    __version__ = 2
    def __init__(self, name, email, status='active'):
        self.user_id = None
        self.name = name
        self.email = email
        self.status = status
    def __repr__(self):
         return f"User(v{self.__version__}, id={self.user_id}, name='{self.name}', email='{self.email}', status='{self.status}')"

# --- 6. Read Data After Evolution ---
# Re-initialize DB instance simulates app restart with new code
db_reloaded = YourDB("my_app_db")
alice_v2 = db_reloaded.select_from("users", {'user_id': 101})[0]
print(f"After Evolution: {alice_v2}") # Alice is now a v2 object with status='active'!

# --- 7. Optimize (Optional) ---
# db_reloaded.optimize_entity("users") # Rewrites log files to contain only v2 data

๐Ÿ“ Directory Structure

yourdb/
โ”œโ”€โ”€ .gitignore
โ”œโ”€โ”€ LICENSE
โ”œโ”€โ”€ MANIFEST.in
โ”œโ”€โ”€ pyproject.toml      # Project configuration (replaces requirements.txt)
โ”œโ”€โ”€ Readme.md
โ”‚
โ”œโ”€โ”€ test_files/         # Contains benchmark and test scripts
โ”‚   โ”œโ”€โ”€ fetch_test.py
โ”‚   โ””โ”€โ”€ main.py
โ”‚
โ””โ”€โ”€ yourdb/             # The core source code of the database package
    โ”œโ”€โ”€ __init__.py     # Package definition & public exports
    โ”œโ”€โ”€ compaction.py   # Log file compaction logic
    โ”œโ”€โ”€ entity.py       # Core storage engine, in-memory cache, indexing
    โ”œโ”€โ”€ locking.py      # Reader-Writer lock for concurrency
    โ”œโ”€โ”€ utils.py        # Serialization, validation, schema evolution helpers
    โ””โ”€โ”€ yourdb.py       # Main public API (YourDB class)

๐Ÿค Contributing & Roadmap

Contributions are welcome! Please feel free to open an issue or submit a pull request.

Our near-term roadmap includes:

  • ๐Ÿš€ Performance optimization for indexed range queries.

  • โณ Implementing Time-Travel Queries based on the log history.

Project details


Download files

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

Source Distribution

yourdb-0.2.1.tar.gz (16.1 kB view details)

Uploaded Source

Built Distribution

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

yourdb-0.2.1-py3-none-any.whl (15.5 kB view details)

Uploaded Python 3

File details

Details for the file yourdb-0.2.1.tar.gz.

File metadata

  • Download URL: yourdb-0.2.1.tar.gz
  • Upload date:
  • Size: 16.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.4

File hashes

Hashes for yourdb-0.2.1.tar.gz
Algorithm Hash digest
SHA256 c5c24d8cd0b75a5f226dbbe4f7a2d4f0e9cf7acacc40ceb6391c74d6c68190d9
MD5 7f8681a6a106bceff0631dfcfd15d32d
BLAKE2b-256 66c41f6784478f199a2286d9da594f22e6850109b3a370eff40d62f29e9cf395

See more details on using hashes here.

File details

Details for the file yourdb-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: yourdb-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 15.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.4

File hashes

Hashes for yourdb-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 864893bc2a52fcfcd33056ac5badc65d9684bbce02fbd0aef695843372a4f490
MD5 e3eda63a23f74427422a7c562740d306
BLAKE2b-256 62e0e0c328bb1270a12f4e6d5c7bc0f9acd4c11fb4d083aa42b90ec1a5e0b64c

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