Skip to main content

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

Project description

JSON DB Engine

A lightweight, document-oriented database using JSON for data persistence. Inspired by MongoDB, this project supports nested queries, indexing for faster lookups, query operators, and a familiar API.

Features

  • Document-based storage: Data is stored in simple JSON files.
  • Automatic _id generation: Every document gets a unique UUID if not provided.
  • Advanced Querying: Supports nested field lookups and a variety of query operators.
  • Indexing: Create indexes on specific fields to speed up queries.
  • Aggregation & Grouping: Perform SQL-like GROUP BY and aggregations (sum, avg, min, max).
  • Persistence: Data is automatically saved to disk after write operations.

Installation

# Clone the repository
git clone https://github.com/cleave3/json_db.git
cd json_db

# Install dependencies using uv
uv sync

API Reference

JSONDatabase

The main entry point for managing collections.

  • db = JSONDatabase(base_path="storage"): Initialize the database with a storage directory.
  • collection = db.get_collection(name): Get or create a collection by name.

JSONCollection

Represents a single collection of documents.

  • insert_one(document): Inserts a single document.
  • find(query): Returns a list of documents matching the query.
  • find_one(query): Returns the first document matching the query, or None.
  • update(query, updates): Updates all documents matching the query with the provided fields.
  • delete(query): Deletes all documents matching the query.
  • create_index(field): Creates an index on the specified field.
  • group_by(field): Groups documents by a specific field.
  • aggregate(field, operation): Performs an aggregation (sum, avg, min, max) on a numeric field.

Basic Usage

from json_db_engine import JSONDatabase

# Initialize DB
db = JSONDatabase()
users = db.get_collection("users")

# Insert
users.insert_one({
    "name": "Alice", 
    "age": 30, 
    "address": {"city": "London", "zip": "SW1A"}
})

# Custom _id (optional)
users.insert_one({"_id": "custom-id-123", "name": "Bob"})

# Find with nested field
user = users.find_one({"address.city": "London"})
print(user)

# Update
users.update({"name": "Alice"}, {"age": 31})

# Aggregate
total_age = users.aggregate("age", "sum")
print(f"Total Age: {total_age}")

Query Operators

JSON DB supports several operators for advanced filtering:

Operator Description Example
$gt Greater than {"age": {"$gt": 25}}
$lt Less than {"age": {"$lt": 40}}
$gte Greater than or equal {"age": {"$gte": 30}}
$lte Less than or equal {"age": {"$lte": 30}}
$ne Not equal {"status": {"$ne": "active"}}
$in Value in list {"tags": {"$in": ["python", "db"]}}
$regex Regular expression {"name": {"$regex": "^A.*"}}

Complex Query Example

results = users.find({
    "age": {"$gte": 20, "$lte": 50},
    "address.city": {"$in": ["London", "Paris"]},
    "name": {"$regex": "i"}
})

Indexing for Performance

Indexes significantly speed up find operations by avoiding full collection scans.

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

# Subsequent queries on 'email' will use the index
user = users.find_one({"email": "alice@example.com"})

Aggregation & Grouping

Group By

Groups documents by a specific field (supports nested fields).

# Group users by city
cities = users.group_by("address.city")
for city, docs in cities.items():
    print(f"{city}: {len(docs)} users")

Aggregate

Performs calculations on numeric fields. Supported operations: sum, avg, min, max.

average_age = users.aggregate("age", "avg")
oldest_user = users.aggregate("age", "max")

Testing

Run the test suite with verbose output to see the results of each test case:

uv run pytest -s test_collection.py

Limitations

  • Not Thread-Safe: This is a lightweight engine designed for single-process use.
  • In-Memory Reads: Reads the entire collection into memory for operations (suitable for small to medium datasets).

Contributing

Pull requests are welcome! For major changes, please open an issue first.

License

MIT

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.0.tar.gz (4.4 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.0-py3-none-any.whl (5.5 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for owi_jsondb-0.1.0.tar.gz
Algorithm Hash digest
SHA256 50737763c3418e904fab301d85857ba8ddeff2e6c99ac2472c420a8657e400ba
MD5 dafe8725ebeab9bba1685e180894bbf3
BLAKE2b-256 7636253b1168bf93e9e56082643bee7816baf1d57856a18c94431fc1258029c6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for owi_jsondb-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 16c61ccd67538bc071e9bad6a02135f4869d9871f522b97ff213c94cb4c2493f
MD5 ec475f0bbcfe26af230d802e8340db02
BLAKE2b-256 875349f29f93dd4e13fb23d64d062213aa970c2ac82f5c3ff6a9158800464326

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