Skip to main content

A lightweight, type-safe storage system for Pydantic models with support for multiple backends and persistent storage.

Project description

🗃️ 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.

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

pydantic_storage-0.1.1.tar.gz (23.8 kB view details)

Uploaded Source

Built Distribution

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

pydantic_storage-0.1.1-py3-none-any.whl (15.4 kB view details)

Uploaded Python 3

File details

Details for the file pydantic_storage-0.1.1.tar.gz.

File metadata

  • Download URL: pydantic_storage-0.1.1.tar.gz
  • Upload date:
  • Size: 23.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.7.6

File hashes

Hashes for pydantic_storage-0.1.1.tar.gz
Algorithm Hash digest
SHA256 52b2a762fc931bdb1b9359a282dbe080474419d62ebeaf741bd5c282b2d9b434
MD5 98c3a615a553d8a058f55bc3fa6fc9e4
BLAKE2b-256 c2f36507e7240f282103756e9a70b12e940c21f4f00266879c339e763582f230

See more details on using hashes here.

File details

Details for the file pydantic_storage-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for pydantic_storage-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 212841500c9d4182d9f87c5ecb8d2a8e3cf0261c5ef8b620035fd8a9133f05a2
MD5 2d57dd58d75bb0a466fa2b230a274333
BLAKE2b-256 530cea493cfab443af7c76467cf995589dfa77856ce492cb5f1ab9f4694cebd4

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