15
15
* limitations under the License.
16
16
*/
17
17
18
- // eslint-disable-next-line import/no-extraneous-dependencies
19
18
import {
20
19
_getProvider ,
21
20
_removeServiceInstance ,
22
21
FirebaseApp ,
23
22
getApp
24
23
} from '@firebase/app' ;
25
- import { deepEqual } from '@firebase/util' ;
24
+ import { deepEqual , getDefaultEmulatorHost } from '@firebase/util' ;
26
25
27
26
import { User } from '../auth/user' ;
28
27
import {
@@ -43,7 +42,10 @@ import {
43
42
setOnlineComponentProvider
44
43
} from '../core/firestore_client' ;
45
44
import { makeDatabaseInfo } from '../lite-api/components' ;
46
- import { Firestore as LiteFirestore } from '../lite-api/database' ;
45
+ import {
46
+ Firestore as LiteFirestore ,
47
+ connectFirestoreEmulator
48
+ } from '../lite-api/database' ;
47
49
import { Query } from '../lite-api/reference' ;
48
50
import {
49
51
indexedDbClearPersistence ,
@@ -186,14 +188,6 @@ export function initializeFirestore(
186
188
} ) ;
187
189
}
188
190
189
- /**
190
- * Returns the existing default {@link Firestore} instance that is associated with the
191
- * default {@link @firebase/app#FirebaseApp }. If no instance exists, initializes a new
192
- * instance with default settings.
193
- *
194
- * @returns The {@link Firestore} instance of the provided app.
195
- */
196
- export function getFirestore ( ) : Firestore ;
197
191
/**
198
192
* Returns the existing default {@link Firestore} instance that is associated with the
199
193
* provided {@link @firebase/app#FirebaseApp }. If no instance exists, initializes a new
@@ -215,7 +209,15 @@ export function getFirestore(app: FirebaseApp): Firestore;
215
209
*/
216
210
export function getFirestore ( databaseId : string ) : Firestore ;
217
211
/**
218
- * Returns the existing {@link Firestore} instance that is associated with the
212
+ * Returns the existing default {@link Firestore} instance that is associated with the
213
+ * default {@link @firebase/app#FirebaseApp }. If no instance exists, initializes a new
214
+ * instance with default settings.
215
+ *
216
+ * @returns The {@link Firestore} instance of the provided app.
217
+ */
218
+ export function getFirestore ( ) : Firestore ;
219
+ /**
220
+ * Returns the existing default {@link Firestore} instance that is associated with the
219
221
* provided {@link @firebase/app#FirebaseApp }. If no instance exists, initializes a new
220
222
* instance with default settings.
221
223
*
@@ -236,9 +238,17 @@ export function getFirestore(
236
238
typeof appOrDatabaseId === 'string'
237
239
? appOrDatabaseId
238
240
: optionalDatabaseId || DEFAULT_DATABASE_NAME ;
239
- return _getProvider ( app , 'firestore' ) . getImmediate ( {
241
+ const db = _getProvider ( app , 'firestore' ) . getImmediate ( {
240
242
identifier : databaseId
241
243
} ) as Firestore ;
244
+ if ( ! db . _initialized ) {
245
+ const firestoreEmulatorHost = getDefaultEmulatorHost ( 'firestore' ) ;
246
+ if ( firestoreEmulatorHost ) {
247
+ const [ host , port ] = firestoreEmulatorHost . split ( ':' ) ;
248
+ connectFirestoreEmulator ( db , host , parseInt ( port , 10 ) ) ;
249
+ }
250
+ }
251
+ return db ;
242
252
}
243
253
244
254
/**
0 commit comments