Skip to content

Commit 4df007f

Browse files
Add useFirestoreEmulator
1 parent 8993f16 commit 4df007f

File tree

7 files changed

+49
-15
lines changed

7 files changed

+49
-15
lines changed

common/api-review/firestore-exp.api.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,9 @@ export function updateDoc(reference: DocumentReference<unknown>, data: UpdateDat
521521
// @public
522522
export function updateDoc(reference: DocumentReference<unknown>, field: string | FieldPath, value: unknown, ...moreFieldsAndValues: unknown[]): Promise<void>;
523523

524+
// @public
525+
export function useFirestoreEmulator(firestore: FirebaseFirestore_2, host: string, port: number): void;
526+
524527
// @public
525528
export function waitForPendingWrites(firestore: FirebaseFirestore): Promise<void>;
526529

common/api-review/firestore-lite.api.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,9 @@ export function updateDoc(reference: DocumentReference<unknown>, data: UpdateDat
427427
// @public
428428
export function updateDoc(reference: DocumentReference<unknown>, field: string | FieldPath, value: unknown, ...moreFieldsAndValues: unknown[]): Promise<void>;
429429

430+
// @public
431+
export function useFirestoreEmulator(firestore: FirebaseFirestore, host: string, port: number): void;
432+
430433
// @public
431434
export function where(fieldPath: string | FieldPath, opStr: WhereFilterOp, value: unknown): QueryConstraint;
432435

packages/firestore/exp/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ export {
3333
enableNetwork,
3434
terminate,
3535
Settings,
36-
PersistenceSettings
36+
PersistenceSettings,
37+
useFirestoreEmulator
3738
} from './src/api/database';
3839

3940
export {

packages/firestore/exp/src/api/database.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ import {
5454
} from '../../../src/local/indexeddb_persistence';
5555
import { cast } from '../../../src/util/input_validation';
5656

57+
export { useFirestoreEmulator } from '../../../lite/src/api/database';
58+
5759
/** DOMException error code constants. */
5860
const DOM_EXCEPTION_INVALID_STATE = 11;
5961
const DOM_EXCEPTION_ABORTED = 20;

packages/firestore/lite/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ export {
2424
FirebaseFirestore,
2525
initializeFirestore,
2626
getFirestore,
27-
terminate
27+
terminate,
28+
useFirestoreEmulator
2829
} from './src/api/database';
2930

3031
export {

packages/firestore/lite/src/api/database.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import {
3939
cast,
4040
validateIsNotUsedTogether
4141
} from '../../../src/util/input_validation';
42+
import { logWarn } from '../../../src/util/log';
4243

4344
declare module '@firebase/component' {
4445
interface NameServiceMapping {
@@ -300,6 +301,38 @@ export function getFirestore(app: FirebaseApp): FirebaseFirestore {
300301
).getImmediate() as FirebaseFirestore;
301302
}
302303

304+
/**
305+
* Modify this instance to communicate with the Cloud Firestore emulator.
306+
*
307+
* Note: This must be called before this instance has been used to do any
308+
* operations.
309+
*
310+
* @param firestore - The Firestore instance to configure to connect to the
311+
* emulator.
312+
* @param host - the emulator host (ex: localhost).
313+
* @param port - the emulator port (ex: 9000).
314+
*/
315+
export function useFirestoreEmulator(
316+
firestore: FirebaseFirestore,
317+
host: string,
318+
port: number
319+
): void {
320+
firestore = cast(firestore, FirebaseFirestore);
321+
322+
if (firestore._getSettings().host !== DEFAULT_HOST) {
323+
logWarn(
324+
'Host has been set in both settings() and useEmulator(), emulator host ' +
325+
'will be used'
326+
);
327+
}
328+
329+
firestore._setSettings({
330+
...firestore._getSettings(),
331+
host: `${host}:${port}`,
332+
ssl: false
333+
});
334+
}
335+
303336
/**
304337
* Terminates the provided Firestore instance.
305338
*
@@ -316,6 +349,7 @@ export function getFirestore(app: FirebaseApp): FirebaseFirestore {
316349
* its resources or in combination with {@link clearIndexedDbPersistence} to
317350
* ensure that all local state is destroyed between test runs.
318351
*
352+
* @param firestore - The Firestore instance to terminate.
319353
* @returns A promise that is resolved when the instance has been successfully
320354
* terminated.
321355
*/

packages/firestore/src/api/database.ts

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import {
3232
validateIsNotUsedTogether,
3333
validateSetOptions
3434
} from '../util/input_validation';
35-
import { logWarn, setLogLevel as setClientLogLevel } from '../util/log';
35+
import { setLogLevel as setClientLogLevel } from '../util/log';
3636
import { FieldPath as ExpFieldPath } from '../../lite/src/api/field_path';
3737
import {
3838
CompleteFn,
@@ -52,6 +52,7 @@ import {
5252
enableNetwork,
5353
FirebaseFirestore,
5454
FirebaseFirestore as ExpFirebaseFirestore,
55+
useFirestoreEmulator,
5556
waitForPendingWrites
5657
} from '../../exp/src/api/database';
5758
import {
@@ -96,7 +97,6 @@ import { LRU_COLLECTION_DISABLED } from '../local/lru_garbage_collector';
9697
import { Compat } from '../compat/compat';
9798
import { ApiLoadBundleTask, LoadBundleTask } from './bundle';
9899
import { makeDatabaseInfo } from '../../lite/src/api/database';
99-
import { DEFAULT_HOST } from '../../lite/src/api/components';
100100
import { WriteBatch as ExpWriteBatch } from '../../exp/src/api/write_batch';
101101
import {
102102
runTransaction,
@@ -241,17 +241,7 @@ export class Firestore
241241
}
242242

243243
useEmulator(host: string, port: number): void {
244-
if (this._delegate._getSettings().host !== DEFAULT_HOST) {
245-
logWarn(
246-
'Host has been set in both settings() and useEmulator(), emulator host will be used'
247-
);
248-
}
249-
250-
this.settings({
251-
host: `${host}:${port}`,
252-
ssl: false,
253-
merge: true
254-
});
244+
useFirestoreEmulator(this._delegate, host, port);
255245
}
256246

257247
enableNetwork(): Promise<void> {

0 commit comments

Comments
 (0)