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

feat(firestore): Added multiple firestore database support #719

Open
wants to merge 2 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
26 changes: 20 additions & 6 deletions firebase_admin/firestore.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,17 @@
'to install the "google-cloud-firestore" module.')

from firebase_admin import _utils

from packaging.version import Version

_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`_.
Expand All @@ -50,15 +51,27 @@ def client(app=None):
.. _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} if database_id else None

fs_client = _utils.get_app_service(app, _FIRESTORE_ATTRIBUTE, _FirestoreClient.from_app, options=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:
if Version(firestore.__version__) < Version("2.12.0"):
raise ValueError(
'The database_id parameter is only supported for google-cloud-firestore version '
'2.12.0 and above. Please upgrade your google-cloud-firestore package to use '
'this feature.')
self._client = firestore.Client(credentials=credentials, project=project, database=database_id)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: the database parameter is only upported in firestore v2.12.0+, but this repo supports v2.9.1.

It's probably ok, but we might want to double-check that it fails gracefully when used with an older version

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wasn't sure the best way to do this but I included a version check if attempting to use older that v2.12.0

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can update the firestore version to the latest in the next release so this check probably won't be necessary (we don't check the version in other SDKs). I understand this PR has been sitting here for a while so I will first merge this and then remove the version check in a follow up PR :)

else:
self._client = firestore.Client(credentials=credentials, project=project)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should add some tests for this new functionality

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a test to cover when an explicit database id is given


def get(self):
return self._client
Expand All @@ -68,9 +81,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)
7 changes: 7 additions & 0 deletions tests/test_firestore.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ def test_service_account(self):
assert client is not None
assert client.project == 'mock-project-id'

def test_service_account_with_explicit_database_id(self):
cred = credentials.Certificate(testutils.resource_filename('service_account.json'))
firebase_admin.initialize_app(cred)
client = firestore.client(database_id='explicit-database-id')
assert client is not None
assert client._database == 'explicit-database-id'

def test_service_account_with_explicit_app(self):
cred = credentials.Certificate(testutils.resource_filename('service_account.json'))
app = firebase_admin.initialize_app(cred)
Expand Down