Skip to main content

Firestore Collections

Simple Firestore collection definitions and queries using pydantic schemas and Firestore query API.

A quick and easy way to make use of the NoSQL document database solution Firestore available in Google Cloud Platform (GCP).

Requirements

  • Python 3.6+
  • GCP project with Firestore enabled

Features

  • Schema definition and validation: Define collection schemas using pydantic and built-in type hinting (typing).
  • Automatic IDs: Automatic ID generation using 12-byte hexadecimal (bson.ObjectId).
  • Simple queries: Query collections using a simple interface.
  • Auxiliary timestamps: Automatically added timestamps to objects like created_at and updated_at.

Example

Define users collection and perform different queries:

from typing import Optional

from pydantic import EmailStr, SecretStr
from firestore_collections import Collection, Schema


class User(Schema):
    __collection_name__ = 'users'
    __unique_keys__ = ['email']

    email: EmailStr
    full_name: str = None
    password: Optional[SecretStr]


# Initialize firestore collection
collection = Collection(schema=User)

# Initialize user object
user = User(
    email='john@doe.com',
    full_name='John')

# Insert
user = collection.insert(user)

# Get object from db
user = collection.get(user.id)

# Update
user.full_name = 'John Doe'
collection.update(user)

# Get by attribute
user = collection.get_by_attribute('email', 'john@doe.com')

# Get all objects
users = collection.get_all()

# Delete object
collection.delete(id=user.id)

GCP credentials

NOTE: The package assumes a valid GCP credentials file is available and its path defined in the environment variable GOOGLE_APPLICATION_CREDENTIALS.

License

This project is licensed under the terms of the MIT license.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

firestore-collections-0.4.9.tar.gz (9.3 kB view details)

Uploaded Source

File details

Details for the file firestore-collections-0.4.9.tar.gz.

File metadata

  • Download URL: firestore-collections-0.4.9.tar.gz
  • Upload date:
  • Size: 9.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.10.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.8.2

File hashes

Hashes for firestore-collections-0.4.9.tar.gz
Algorithm Hash digest
SHA256 221919a4ecd5f10b4f3227a9ec2c22bcbef1b7220162fdcaa5f744f75c78a8be
MD5 78ed72e0432c829ecc0d07a5f4c420bf
BLAKE2b-256 79b657be101f75ebe9bced96f045cf9f214f73030f7bcb83ba7fb08810c84472

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 Sentry Error logging StatusPage Status page