typed-json-db


Nametyped-json-db JSON
Version 0.2.2 PyPI version JSON
download
home_pageNone
SummaryA simple JSON-based database for Python applications
upload_time2025-07-17 07:36:47
maintainerNone
docs_urlNone
authorNone
requires_python>=3.11
licenseMIT License Copyright (c) 2025 Björn Olsson Jarl Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
keywords database dataclass json persistence storage typed
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # 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
- � **Primary key support**: Optional configurable primary key with uniqueness enforcement
- ⚡ **Indexing**: Automatic indexing for primary key operations for fast lookups
- �📦 **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:

```bash
pip install typed-json-db
```

Or using uv:

```bash
uv add typed-json-db
```

## Quick Start

```python
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"))

# Or create with a primary key for better performance and uniqueness enforcement
db = JsonDB(User, Path("users.json"), primary_key="id")

# 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, primary_key: Optional[str] = None)

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
- `primary_key`: Optional field name to use as primary key for uniqueness and indexing

### Methods

#### add(item: T) -> T
Add a new item to the database and save automatically. If a primary key is configured, enforces uniqueness.

#### get(primary_key_value: Any) -> Optional[T]
Get an item by its primary key value. Returns None if not found. Requires a primary key to be configured.

#### find(**kwargs) -> List[T]
Find all items matching the given attribute criteria. Requires at least one search criterion.

#### all() -> List[T]
Get all items in the database.

#### update(item: T) -> T
Update an existing item (by primary key) and save automatically. Requires a primary key to be configured.

#### remove(primary_key_value: Any) -> bool
Remove an item by its primary key value. Returns True if removed, False if not found. Requires a primary key to be configured.

#### save() -> None
Manually save the database (automatic for add/update/remove operations).

## Advanced Features

### Primary Key Configuration

Configure a primary key for better performance and data integrity:

```python
@dataclass
class Product:
    sku: str
    name: str
    price: float

# Use 'sku' as primary key
db = JsonDB(Product, Path("products.json"), primary_key="sku")

# Primary key operations are fast (O(1)) and enforce uniqueness
product = Product(sku="ABC123", name="Widget", price=9.99)
db.add(product)

# Fast retrieval by primary key
found = db.get("ABC123")

# Trying to add duplicate primary key raises an exception
duplicate = Product(sku="ABC123", name="Another Widget", price=19.99)
# db.add(duplicate)  # Raises JsonDBException
```

### Database Operations Without Primary Key

You can still use the database without a primary key, but some operations will be limited:

```python
# No primary key specified
db = JsonDB(User, Path("users.json"))

# These work normally
db.add(user)
users = db.find(status=Status.ACTIVE)
all_users = db.all()

# These require a primary key and will raise JsonDBException
# db.get(some_id)      # Not available
# db.update(user)      # Not available  
# db.remove(some_id)   # Not available
```

### Automatic Type Conversion

The database automatically handles serialization/deserialization of:
- UUID fields
- Enum values  
- datetime and date objects
- Complex nested dataclass structures

### Performance and Indexing

When a primary key is configured, the database automatically creates an in-memory index for fast lookups:

- **Primary key operations** (`get`, single-key `find`) are O(1) - constant time
- **Non-primary key searches** use linear search - O(n) time
- **Index is automatically maintained** during add, update, and remove operations
- **Index is rebuilt** when loading the database from disk

```python
# Fast O(1) operations with primary key
db = JsonDB(User, Path("users.json"), primary_key="id")
user = db.get(user_id)  # Very fast, uses index

# Linear search operations (still fast for reasonable dataset sizes)
active_users = db.find(status=Status.ACTIVE)  # Searches all records
```

### Error Handling

```python
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

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

# Install development dependencies
uv sync
```

### Running Tests

```bash
make test
```

### Code Formatting and Checking

```bash
make format
make check
```

### Building the Package

```bash
make build
```

### Publishing to PyPI

```bash
# Test on TestPyPI first
make publish-test

