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 storefile_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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file typed_json_db-0.2.0.tar.gz.
File metadata
- Download URL: typed_json_db-0.2.0.tar.gz
- Upload date:
- Size: 11.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7c5360de70ef6839721587d73150bbc244b82ebc6835dfd4e2954dd3b172fb56
|
|
| MD5 |
3cd0efb22451ffca5813a69d881ebb90
|
|
| BLAKE2b-256 |
14f1c75cf8adc9c1bb8e2ddb2af6a75eefb9bde7442dd80aad0edde7b2f1c2d4
|
Provenance
The following attestation bundles were made for typed_json_db-0.2.0.tar.gz:
Publisher:
deploy-pypi.yml on frangiz/typed-json-db
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
typed_json_db-0.2.0.tar.gz -
Subject digest:
7c5360de70ef6839721587d73150bbc244b82ebc6835dfd4e2954dd3b172fb56 - Sigstore transparency entry: 265900516
- Sigstore integration time:
-
Permalink:
frangiz/typed-json-db@dcdea3133b19a514a6e2b57f807e1159280f6def -
Branch / Tag:
refs/heads/main - Owner: https://github.com/frangiz
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
deploy-pypi.yml@dcdea3133b19a514a6e2b57f807e1159280f6def -
Trigger Event:
push
-
Statement type:
File details
Details for the file typed_json_db-0.2.0-py3-none-any.whl.
File metadata
- Download URL: typed_json_db-0.2.0-py3-none-any.whl
- Upload date:
- Size: 7.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f1b35ea6d7e171c69756ae8c4521e9f6550ab75c4c65701340d5e0e8767d9474
|
|
| MD5 |
421a10aff31f7203f0868028002b2858
|
|
| BLAKE2b-256 |
73bdfc0449fc50bdd6aa46e53c38258d688ec560f9001752bea6d2c4dc2f74a0
|
Provenance
The following attestation bundles were made for typed_json_db-0.2.0-py3-none-any.whl:
Publisher:
deploy-pypi.yml on frangiz/typed-json-db
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
typed_json_db-0.2.0-py3-none-any.whl -
Subject digest:
f1b35ea6d7e171c69756ae8c4521e9f6550ab75c4c65701340d5e0e8767d9474 - Sigstore transparency entry: 265900530
- Sigstore integration time:
-
Permalink:
frangiz/typed-json-db@dcdea3133b19a514a6e2b57f807e1159280f6def -
Branch / Tag:
refs/heads/main - Owner: https://github.com/frangiz
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
deploy-pypi.yml@dcdea3133b19a514a6e2b57f807e1159280f6def -
Trigger Event:
push
-
Statement type: