Google Firestore is a cloud-based NoSQL database from Firebase, designed for storing JSON documents. It's an excellent choice for scalable, real-time applications. However, working directly with Firestore through the google-cloud-firestore library can be cumbersome, especially if you're used to an object-oriented approach and typed code.
FireO — an ORM for Google Firestore — is a Python library that simplifies working with Firestore by providing an interface reminiscent of popular ORMs. Models, fields, validation, relationships — all of this is now available in the Firebase ecosystem.
What is FireO and Why Use It
FireO (Firebase ORM) is an object-relational mapping tool for Google Firestore, written in Python. It lets developers work with Firestore like a traditional relational database, using classes and objects instead of direct API calls.
Key Benefits of FireO:
- Ease of Use: Intuitive syntax similar to Django ORM
- Field Typing: Strict typing with automatic validation
- Data Validation: Built-in data checks before saving
- Relationship Support: Work with reference fields and nested structures
- Automatic Metadata Management: Auto-generated timestamps
- Migrations: Simplified data structure management
Installing and Configuring FireO
Installing the Library
pip install fireo
Setting Up Authentication
To work with Firestore, you need to set up authentication using a Service Account:
- Create a Service Account in the Firebase Console
- Download the JSON key
- Set the key path in an environment variable:
export GOOGLE_APPLICATION_CREDENTIALS="path/to/your-key.json"
Alternative Connection Setup
from fireo import connection
# Connect using a file
connection(from_file="path/to/serviceAccountKey.json")
# Connect using an environment variable
connection()
Firestore Basics and the Document-Oriented Model
Firestore Data Structure
Firestore stores data in the following hierarchy:
- Database → Collections → Documents → Fields
- Documents can contain subcollections
- Fields can be of various types: strings, numbers, arrays, objects, etc.
NoSQL Approach Features
Firestore is a schemaless database, meaning:
- Documents in the same collection can have different fields
- No need to define the structure in advance
- Flexibility to change data structure over time
FireO adds a layer of structure and validation on top of this flexibility.
Creating Models in FireO
Basic Model
from fireo.models import Model
from fireo.fields import TextField, NumberField, BooleanField
class User(Model):
name = TextField(required=True, max_length=100)
age = NumberField(required=True)
is_active = BooleanField(default=True)
class Meta:
collection_name = "users" # Optional; defaults to the class name
Advanced Model with Validation
from fireo.fields import EmailField, DateTimeField, ListField
from datetime import datetime
class Profile(Model):
user_id = TextField(required=True)
email = EmailField(required=True)
created_at = DateTimeField(default=datetime.now)
tags = ListField