Skip to main content

An interactive console for Firestore based on Python ORM

Project description

PyFireConsole

PyFireConsole

Introduction

Inspired by Rails console, PyFireConsole provides a seamless interface to Google's Firestore in Python, simplifying tasks such as connection, ORM, and data associations. It makes managing Firestore a breeze.

Features

  • Model Definition and ORM: Define your Firestore data models within Python and use object-relational mapping (ORM) for easier data manipulation and querying.
  • Data Associations: Effortlessly manage relationships between your Firestore data models.
  • Interactive Console: Inspired by the Rails console, PyFireConsole provides a console for interactive data manipulation and querying, making it simple to perform tasks on your Firestore data.

Installation

pip install pyfireconsole

Getting Started

from datetime import datetime
from typing import Optional
from pyfireconsole.models.firestore_model import Collection, DocumentRef, FirestoreModel
from pyfireconsole.db.connection import FirestoreConnection

# Define your models 
class User(FirestoreModel):
    name: str
    email: str

class Publisher(FirestoreModel):
    name: str
    address: Optional[str]

class Tag(FirestoreModel):
    name: str

class Book(FirestoreModel):
    title: str
    user_id: str
    FirestoreModel.belongs_to(User)  # Make accessible via book.user
    published_at: datetime
    authors: list[str]
    tags: Collection[Tag] = Collection(Tag)
    publisher_ref: DocumentRef[Publisher]


# Initialize FirestoreConnection using your default credentials of gcloud. (use `gcloud auth application-default login` or set GOOGLE_APPLICATION_CREDENTIALS)
FirestoreConnection().initialize(project_id="YOUR-PROJECT-ID")

# Or you can specify service_account_key_path
# FirestoreConnection().initialize(service_account_key_path="./service-account.json", project_id="YOUR-PROJECT-ID")

print("==================== find ====================")
book = Book.find("12345")  # => Book
print(book.model_dump())  # => dict
print(f"ID: {book.id} | Title: {book.title} | Authors: {book.authors} | Published At: {book.published_at.isoformat()}")

print("==================== belongs_to ====================")
print(book.user)  # => User
print(book.user.name)  # => str

print("==================== reference ====================")
print(book.publisher_ref)  # => DocumentRef
print(book.publisher_ref.path)  # => str (So far, we can't access publisher_ref.name directly for ref type)

print("==================== where ====================")
print(Book.where("title", "==", "test"))  # => Book[] Make sure to create index in firestore for compound queries

We assume that you have a firestore database with the following structure:

=== Firestore Database ===
- users Collection
    - {user_id} Document
        - name: str
        - email: str
- publishers Collection
    - {publisher_id} Document
        - name: str
        - address: str
- books Collection
    - {book_id} Document
        - title: str
        - user_id: str
        - published_at: datetime
        - authors: list[str]
        - tags: Sub Collection
            - {tag_id} Document
                - name: str
        - publisher_ref: Reference

Interactive Console

PyFireConsole comes with an interactive console that allows developer to view and manipulate Firestore data in a live and easily. This feature is inspired by the Rails console.

How to setup interactive console:

from datetime import datetime
from typing import Optional
from pyfireconsole.models.firestore_model import Collection, DocumentRef, FirestoreModel
from pyfireconsole.db.connection import FirestoreConnection
from pyfireconsole import PyFireConsole

class User(FirestoreModel):
    name: str
    email: str

class Publisher(FirestoreModel):
    name: str
    address: Optional[str]

class Tag(FirestoreModel):
    name: str

class Book(FirestoreModel):
    title: str
    user_id: str
    FirestoreModel.belongs_to(User)  # Make accessible via book.user
    published_at: datetime
    authors: list[str]
    tags: Collection[Tag] = Collection(Tag)
    publisher_ref: DocumentRef[Publisher]

FirestoreConnection().initialize(project_id="YOUR-PROJECT-ID")
PyFireConsole().run()

Alternatively, you can define your model files in app/models/ and initialize the console as follows:

from pyfireconsole.db.connection import FirestoreConnection
from pyfireconsole import PyFireConsole

FirestoreConnection().initialize(project_id="YOUR-PROJECT-ID")
PyFireConsole(model_dir="app/models").run()

Through the interactive console, you can conveniently test and experiment with your Firestore data models.

Contributing

Your contributions to PyFireConsole are warmly welcomed! Feel free to submit a pull request directly if you have any improvements or features to suggest. For any questions or issues, please create an issue on Github. Thank you for your interest in improving PyFireConsole!

License

PyFireConsole is released under the MIT License.

This means you are free to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the software. This permission is granted provided that the above copyright notice and this permission notice are included in all copies or substantial portions of the software.

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

pyfireconsole-0.0.2.tar.gz (9.4 kB view hashes)

Uploaded Source

Built Distribution

pyfireconsole-0.0.2-py3-none-any.whl (9.3 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page