Skip to main content

A lightweight document-oriented database using JSON for data persistence.

Project description

Owi JSONDB

PyPI version License: MIT Python Versions GitHub

Owi JSONDB is a lightweight, document-oriented database engine designed for Python applications that need a simple, schema-less data store without the overhead of a full database server. Inspired by MongoDB, it provides a familiar API, supports nested queries, indexing, and powerful aggregation tools—all using standard JSON files for persistence.


🚀 Key Features

  • 📂 Document-Oriented: Store data as flexible, schema-less JSON documents.
  • 🆔 Auto-ID: Automatic UUID generation for every document (custom IDs also supported).
  • 🔍 Advanced Querying: Support for nested field lookups (e.g., user.profile.city) and rich query operators.
  • ⚡ Fast Performance: In-memory data processing with disk-based indexing for optimized lookups.
  • 📊 Aggregations: Built-in support for GROUP BY and numeric aggregations (sum, avg, min, max).
  • 🛡️ Simple & Reliable: Pure Python implementation with automatic data persistence on writes.

📦 Supported Data Types

Owi JSONDB supports all standard JSON-serializable Python types:

  • Primitives: str, int, float, bool, None.
  • Collections: list (Arrays) and dict (Objects).

🛠️ Installation

pip install owi-jsondb

Or using uv:

uv add owi-jsondb

📖 Complete Use Case Guide

1. Database & Collection Management

Initialize your database and access collections.

from owi_jsondb import JSONDatabase

# Initialize with a custom storage path (defaults to "storage")
db = JSONDatabase(base_path="my_app_data")

# Get or create a collection
users = db.get_collection("users")
products = db.get_collection("products")

2. Inserting Documents

Supports automatic ID generation or manual ID assignment.

# Insert with auto-generated UUID
users.insert_one({"name": "Alice", "age": 30})

# Insert with a custom manual _id
users.insert_one({"_id": "user_101", "name": "Bob", "role": "admin"})

3. Finding Documents

Powerful filtering with support for nested fields.

# Find all documents
all_users = users.find()

# Find with exact match
admins = users.find({"role": "admin"})

# Find ONE document
bob = users.find_one({"_id": "user_101"})

# Deeply nested field lookup
uk_users = users.find({"address.country": "UK"})

4. Advanced Query Operators

Filter data using MongoDB-like operators.

# Greater Than / Less Than
young_users = users.find({"age": {"$lt": 25}})
senior_users = users.find({"age": {"$gte": 65}})

# Not Equal
active_users = users.find({"status": {"$ne": "banned"}})

# In List
specific_groups = users.find({"group": {"$in": ["A", "C"]}})

# Regular Expressions (Case-insensitive search)
smiths = users.find({"name": {"$regex": "(?i)smith"}})

# Multiple operators on one field
filtered = users.find({"score": {"$gt": 50, "$lt": 100}})

5. Updating Data

Modify existing documents using filters.

# Update a single document's field
users.update({"_id": "user_101"}, {"role": "super-admin"})

# Bulk update all matching documents
users.update({"status": "active"}, {"last_login": "2024-01-01"})

6. Deleting Data

Remove documents selectively.

# Delete a specific document
users.delete({"_id": "user_101"})

# Bulk delete
users.delete({"age": {"$lt": 18}})

# Delete all documents in a collection
users.delete({})

7. Indexing for Speed

Significantly improve performance for large datasets.

# Create index on the 'email' field
users.create_index("email")

# Create index on a NESTED field
users.create_index("metadata.auth_id")

# Queries on these fields will now use the fast index lookup
user = users.find_one({"email": "alice@example.com"})

💡 Pro Tip: Owi JSONDB maintains index files (.index.json) automatically. Calling create_index() is idempotent and can be safely called every time your app starts.


📝 Simple Application Example: Todo List

from owi_jsondb import JSONDatabase

# Setup
db = JSONDatabase(base_path="todo_app")
tasks = db.get_collection("tasks")
tasks.create_index("name") # create index

# Add tasks
tasks.insert_one({"title": "Record Demo", "priority": 1, "completed": False})
tasks.insert_one({"title": "Publish Package", "priority": 0, "completed": False})

# Mark task as completed
tasks.update({"title": "Record Demo"}, {"completed": True})

# Get pending high-priority tasks
high_priority = tasks.find({"priority": {"$lt": 1}, "completed": False})
print(f"Pending High Priority: {[t['title'] for t in high_priority]}")

📊 Aggregation & Grouping

Group By

# Group documents by city
by_city = users.group_by("address.city")

Numeric Aggregations

# Supported: sum, avg, min, max
total_score = users.aggregate("score", "sum")
average_age = users.aggregate("age", "avg")
oldest_user = users.aggregate("age", "max")
youngest_user = users.aggregate("age", "min")

🔍 Operator Summary Table

Operator Function Example Usage
$gt Greater than {"age": {"$gt": 21}}
$lt Less than {"price": {"$lt": 9.99}}
$gte Greater or equal {"score": {"$gte": 50}}
$lte Less or equal {"stock": {"$lte": 10}}
$ne Not equal {"type": {"$ne": "internal"}}
$in Exists in list {"tags": {"$in": ["python", "db"]}}
$regex Regex match {"name": {"$regex": "^A.*"}}

📂 API Reference

JSONDatabase

  • JSONDatabase(base_path="storage"): Initialize database directory.
  • get_collection(name): Returns a JSONCollection instance.

JSONCollection

  • insert_one(document): Add a document.
  • find(query={}): Find matching documents.
  • find_one(query={}): Find first match.
  • update(query, updates): Update matching documents.
  • delete(query): Remove matching documents.
  • create_index(field): Enable indexing for a field.
  • group_by(field): Group by field value.
  • aggregate(field, operation): sum, avg, min, max.

🧪 Testing

uv run pytest -s test_collection.py

🔗 Links

⚖️ License

Distributed under the MIT License. See LICENSE for more information.

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

owi_jsondb-0.1.1.tar.gz (5.5 kB view details)

Uploaded Source

Built Distribution

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

owi_jsondb-0.1.1-py3-none-any.whl (6.6 kB view details)

Uploaded Python 3

File details

Details for the file owi_jsondb-0.1.1.tar.gz.

File metadata

  • Download URL: owi_jsondb-0.1.1.tar.gz
  • Upload date:
  • Size: 5.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.9

File hashes

Hashes for owi_jsondb-0.1.1.tar.gz
Algorithm Hash digest
SHA256 f4713c954159de87ecf4bde7be8667a0299df86f9105ddeb4055e8883e2d0008
MD5 7c080327a414de6b039df30fa252ed2a
BLAKE2b-256 766c728f13f3d8f3135cd603418e40d7f4476ebf412c149d473bc50f5ed3c7a1

See more details on using hashes here.

File details

Details for the file owi_jsondb-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for owi_jsondb-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c803c6b3b5e4595d9160baeac60339d3ed8e1f1aaa4021fc2516b13e82b57ff4
MD5 909c4a1669aecca690af14340ff66ff0
BLAKE2b-256 9586d8ccc573b8c6f656c31f028c8dc78f65448a5f7d4befa544a1deb32e0991

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