Skip to main content

🗃️ PydanticStorage

PyPI version Python versions License

A lightweight, extensible, and fully type-safe data storage system built with Pydantic and modern Python. Initially file-based (JSON), it's designed to support multiple backends like CSV, SQL, and more. It enables persistent storage of Pydantic models, auto-generated IDs, metadata, uniqueness constraints, partial updates, and structured storage with a real-world schema.

🚀 Features

  • ✅ JSON-based file storage for Pydantic models
  • 🔐 Unique field constraint (e.g., prevent duplicate emails)
  • 🆔 Auto ID assignment starting from 1
  • 🧩 Partial & full record updates
  • 🗂️ Structured schema with metadata + records
  • 🧹 Methods like filter, exists, count, clear, create, bulk_create, etc.
  • 📦 Metadata support

📦 Installation

Install the package using your preferred Python package manager:

Using uv

uv add pydantic-storage

Using pip

pip install pydantic-storage

🧠 Usage

1. Define your Pydantic model

from pydantic import BaseModel

class User(BaseModel):
    id: int | None = None
    name: str
    email: str

2. Initialize Storage

from pydantic_storage import JsonFileStorage

store = JsonFileStorage(
    model=User,
    uri="records/users.json",
    unique_fileds=["email"],
    metadata={
        "version": "1.0.0",
        "title": "User Store",
        "description": "Stores user info",
    }
)

3. Create Records

store.create(User(name="Alice", email="alice@example.com"))
store.create(User(name="Bob", email="bob@example.com"))

4. Get or Filter Records

user = store.get("email", "alice@example.com")
users = store.filter(lambda user: user.name.startswith("A"))

5. Update Records

# Full update using function
store.update("id", 1, lambda user: user.model_copy(
    update={"email": "new@example.com"}
))

# Partial update
store.update_partial("id", 2, {"name": "Robert"})

6. Delete or Clear all Records

store.delete("email", "bob@example.com")
store.clear()

7. Bulk Create

store.bulk_create([
    User(name="Charlie", email="charlie@example.com"),
    User(name="Alice", email="alice@example.com")  # Skipped if duplicate
])

📁 JSON Structure Example

{
  "metadata": {
    "version": "1.0.0",
    "title": "User records",
    "description": "User record descriptions",
    "storage": {
      "backend": "file",
      "format": "json",
      "encryption": "none",
      "uri": "file:///mnt/data/workshop/applications/pypi-package/pydantic-storage/tests/db_files/users.json"
    },
    "timestamps": {
      "created_at": "2025-08-11T13:54:05.071548Z",
      "accessed_at": "2025-08-11T13:56:19.872671Z",
      "modified_at": "2025-08-11T13:56:19.872308Z"
    }
  },
  "records": [
    {
      "id": 1,
      "name": "shailesh",
      "email": "shailesh@gmail.com"
    },
    {
      "id": 2,
      "name": "yash",
      "email": "yash@gmail.com"
    },
    {
      "id": 3,
      "name": "json",
      "email": "json@gmail.com"
    },
    {
      "id": 4,
      "name": "nice",
      "email": "nice@gmail.com"
    },
    {
      "id": 5,
      "name": "yashika",
      "email": "yashika@gmail.com"
    }
  ]
}

❗ Exceptions

  • DuplicateEntryError: Raised when uniqueness is violated on create.
  • ValidationError: Raised if an action violates the model schema.

🧪 Testing

Run tests with:

pytest tests

Covers record creation, uniqueness, updates, deletions, and metadata.

🛠️ Planned Features

  • 🔒 Optional encryption
  • 📦 Export/import to CSV, SQLite
  • 🕒 Automatic backups
  • 🧪 Pytest-based suite
  • 📊 Schema evolution support

🤝 Contributing

Contributions are welcome! Please open an issue or PR for any improvements.

📄 License

MIT License. See the LICENSE file for details.

👤 Author

For questions or assistance, contact Shailesh at shaileshpandit141@gmail.com.

Metadata

Release files for pydantic-storage 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pydantic-storage 0.1.1
File Size Uploaded
pydantic_storage-0.1.1.tar.gz 23.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pydantic-storage 0.1.1
File Interpreter ABI Platform
pydantic_storage-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 39.2 kB

Release files / pydantic_storage-0.1.1.tar.gz

Download URL pydantic_storage-0.1.1.tar.gz
Size 23.8 kB
Tags Source
SHA-256 checksum
How to use checksums
52b2a762fc931bdb1b9359a282dbe080474419d62ebeaf741bd5c282b2d9b434
BLAKE2b-256 checksum
How to use checksums
c2f36507e7240f282103756e9a70b12e940c21f4f00266879c339e763582f230
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.7.6

Release files / pydantic_storage-0.1.1-py3-none-any.whl

Download URL pydantic_storage-0.1.1-py3-none-any.whl
Size 15.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
212841500c9d4182d9f87c5ecb8d2a8e3cf0261c5ef8b620035fd8a9133f05a2
BLAKE2b-256 checksum
How to use checksums
530cea493cfab443af7c76467cf995589dfa77856ce492cb5f1ab9f4694cebd4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.7.6

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page