firestore-internal.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*! firebase-admin v12.1.1 */
  2. "use strict";
  3. /*!
  4. * @license
  5. * Copyright 2017 Google Inc.
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. */
  19. Object.defineProperty(exports, "__esModule", { value: true });
  20. exports.getFirestoreOptions = exports.FirestoreService = void 0;
  21. const error_1 = require("../utils/error");
  22. const credential_internal_1 = require("../app/credential-internal");
  23. const validator = require("../utils/validator");
  24. const utils = require("../utils/index");
  25. class FirestoreService {
  26. constructor(app) {
  27. this.databases = new Map();
  28. this.firestoreSettings = new Map();
  29. this.appInternal = app;
  30. }
  31. initializeDatabase(databaseId, settings) {
  32. const existingInstance = this.databases.get(databaseId);
  33. if (existingInstance) {
  34. const initialSettings = this.firestoreSettings.get(databaseId) ?? {};
  35. if (this.checkIfSameSettings(settings, initialSettings)) {
  36. return existingInstance;
  37. }
  38. throw new error_1.FirebaseFirestoreError({
  39. code: 'failed-precondition',
  40. message: 'initializeFirestore() has already been called with ' +
  41. 'different options. To avoid this error, call initializeFirestore() with the ' +
  42. 'same options as when it was originally called, or call getFirestore() to return the' +
  43. ' already initialized instance.'
  44. });
  45. }
  46. const newInstance = initFirestore(this.app, databaseId, settings);
  47. this.databases.set(databaseId, newInstance);
  48. this.firestoreSettings.set(databaseId, settings);
  49. return newInstance;
  50. }
  51. getDatabase(databaseId) {
  52. let database = this.databases.get(databaseId);
  53. if (database === undefined) {
  54. database = initFirestore(this.app, databaseId, {});
  55. this.databases.set(databaseId, database);
  56. this.firestoreSettings.set(databaseId, {});
  57. }
  58. return database;
  59. }
  60. checkIfSameSettings(settingsA, settingsB) {
  61. const a = settingsA ?? {};
  62. const b = settingsB ?? {};
  63. // If we start passing more settings to Firestore constructor,
  64. // replace this with deep equality check.
  65. return (a.preferRest === b.preferRest);
  66. }
  67. /**
  68. * Returns the app associated with this Storage instance.
  69. *
  70. * @returns The app associated with this Storage instance.
  71. */
  72. get app() {
  73. return this.appInternal;
  74. }
  75. }
  76. exports.FirestoreService = FirestoreService;
  77. function getFirestoreOptions(app, firestoreSettings) {
  78. if (!validator.isNonNullObject(app) || !('options' in app)) {
  79. throw new error_1.FirebaseFirestoreError({
  80. code: 'invalid-argument',
  81. message: 'First argument passed to admin.firestore() must be a valid Firebase app instance.',
  82. });
  83. }
  84. const projectId = utils.getExplicitProjectId(app);
  85. const credential = app.options.credential;
  86. // eslint-disable-next-line @typescript-eslint/no-var-requires
  87. const { version: firebaseVersion } = require('../../package.json');
  88. const preferRest = firestoreSettings?.preferRest;
  89. if (credential instanceof credential_internal_1.ServiceAccountCredential) {
  90. return {
  91. credentials: {
  92. private_key: credential.privateKey,
  93. client_email: credential.clientEmail,
  94. },
  95. // When the SDK is initialized with ServiceAccountCredentials an explicit projectId is
  96. // guaranteed to be available.
  97. projectId: projectId,
  98. firebaseVersion,
  99. preferRest,
  100. };
  101. }
  102. else if ((0, credential_internal_1.isApplicationDefault)(app.options.credential)) {
  103. // Try to use the Google application default credentials.
  104. // If an explicit project ID is not available, let Firestore client discover one from the
  105. // environment. This prevents the users from having to set GOOGLE_CLOUD_PROJECT in GCP runtimes.
  106. return validator.isNonEmptyString(projectId)
  107. ? { projectId, firebaseVersion, preferRest }
  108. : { firebaseVersion, preferRest };
  109. }
  110. throw new error_1.FirebaseFirestoreError({
  111. code: 'invalid-credential',
  112. message: 'Failed to initialize Google Cloud Firestore client with the available credentials. ' +
  113. 'Must initialize the SDK with a certificate credential or application default credentials ' +
  114. 'to use Cloud Firestore API.',
  115. });
  116. }
  117. exports.getFirestoreOptions = getFirestoreOptions;
  118. function initFirestore(app, databaseId, firestoreSettings) {
  119. const options = getFirestoreOptions(app, firestoreSettings);
  120. options.databaseId = databaseId;
  121. let firestoreDatabase;
  122. try {
  123. // Lazy-load the Firestore implementation here, which in turns loads gRPC.
  124. firestoreDatabase = require('@google-cloud/firestore').Firestore;
  125. }
  126. catch (err) {
  127. throw new error_1.FirebaseFirestoreError({
  128. code: 'missing-dependencies',
  129. message: 'Failed to import the Cloud Firestore client library for Node.js. '
  130. + 'Make sure to install the "@google-cloud/firestore" npm package. '
  131. + `Original error: ${err}`,
  132. });
  133. }
  134. return new firestoreDatabase(options);
  135. }