-
Notifications
You must be signed in to change notification settings - Fork 324
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,11 +34,12 @@ | |
_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`_. | ||
|
@@ -50,15 +51,22 @@ 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, options, _FIRESTORE_ATTRIBUTE, _FirestoreClient.from_app) | ||
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we should add some tests for this new functionality There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
@@ -68,9 +76,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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see this is a private module, but adding a new positional argument into the middle of the list seems dangerous. Can this be an optional kwarg?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done