Skip to main content

PocketBase ORM

A Python ORM (Object-Relational Mapper) for PocketBase that provides Pydantic model integration and automatic schema synchronization.

Features

  • 🚀 Pydantic model integration for data validation and serialization
  • 🔄 Automatic schema synchronization with PocketBase collections
  • 📦 Support for most PocketBase field types including relations and file uploads
  • 🛠️ Simple and intuitive API for CRUD operations

Installation

uv install pocketbase-orm

Quick Start

from pocketbase_orm import PBModel
from pydantic import EmailStr, AnyUrl, Field
from datetime import datetime, timezone
from pocketbase.client import FileUpload

# Define your models
class RelatedModel(PBModel, collection="related_models"):  # Optionally specify collection name
    name: str

class Example(PBModel):  # Collection name will be "examples" by default
    text_field: str
    number_field: int
    is_active: bool
    url_field: AnyUrl
    created_at: datetime
    options: list[str]
    email_field: EmailStr | None = None
    related_model: RelatedModel | str = Field(..., description="Related model reference")
    image: FileUpload | str = Field(..., description="Image file upload")

# Initialize PocketBase client and bind it to the ORM
client = PBModel.init_client(
    "YOUR_POCKETBASE_URL",
    "admin@example.com",
    "password"
)

# Sync collection schemas
RelatedModel.sync_collection()
Example.sync_collection()

# Create and save records
related_model = RelatedModel(name="Related Model")
related_model.save()

# Create a new record with file upload
with open("image.png", "rb") as f:
    example = Example(
        text_field="Test with image",
        number_field=123,
        is_active=True,
        email_field="test@example.com",
        url_field="http://example.com",
        created_at=datetime.now(timezone.utc),
        options=["option1", "option2"],
        related_model=related_model.id,
        image=FileUpload(("image.png", f))
    )
    example.save()

# Query records
full_list = Example.get_full_list()
one = Example.get_one("RECORD_ID")
first = Example.get_first_list_item(filter='email_field = "test@example.com"')
filtered_list = Example.get_list(filter='email_field = "test@example.com"')

# Get file contents
image_bytes = example.get_file_contents("image")

Model Definition

Models inherit from PBModel and use Pydantic field types:

from enum import Enum

class UserType(str, Enum):
    ADMIN = "admin"
    REGULAR = "regular"
    GUEST = "guest"

class MyModel(PBModel, collection="my_models"):  # Specify custom collection name
    name: str
    age: int
    email: EmailStr | None = None
    user_type: UserType  # Will be created as a select field in PocketBase

The collection name will be automatically derived from the class name (pluralized) if not specified using the collection parameter when subclassing PBModel.

Supported Field Types

  • Text: str
  • Number: int, float
  • Boolean: bool
  • Email: EmailStr
  • URL: AnyUrl
  • DateTime: datetime
  • JSON: List, Dict
  • File: FileUpload | str
  • Relation: Union[RelatedModel, str]
  • Select: Enum

API Reference

Class Methods

  • bind_client(client: PocketBase): Bind PocketBase client to the model class
  • sync_collection(): Create or update the collection schema in PocketBase
  • delete_collection(): Delete the entire collection from PocketBase
  • get_one(id: str, **kwargs) -> T: Get a single record by ID
  • get_list(*args, **kwargs) -> List[T]: Get a paginated list of records
  • get_full_list(*args, **kwargs) -> List[T]: Get all records
  • get_first_list_item(*args, **kwargs) -> T: Get the first matching record
  • delete_by_id(id: str, **kwargs): Delete a record by ID

Instance Methods

  • save() -> T: Create or update the record
  • delete(): Delete the current record
  • get_file_contents(field: str) -> bytes: Get the contents of a file field

Limitations

  • The ORM currently supports basic CRUD operations and schema synchronization
  • Complex queries should use the PocketBase client directly
  • Relationship handling is limited to single relations
  • Indexes must be created manually
  • Schema syncing currently relies on a fork until https://github.com/vaphes/pocketbase/pull/120 is merged

Contributing

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

License

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

Metadata

Release files for pocketbase-orm 0.16.0

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

Source distribution (sdist)

Source distribution for pocketbase-orm 0.16.0
File Size Uploaded Provenance
pocketbase_orm-0.16.0.tar.gz 8.0 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for pocketbase-orm 0.16.0
File Interpreter ABI Platform
pocketbase_orm-0.16.0-py3-none-any.whl Python 3 none any Details

Total release size: 16.8 kB

Release files / pocketbase_orm-0.16.0.tar.gz

Download URL pocketbase_orm-0.16.0.tar.gz
Size 8.0 kB
Tags Source
SHA-256 checksum
How to use checksums
1c5c00aafbdaf120a18d0c471a267d6249081636faa27d74b63c1bf37ec5cd86
BLAKE2b-256 checksum
How to use checksums
c3fa3ab362704e00aa25ec6471dc5422225a733a788dc56f8bb9de687d68a0af
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.6.6

Release files / pocketbase_orm-0.16.0-py3-none-any.whl

Download URL pocketbase_orm-0.16.0-py3-none-any.whl
Size 8.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9a581afa64b3934936bd78d0fdc9eaa226e2b0264f1a9c1fd72f29f232d5095f
BLAKE2b-256 checksum
How to use checksums
aa326f0bb129dc200a923787fb3ca9d0dee759a7941191880e56c853aad43897
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.6.6

Security for pocketbase-orm 0.16.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

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