Asynchronous Pydantic ODM for Google Cloud Firestore
Project description
Firestore Pydantic ODM
Firestore Pydantic ODM is a lightweight, fully-typed Object-Document Mapper for Google Cloud Firestore.
It combines [Pydantic]’s data-validation super-powers with Firestore’s scalable NoSQL store, offering async CRUD, batch writes, transactions, and projections that request only the fields you need—making queries faster and cheaper.
Features
- Asynchronous CRUD: Full support for creating, reading, updating, and deleting Firestore documents using
async/await. - Validation with Pydantic: Define your data models with automatic validation, ensuring data integrity before it reaches the database.
- Advanced Queries: Perform searches with filters, projections (selecting only specific fields), and ordering.
- Batch Operations and Transactions: Group multiple write operations and execute transactions atomically for greater efficiency and consistency.
- Emulator and Testing Support: Easily switch to the Firestore emulator or plug in mocks for unit testing.
- Seamless Integration: Fits smoothly into any Python project with minimal setup.
Installation
pip install firestore-pydantic-odm
Quick Start
1 · Define a model
from firestore_pydantic_odm import BaseFirestoreModel
class User(BaseFirestoreModel):
class Settings:
name = "users" # Firestore collection name
name: str
email: str
2 · Initialise Firestore
from firestore_pydantic_odm import FirestoreDB, BaseFirestoreModel
db = FirestoreDB(project_id="my-project", emulator_host="localhost:8080") # optional emulator
BaseFirestoreModel.initialize_db(db)
3 · Async CRUD
user = User(name="Alice", email="alice@example.com")
await user.save() # CREATE
user.email = "alice@new.com"
await user.update() # UPDATE
await user.delete() # DELETE
4 · Querying & Projections
# Simple filter
async for u in User.find(filters=[User.name == "Alice"]):
print(u)
# Single document
u = await User.find_one(filters=[User.email == "alice@new.com"])
Projections — selecting only the fields you need
from pydantic import BaseModel
class UserProjection(BaseModel):
name: str # only grab the `name` field
async for u in User.find(
filters=[User.age >= 18],
projection=UserProjection):
print(u.name) # `u` is an instance of UserProjection
# Fetch a single document with a projection
u = await User.find_one(
filters=[User.id == "abc123"],
projection=UserProjection)
How it works: the ODM converts
UserProjectioninto a Firestore field mask, so the RPC fetches only the columns defined in that class. Each item yielded byfind()(or returned byfind_one()) is therefore of typeUserProjection, giving you a cleanList[UserProjection]with exactly the data requested.
5 · Batch writes
from firestore_pydantic_odm import BatchOperation
ops = [
(BatchOperation.CREATE, User(name="Bob", email="bob@example.com")),
(BatchOperation.UPDATE, user), # previously fetched instance
(BatchOperation.DELETE, another_user) # instance with `id` set
]
await User.batch_write(ops)
Testing
The project ships with pytest and pytest-asyncio fixtures. To run the suite:
pytest
Set FIRESTORE_EMULATOR_HOST=localhost:8080 to run tests against the local emulator instead of production Firestore.
Contributing
- Fork the repository
git checkout -b feature/awesome- Write code & tests; ensure all tests pass
- Open a Pull Request describing your improvements
License
Distributed under the BSD 3-Clause License.
See the LICENSE file for full text.
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 firestore_pydantic_odm-0.2.0.tar.gz.
File metadata
- Download URL: firestore_pydantic_odm-0.2.0.tar.gz
- Upload date:
- Size: 12.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.11.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
faaeab70d9697d7ac0229d9326f5a05cae10704249aead9fb122a0702cef5033
|
|
| MD5 |
c6e1cafbbff0fd9b0ec64518b34224ef
|
|
| BLAKE2b-256 |
a9c5a9b156ca2953d45a1a9426105a7c484e292b983a678c9668b5c3daf35391
|
File details
Details for the file firestore_pydantic_odm-0.2.0-py3-none-any.whl.
File metadata
- Download URL: firestore_pydantic_odm-0.2.0-py3-none-any.whl
- Upload date:
- Size: 11.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.11.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
54cbdd8ceb76bcd9882c9c76cd6400a094b1266361d60b6a0eeee07f53fbd837
|
|
| MD5 |
da9a918eef00eb1a3c29afbdaf67c69b
|
|
| BLAKE2b-256 |
9e435f40a01b6eee97248381019b73f378637a17af378edf642a4b3e9cd3f4f8
|