Skip to main content

A simple JSON-based database for Python applications

Project description

Simple JSON DB

A lightweight, type-safe JSON-based database for Python applications using dataclasses. Perfect for small projects, prototyping, and situations where you need a simple persistent storage solution with type safety.

Features

  • 🚀 Type-safe: Uses Python dataclasses for structured data
  • 📁 File-based: Uses JSON files for storage - easy to inspect and backup
  • 🔍 Query support: Find records using attribute-based queries
  • 🔧 CRUD operations: Create, Read, Update, Delete operations
  • 📦 Zero dependencies: No external dependencies required
  • 🐍 Type hints: Full type hint support with generics
  • Well tested: Comprehensive test suite
  • 🆔 UUID support: Automatic handling of UUID fields

Installation

Install from PyPI using pip:

pip install typed-json-db

Or using uv:

uv add typed-json-db

Quick Start

from dataclasses import dataclass
from enum import Enum
import uuid
from pathlib import Path
from typed_json_db import JsonDB

# Define your data structure using dataclasses
class Status(Enum):
    PENDING = "pending"
    ACTIVE = "active"
    COMPLETED = "completed"

@dataclass
class User:
    id: uuid.UUID
    name: str
    email: str
    status: Status
    age: int

# Create or connect to a database
db = JsonDB(User, Path("users.json"))

# Add records
user1 = User(
    id=uuid.uuid4(),
    name="Alice Johnson", 
    email="alice@example.com",
    status=Status.ACTIVE,
    age=30
)
db.add(user1)

# Find records
active_users = db.find(status=Status.ACTIVE)
specific_user = db.get(user1.id)
all_users = db.all()

# Update records (modify and save)
user1.age = 31
db.update(user1)

# Remove records
db.remove(user1.id)

API Reference

JsonDB[T](data_class: Type[T], file_path: Path)

Create a new type-safe database instance.

Parameters:

  • data_class: The dataclass type this database will store
  • file_path: Path to the JSON database file

Methods

add(item: T) -> T

Add a new item to the database and save automatically.

get(id_value: uuid.UUID) -> Optional[T]

Get an item by its UUID. Returns None if not found.

find(**kwargs) -> List[T]

Find all items matching the given attribute criteria.

all() -> List[T]

Get all items in the database.

update(item: T) -> T

Update an existing item (by ID) and save automatically.

remove(id_value: uuid.UUID) -> bool

Remove an item by its UUID. Returns True if removed, False if not found.

save() -> None

Manually save the database (automatic for add/update/remove operations).

Advanced Features

Automatic Type Conversion

The database automatically handles serialization/deserialization of:

  • UUID fields
  • Enum values
  • datetime and date objects
  • Complex nested dataclass structures

Error Handling

from typed_json_db import JsonDBException

try:
    db.add(invalid_item)
except JsonDBException as e:
    print(f"Database error: {e}")

Development

This project uses uv for dependency management and packaging.

Setup Development Environment

# Clone the repository
git clone https://github.com/frangiz/typed-json-db.git
cd typed-json-db

# Install development dependencies
uv sync

Running Tests

make test

Code Formatting and Checking

make format
make check

Building the Package

make build

Publishing to PyPI

# Test on TestPyPI first
make publish-test

# Publish to PyPI
make publish

License

This project is licensed under the MIT License - see the LICENSE file for details.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

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

typed_json_db-0.2.1.tar.gz (12.9 kB view details)

Uploaded Source

Built Distribution

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

typed_json_db-0.2.1-py3-none-any.whl (8.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: typed_json_db-0.2.1.tar.gz
  • Upload date:
  • Size: 12.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for typed_json_db-0.2.1.tar.gz
Algorithm Hash digest
SHA256 44da6dd0d409ab971b8a09f132b4c1695f9053552fc6980c02ba112b57d7d084
MD5 4e2d3632d6b4f897d5443efcb697370a
BLAKE2b-256 6aa6d687fd9fe86162c9c455233bf5c09d3ad57e6bbb65ff085e15b736b675ca

See more details on using hashes here.

Provenance

The following attestation bundles were made for typed_json_db-0.2.1.tar.gz:

Publisher: deploy-pypi.yml on frangiz/typed-json-db

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

File details

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

File metadata

  • Download URL: typed_json_db-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 8.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for typed_json_db-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 1244619476e114acf6c10f45845d059108b15f1240b3a17fb7d881519d2dde29
MD5 28f36e0047970cd6fc2528381aa2fb6d
BLAKE2b-256 87e55403971e6aa8bec4d545956a6f330a4b7b183e595cd03a126b837f42bc8d

See more details on using hashes here.

Provenance

The following attestation bundles were made for typed_json_db-0.2.1-py3-none-any.whl:

Publisher: deploy-pypi.yml on frangiz/typed-json-db

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