forked from firebase/firebase-admin-node
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Prototyped Firestore integration * Added integration tests * Fixing a test case * Exposing Firestore client directly from admin.firestore() * Merged with master; Updated tests * Updated documentation and fixed some typos; Introduced FirebaseFirestoreError type; Fixed index.d.ts by exporting Firestore as any; Added unit tests for utils.getProjectId() * Fixing an incorrect test description * Using Firestore type definitions * Using auto IDs in integration test * Some minor changes that enables building RCs from this branch. Some of these changes can be removed once the Firestore is officially released. * Supporting Firestore initialization with application default credentials, without a project ID. * Fixing up some nits * Exposing Firestore Types from admin.firestore Namespace (#5) * Upgraded to latest Firestore; Exposing FieldValue and few others via admin.firestore * Fixing Admin SDK type definitions for Firestore * Exporting all Firestore types via admin.firestore * Using lodash.assign() which only copies 'own' members and none of the inheritted members. * Declaring Firestore type in terms of admin.firestore.Firestore; More integration tests for admin.firestore namespace * Making GCS types a required dependency * Updated the Firestore dependency * Fixing a Node5 test failure * Updated shrinkwrap * Fixing NPM shrinkwrap/dependency issues
- Loading branch information
1 parent
05078a9
commit 72ab108
Showing
21 changed files
with
5,983 additions
and
49 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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 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 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 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 |
---|---|---|
@@ -0,0 +1,105 @@ | ||
/*! | ||
* Copyright 2017 Google Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import {FirebaseApp} from '../firebase-app'; | ||
import {FirebaseFirestoreError} from '../utils/error'; | ||
import {FirebaseServiceInterface, FirebaseServiceInternalsInterface} from '../firebase-service'; | ||
import {ApplicationDefaultCredential, Certificate} from '../auth/credential'; | ||
import {Firestore} from '@google-cloud/firestore'; | ||
|
||
import * as validator from '../utils/validator'; | ||
import * as utils from '../utils/index'; | ||
|
||
/** | ||
* Internals of a Firestore instance. | ||
*/ | ||
class FirestoreInternals implements FirebaseServiceInternalsInterface { | ||
/** | ||
* Deletes the service and its associated resources. | ||
* | ||
* @return {Promise<()>} An empty Promise that will be fulfilled when the service is deleted. | ||
*/ | ||
public delete(): Promise<void> { | ||
// There are no resources to clean up. | ||
return Promise.resolve(); | ||
} | ||
} | ||
|
||
function initFirestore(app: FirebaseApp): Firestore { | ||
if (!validator.isNonNullObject(app) || !('options' in app)) { | ||
throw new FirebaseFirestoreError({ | ||
code: 'invalid-argument', | ||
message: 'First argument passed to admin.firestore() must be a valid Firebase app instance.', | ||
}); | ||
} | ||
|
||
const projectId: string = utils.getProjectId(app); | ||
const cert: Certificate = app.options.credential.getCertificate(); | ||
let options: any; | ||
if (cert != null) { | ||
// cert is available when the SDK has been initialized with a service account JSON file, | ||
// or by setting the GOOGLE_APPLICATION_CREDENTIALS envrionment variable. | ||
|
||
if (!validator.isNonEmptyString(projectId)) { | ||
// Assert for an explicit projct ID (either via AppOptions or the cert itself). | ||
throw new FirebaseFirestoreError({ | ||
code: 'no-project-id', | ||
message: 'Failed to determine project ID for Firestore. Initialize the ' | ||
+ 'SDK with service account credentials or set project ID as an app option. ' | ||
+ 'Alternatively set the GCLOUD_PROJECT environment variable.', | ||
}); | ||
} | ||
options = { | ||
credentials: { | ||
private_key: cert.privateKey, | ||
client_email: cert.clientEmail, | ||
}, | ||
projectId, | ||
}; | ||
} else if (app.options.credential instanceof ApplicationDefaultCredential) { | ||
// Try to use the Google application default credentials. | ||
if (validator.isNonEmptyString(projectId)) { | ||
options = {projectId}; | ||
} else { | ||
// If an explicit project ID is not available, let Firestore client discover one from the | ||
// environment. This prevents the users from having to set GCLOUD_PROJECT in GCP runtimes. | ||
options = {}; | ||
} | ||
} else { | ||
throw new FirebaseFirestoreError({ | ||
code: 'invalid-credential', | ||
message: 'Failed to initialize Google Cloud Firestore client with the available credentials. ' + | ||
'Must initialize the SDK with a certificate credential or application default credentials ' + | ||
'to use Cloud Firestore API.', | ||
}); | ||
} | ||
return new Firestore(options); | ||
} | ||
|
||
/** | ||
* Creates a new Firestore service instance for the given FirebaseApp. | ||
* | ||
* @param {FirebaseApp} app The App for this Firestore service. | ||
* @return {FirebaseServiceInterface} A Firestore service instance. | ||
*/ | ||
export function initFirestoreService(app: FirebaseApp): FirebaseServiceInterface { | ||
let firestore: any = initFirestore(app); | ||
|
||
// Extend the Firestore client object so it implements FirebaseServiceInterface. | ||
utils.addReadonlyGetter(firestore, 'app', app); | ||
firestore.INTERNAL = new FirestoreInternals(); | ||
return firestore; | ||
} |
This file contains 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 |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import {initFirestoreService} from './firestore'; | ||
import {AppHook, FirebaseApp} from '../firebase-app'; | ||
import {FirebaseServiceInterface} from '../firebase-service'; | ||
import * as firebase from '../default-namespace'; | ||
import {FirebaseServiceNamespace} from '../firebase-namespace'; | ||
import * as firestoreNamespace from '@google-cloud/firestore'; | ||
import * as _ from 'lodash/object'; | ||
|
||
/** | ||
* Factory function that creates a new Firestore service. | ||
* | ||
* @param {Object} app The app for this service. | ||
* @param {function(Object)} extendApp An extend function to extend the app namespace. | ||
* | ||
* @return {Firestore} The Firestore service for the specified app. | ||
*/ | ||
function serviceFactory(app: FirebaseApp, extendApp: (props: Object) => void): FirebaseServiceInterface { | ||
return initFirestoreService(app); | ||
} | ||
|
||
/** | ||
* Handles app life-cycle events. | ||
* | ||
* @param {string} event The app event that is occurring. | ||
* @param {FirebaseApp} app The app for which the app hook is firing. | ||
*/ | ||
let appHook: AppHook = (event: string, app: FirebaseApp) => { | ||
return; | ||
}; | ||
|
||
export default function(): FirebaseServiceNamespace<FirebaseServiceInterface> { | ||
return firebase.INTERNAL.registerService( | ||
'firestore', | ||
serviceFactory, | ||
_.assign({}, firestoreNamespace), | ||
appHook | ||
); | ||
} |
This file contains 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 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 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 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
Oops, something went wrong.