# Publish to PyPI
make publish
```

## License

This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.

## Contributing

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

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "typed-json-db",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.11",
    "maintainer_email": "frangiz <frangiz@gmail.com>",
    "keywords": "database, dataclass, json, persistence, storage, typed",
    "author": null,
    "author_email": "frangiz <frangiz@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/21/35/909831d93940e4c0a366bb33e6720ed0a1c9e4e675d3d87b0efacb2b8e4c/typed_json_db-0.2.2.tar.gz",
    "platform": null,
    "description": "# Simple JSON DB\n\nA 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.\n\n## Features\n\n- \ud83d\ude80 **Type-safe**: Uses Python dataclasses for structured data\n- \ud83d\udcc1 **File-based**: Uses JSON files for storage - easy to inspect and backup\n- \ud83d\udd0d **Query support**: Find records using attribute-based queries\n- \ud83d\udd27 **CRUD operations**: Create, Read, Update, Delete operations\n- \ufffd **Primary key support**: Optional configurable primary key with uniqueness enforcement\n- \u26a1 **Indexing**: Automatic indexing for primary key operations for fast lookups\n- \ufffd\ud83d\udce6 **Zero dependencies**: No external dependencies required\n- \ud83d\udc0d **Type hints**: Full type hint support with generics\n- \u2705 **Well tested**: Comprehensive test suite\n- \ud83c\udd94 **UUID support**: Automatic handling of UUID fields\n\n## Installation\n\nInstall from PyPI using pip:\n\n```bash\npip install typed-json-db\n```\n\nOr using uv:\n\n```bash\nuv add typed-json-db\n```\n\n## Quick Start\n\n```python\nfrom dataclasses import dataclass\nfrom enum import Enum\nimport uuid\nfrom pathlib import Path\nfrom typed_json_db import JsonDB\n\n# Define your data structure using dataclasses\nclass Status(Enum):\n    PENDING = \"pending\"\n    ACTIVE = \"active\"\n    COMPLETED = \"completed\"\n\n@dataclass\nclass User:\n    id: uuid.UUID\n    name: str\n    email: str\n    status: Status\n    age: int\n\n# Create or connect to a database\ndb = JsonDB(User, Path(\"users.json\"))\n\n# Or create with a primary key for better performance and uniqueness enforcement\ndb = JsonDB(User, Path(\"users.json\"), primary_key=\"id\")\n\n# Add records\nuser1 = User(\n    id=uuid.uuid4(),\n    name=\"Alice Johnson\", \n    email=\"alice@example.com\",\n    status=Status.ACTIVE,\n    age=30\n)\ndb.add(user1)\n\n# Find records\nactive_users = db.find(status=Status.ACTIVE)\nspecific_user = db.get(user1.id)\nall_users = db.all()\n\n# Update records (modify and save)\nuser1.age = 31\ndb.update(user1)\n\n# Remove records\ndb.remove(user1.id)\n```\n\n## API Reference\n\n### JsonDB[T](data_class: Type[T], file_path: Path, primary_key: Optional[str] = None)\n\nCreate a new type-safe database instance.\n\n**Parameters:**\n- `data_class`: The dataclass type this database will store\n- `file_path`: Path to the JSON database file\n- `primary_key`: Optional field name to use as primary key for uniqueness and indexing\n\n### Methods\n\n#### add(item: T) -> T\nAdd a new item to the database and save automatically. If a primary key is configured, enforces uniqueness.\n\n#### get(primary_key_value: Any) -> Optional[T]\nGet an item by its primary key value. Returns None if not found. Requires a primary key to be configured.\n\n#### find(**kwargs) -> List[T]\nFind all items matching the given attribute criteria. Requires at least one search criterion.\n\n#### all() -> List[T]\nGet all items in the database.\n\n#### update(item: T) -> T\nUpdate an existing item (by primary key) and save automatically. Requires a primary key to be configured.\n\n#### remove(primary_key_value: Any) -> bool\nRemove an item by its primary key value. Returns True if removed, False if not found. Requires a primary key to be configured.\n\n#### save() -> None\nManually save the database (automatic for add/update/remove operations).\n\n## Advanced Features\n\n### Primary Key Configuration\n\nConfigure a primary key for better performance and data integrity:\n\n```python\n@dataclass\nclass Product:\n    sku: str\n    name: str\n    price: float\n\n# Use 'sku' as primary key\ndb = JsonDB(Product, Path(\"products.json\"), primary_key=\"sku\")\n\n# Primary key operations are fast (O(1)) and enforce uniqueness\nproduct = Product(sku=\"ABC123\", name=\"Widget\", price=9.99)\ndb.add(product)\n\n# Fast retrieval by primary key\nfound = db.get(\"ABC123\")\n\n# Trying to add duplicate primary key raises an exception\nduplicate = Product(sku=\"ABC123\", name=\"Another Widget\", price=19.99)\n# db.add(duplicate)  # Raises JsonDBException\n```\n\n### Database Operations Without Primary Key\n\nYou can still use the database without a primary key, but some operations will be limited:\n\n```python\n# No primary key specified\ndb = JsonDB(User, Path(\"users.json\"))\n\n# These work normally\ndb.add(user)\nusers = db.find(status=Status.ACTIVE)\nall_users = db.all()\n\n# These require a primary key and will raise JsonDBException\n# db.get(some_id)      # Not available\n# db.update(user)      # Not available  \n# db.remove(some_id)   # Not available\n```\n\n### Automatic Type Conversion\n\nThe database automatically handles serialization/deserialization of:\n- UUID fields\n- Enum values  \n- datetime and date objects\n- Complex nested dataclass structures\n\n### Performance and Indexing\n\nWhen a primary key is configured, the database automatically creates an in-memory index for fast lookups:\n\n- **Primary key operations** (`get`, single-key `find`) are O(1) - constant time\n- **Non-primary key searches** use linear search - O(n) time\n- **Index is automatically maintained** during add, update, and remove operations\n- **Index is rebuilt** when loading the database from disk\n\n```python\n# Fast O(1) operations with primary key\ndb = JsonDB(User, Path(\"users.json\"), primary_key=\"id\")\nuser = db.get(user_id)  # Very fast, uses index\n\n# Linear search operations (still fast for reasonable dataset sizes)\nactive_users = db.find(status=Status.ACTIVE)  # Searches all records\n```\n\n### Error Handling\n\n```python\nfrom typed_json_db import JsonDBException\n\ntry:\n    db.add(invalid_item)\nexcept JsonDBException as e:\n    print(f\"Database error: {e}\")\n```\n\n## Development\n\nThis project uses `uv` for dependency management and packaging.\n\n### Setup Development Environment\n\n```bash\n# Clone the repository\ngit clone https://github.com/frangiz/typed-json-db.git\ncd typed-json-db\n\n# Install development dependencies\nuv sync\n```\n\n### Running Tests\n\n```bash\nmake test\n```\n\n### Code Formatting and Checking\n\n```bash\nmake format\nmake check\n```\n\n### Building the Package\n\n```bash\nmake build\n```\n\n### Publishing to PyPI\n\n```bash\n# Test on TestPyPI first\nmake publish-test\n\n# Publish to PyPI\nmake publish\n```\n\n## License\n\nThis project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.\n\n## Contributing\n\nContributions are welcome! Please feel free to submit a Pull Request.",
    "bugtrack_url": null,
    "license": "MIT License\n        \n        Copyright (c) 2025 Bj\u00f6rn Olsson Jarl\n        \n        Permission is hereby granted, free of charge, to any person obtaining a copy\n        of this software and associated documentation files (the \"Software\"), to deal\n        in the Software without restriction, including without limitation the rights\n        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n        copies of the Software, and to permit persons to whom the Software is\n        furnished to do so, subject to the following conditions:\n        \n        The above copyright notice and this permission notice shall be included in all\n        copies or substantial portions of the Software.\n        \n        THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n        SOFTWARE.",
    "summary": "A simple JSON-based database for Python applications",
    "version": "0.2.2",
    "project_urls": {
        "Documentation": "https://github.com/frangiz/typed-json-db#readme",
        "Homepage": "https://github.com/frangiz/typed-json-db",
        "Issues": "https://github.com/frangiz/typed-json-db/issues",
        "Repository": "https://github.com/frangiz/typed-json-db"
    },
    "split_keywords": [
        "database",
        " dataclass",
        " json",
        " persistence",
        " storage",
        " typed"
    ],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "1da40344112c8b16081c456c3640c31f314248557e94b166b9bf4be0ac90b516",
                "md5": "08ce1f33f0318b8e9f31d1a1b3ababab",
                "sha256": "6e54fe1b471afdce7525d1a0aa3e24310203d58e6e1b36da4317b9432a8f6f3d"
            },
            "downloads": -1,
            "filename": "typed_json_db-0.2.2-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "08ce1f33f0318b8e9f31d1a1b3ababab",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.11",
            "size": 8972,
            "upload_time": "2025-07-17T07:36:46",
            "upload_time_iso_8601": "2025-07-17T07:36:46.481549Z",
            "url": "https://files.pythonhosted.org/packages/1d/a4/0344112c8b16081c456c3640c31f314248557e94b166b9bf4be0ac90b516/typed_json_db-0.2.2-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "2135909831d93940e4c0a366bb33e6720ed0a1c9e4e675d3d87b0efacb2b8e4c",
                "md5": "c60ca4c0d622b7097a1067a3925880fa",
                "sha256": "b350250157d78435b6ec51b82fddd58b8303c04b34ef1a60a2a28fba076ce3d8"
            },
            "downloads": -1,
            "filename": "typed_json_db-0.2.2.tar.gz",
            "has_sig": false,
            "md5_digest": "c60ca4c0d622b7097a1067a3925880fa",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.11",
            "size": 14496,
            "upload_time": "2025-07-17T07:36:47",
            "upload_time_iso_8601": "2025-07-17T07:36:47.447363Z",
            "url": "https://files.pythonhosted.org/packages/21/35/909831d93940e4c0a366bb33e6720ed0a1c9e4e675d3d87b0efacb2b8e4c/typed_json_db-0.2.2.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-07-17 07:36:47",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "frangiz",
    "github_project": "typed-json-db#readme",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "typed-json-db"
}
        
Elapsed time: 1.04261s