/
firestore-internal.ts
135 lines (121 loc) · 4.74 KB
/
firestore-internal.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*!
* @license
* 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 { FirebaseFirestoreError } from '../utils/error';
import { ServiceAccountCredential, isApplicationDefault } from '../app/credential-internal';
import { Firestore, Settings } from '@google-cloud/firestore';
import * as validator from '../utils/validator';
import * as utils from '../utils/index';
import { App } from '../app';
/**
* Settings to pass to the Firestore constructor.
*
* @public
*/
export interface FirestoreSettings {
/**
* Use HTTP/1.1 REST transport where possible.
*
* @defaultValue `undefined`
*/
preferRest?: boolean;
}
export class FirestoreService {
private readonly appInternal: App;
private readonly databases: Map<string, Firestore> = new Map();
private readonly firestoreSettings: FirestoreSettings;
constructor(app: App, firestoreSettings?: FirestoreSettings) {
this.appInternal = app;
this.firestoreSettings = firestoreSettings ?? {};
}
getDatabase(databaseId: string): Firestore {
let database = this.databases.get(databaseId);
if (database === undefined) {
database = initFirestore(this.app, databaseId, this.firestoreSettings);
this.databases.set(databaseId, database);
}
return database;
}
checkIfSameSettings(firestoreSettings: FirestoreSettings): boolean {
// If we start passing more settings to Firestore constructor,
// replace this with deep equality check.
return (firestoreSettings.preferRest === this.firestoreSettings.preferRest);
}
/**
* Returns the app associated with this Storage instance.
*
* @returns The app associated with this Storage instance.
*/
get app(): App {
return this.appInternal;
}
}
export function getFirestoreOptions(app: App, firestoreSettings?: FirestoreSettings): Settings {
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 | null = utils.getExplicitProjectId(app);
const credential = app.options.credential;
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { version: firebaseVersion } = require('../../package.json');
const preferRest = firestoreSettings?.preferRest;
if (credential instanceof ServiceAccountCredential) {
return {
credentials: {
private_key: credential.privateKey,
client_email: credential.clientEmail,
},
// When the SDK is initialized with ServiceAccountCredentials an explicit projectId is
// guaranteed to be available.
projectId: projectId!,
firebaseVersion,
preferRest,
};
} else if (isApplicationDefault(app.options.credential)) {
// Try to use the Google application default credentials.
// If an explicit project ID is not available, let Firestore client discover one from the
// environment. This prevents the users from having to set GOOGLE_CLOUD_PROJECT in GCP runtimes.
return validator.isNonEmptyString(projectId)
? { projectId, firebaseVersion, preferRest }
: { firebaseVersion, preferRest };
}
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.',
});
}
function initFirestore(app: App, databaseId: string, firestoreSettings?: FirestoreSettings): Firestore {
const options = getFirestoreOptions(app, firestoreSettings);
options.databaseId = databaseId;
let firestoreDatabase: typeof Firestore;
try {
// Lazy-load the Firestore implementation here, which in turns loads gRPC.
firestoreDatabase = require('@google-cloud/firestore').Firestore;
} catch (err) {
throw new FirebaseFirestoreError({
code: 'missing-dependencies',
message: 'Failed to import the Cloud Firestore client library for Node.js. '
+ 'Make sure to install the "@google-cloud/firestore" npm package. '
+ `Original error: ${err}`,
});
}
return new firestoreDatabase(options);
}