Metadata-Version: 2.1
Name: firedantic
Version: 0.1.4
Summary: Pydantic base model for Firestore
Home-page: https://github.com/digitalliving/firedantic
License: BSD-3-Clause
Author: Digital Living International Ltd
Requires-Python: >=3.6,<4.0
Classifier: License :: OSI Approved :: BSD License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.6
Classifier: Programming Language :: Python :: 3.7
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Requires-Dist: google-cloud-firestore (>=1.9.0,<2.0.0)
Requires-Dist: grpcio (>=1.32.0,<2.0.0)
Requires-Dist: pydantic (>=1.6.1,<2.0.0)
Project-URL: Repository, https://github.com/digitalliving/firedantic
Description-Content-Type: text/markdown

# Firedantic

[![Build Status](https://travis-ci.org/digitalliving/firedantic.svg?branch=master)](https://travis-ci.org/digitalliving/firedantic)

Database models for Firestore using Pydantic base models.


## Installation

The package is available on PyPi:

```bash
pip install firedantic
```


## Usage

In your application you will need to configure the firestore db client and
optionally the collection prefix, which by default is empty.

```python
from mock import Mock
from os import environ

import google.auth.credentials
from firedantic import configure
from google.cloud import firestore

# Firestore emulator must be running if using locally.
if environ.get("FIRESTORE_EMULATOR_HOST"):
    client = firestore.Client(
        project="firedantic-test",
        credentials=Mock(spec=google.auth.credentials.Credentials)
    )
else:
    client = firestore.Client()

configure(client, prefix="firedantic-test-")
```

Once that is done, you can start defining your Pydantic models, e.g:

```python
from pydantic import BaseModel

from firedantic import Model

class Owner(BaseModel):
    """Dummy owner Pydantic model."""
    first_name: str
    last_name: str


class Company(Model):
    """Dummy company Firedantic model."""
    __collection__ = "companies"
    company_id: str
    owner: Owner

# Now you can use the model to save it to Firestore
owner = Owner(first_name="John", last_name="Doe")
company = Company(company_id="1234567-8", owner=owner)
company.save()

# Prints out the firestore ID of the Company model
print(company.id)
```

Querying is done via a MongoDB-like `find()`:

```python
from firedantic import Model

class Product(Model):
    __collection__ = "products"
    product_id: str
    stock: int

Product.find({"product_id": "abc-123"})
Product.find({"stock": {">=": 3}})
```

The query operators are found at [https://firebase.google.com/docs/firestore/query-data/queries#query_operators](https://firebase.google.com/docs/firestore/query-data/queries#query_operators).


## Development

PRs are welcome!



To run tests locally, you should run:

```bash
poetry install
poetry run invoke test
# or
poetry run py test
```


## License

This code is released under the BSD 3-Clause license. Details in the
[LICENSE](./LICENSE) file.

