Documentation Index
Fetch the complete documentation index at: https://mintlify.com/santosdevco/firestore-pydantic-odm/llms.txt
Use this file to discover all available pages before exploring further.
FirestoreDB is a lightweight wrapper around the google.cloud.firestore_v1.AsyncClient that provides a single, consistent way to configure your Firestore connection — whether you are targeting the production service, a local emulator during development, or a MagicMock in unit tests. Once constructed, pass it to init_firestore_odm to wire up all your document models.
Constructor
Your Google Cloud project identifier (for example
"my-gcp-project"). This is passed directly to the underlying AsyncClient.The Firestore database ID to connect to. Omit (or pass
None) to use the default database "(default)".Explicit Google Auth credentials object. When
None the Google SDK’s Application Default Credentials chain is used automatically.Host and port of a running Firestore emulator such as
"localhost:8080". When provided, the constructor sets the FIRESTORE_EMULATOR_HOST environment variable and points the AsyncClient to the emulator instead of the production service.The client attribute
After construction, FirestoreDB.client holds the live AsyncClient instance. You can access it directly if you need to run raw Firestore operations outside the ODM.
The
client attribute is replaced in-place by use_emulator, clear_emulator, and mock_firestore_for_tests. Any models that already have a reference to db will automatically use the new client because BaseFirestoreModel reads db.client at call time, not at injection time.Methods
use_emulator
Switch the instance to a local Firestore emulator and recreate the AsyncClient. Sets the FIRESTORE_EMULATOR_HOST environment variable so that the Google client libraries route all traffic to the emulator.
The hostname and port where the Firestore emulator is listening. Uses
"localhost:8080" by default, which matches the default port used by firebase emulators:start.clear_emulator
Disable the emulator and reconnect to the production Firestore service. Removes the FIRESTORE_EMULATOR_HOST environment variable and recreates the AsyncClient pointing at the real backend.
mock_firestore_for_tests
Replace client with a unittest.mock.MagicMock. This is the fastest way to isolate unit tests from Firestore without running the emulator or touching the network.
After calling this method, all calls to
db.client return the mock. You can configure return values on the mock using standard unittest.mock APIs. No network calls are made.The init_firestore_odm function
init_firestore_odm is a standalone module-level function exported from firestore_pydantic_odm. It wires up one or more document model classes to a FirestoreDB instance in a single call, replacing the need to call initialize_db and initialize_fields on each class individually. It also populates the internal model registry used for cascade deletions.
The
FirestoreDB instance to inject into every model class listed in document_models.All model classes that should be registered with the ODM. Include every top-level collection model and every subcollection model — the order does not matter. The registry built from this list is used when performing cascade deletes.
