Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added multiple Firestore databases support #728

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion firebase_admin/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,10 @@ def _get_initialized_app(app):



def get_app_service(app, name, initializer):
def get_app_service(app, name, initializer, options=None):
app = _get_initialized_app(app)
if options:
app.options._options.update(options)
return app._get_service(name, initializer) # pylint: disable=protected-access


Expand Down
21 changes: 14 additions & 7 deletions firebase_admin/firestore.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,31 +34,37 @@
_FIRESTORE_ATTRIBUTE = '_firestore'


def client(app=None):
def client(app=None, database_id=None):
"""Returns a client that can be used to interact with Google Cloud Firestore.

Args:
app: An App instance (optional).

database_id: The ID of the Google Cloud Firestore database to use. If none provided, default database will be used (optional).

Returns:
google.cloud.firestore.Firestore: A `Firestore Client`_.

Raises:
ValueError: If a project ID is not specified either via options, credentials or
environment variables, or if the specified project ID is not a valid string.

.. _Firestore Client: https://googlecloudplatform.github.io/google-cloud-python/latest\
/firestore/client.html
"""
fs_client = _utils.get_app_service(app, _FIRESTORE_ATTRIBUTE, _FirestoreClient.from_app)
options = {"database_id": database_id}

fs_client = _utils.get_app_service(app, _FIRESTORE_ATTRIBUTE, _FirestoreClient.from_app, options)
return fs_client.get()


class _FirestoreClient:
"""Holds a Google Cloud Firestore client instance."""

def __init__(self, credentials, project):
self._client = firestore.Client(credentials=credentials, project=project)
def __init__(self, credentials, project, database_id=None):
if database_id:
self._client = firestore.Client(credentials=credentials, project=project, database=database_id)
else:
self._client = firestore.Client(credentials=credentials, project=project)

def get(self):
return self._client
Expand All @@ -68,9 +74,10 @@ def from_app(cls, app):
"""Creates a new _FirestoreClient for the specified app."""
credentials = app.credential.get_credential()
project = app.project_id
database_id = app.options.get('database_id')
if not project:
raise ValueError(
'Project ID is required to access Firestore. Either set the projectId option, '
'or use service account credentials. Alternatively, set the GOOGLE_CLOUD_PROJECT '
'environment variable.')
return _FirestoreClient(credentials, project)
return _FirestoreClient(credentials, project, database_id)