- 
                Notifications
    You must be signed in to change notification settings 
- Fork 344
feat(firestore): Added multiple firestore database support #719
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
          
     Closed
      
      
    
  
     Closed
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            2 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      
    File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
|  | @@ -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`_. | ||
|  | @@ -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) | ||
| 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 +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) | ||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
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.
note: the
databaseparameter 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
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.
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 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 :)