Skip to content

Add useFirestoreEmulator #4142

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions common/api-review/firestore-exp.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,9 @@ export function updateDoc(reference: DocumentReference<unknown>, data: UpdateDat
// @public
export function updateDoc(reference: DocumentReference<unknown>, field: string | FieldPath, value: unknown, ...moreFieldsAndValues: unknown[]): Promise<void>;

// @public
export function useFirestoreEmulator(firestore: FirebaseFirestore_2, host: string, port: number): void;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the _2 intentional?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it's because we share the code from firestore-lite and firestore-exp. This should go away once #4140 is merged.


// @public
export function waitForPendingWrites(firestore: FirebaseFirestore): Promise<void>;

Expand Down
3 changes: 3 additions & 0 deletions common/api-review/firestore-lite.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,9 @@ export function updateDoc(reference: DocumentReference<unknown>, data: UpdateDat
// @public
export function updateDoc(reference: DocumentReference<unknown>, field: string | FieldPath, value: unknown, ...moreFieldsAndValues: unknown[]): Promise<void>;

// @public
export function useFirestoreEmulator(firestore: FirebaseFirestore, host: string, port: number): void;

// @public
export function where(fieldPath: string | FieldPath, opStr: WhereFilterOp, value: unknown): QueryConstraint;

Expand Down
3 changes: 2 additions & 1 deletion packages/firestore/exp/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ export {
waitForPendingWrites,
disableNetwork,
enableNetwork,
terminate
terminate,
useFirestoreEmulator
} from '../src/exp/database';

export { Settings, PersistenceSettings } from '../src/exp/settings';
Expand Down
3 changes: 2 additions & 1 deletion packages/firestore/lite/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ export {
FirebaseFirestore,
initializeFirestore,
getFirestore,
terminate
terminate,
useFirestoreEmulator
} from '../src/lite/database';

export {
Expand Down
16 changes: 3 additions & 13 deletions packages/firestore/src/api/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ import {
enableNetwork,
ensureFirestoreConfigured,
FirebaseFirestore as ExpFirebaseFirestore,
useFirestoreEmulator,
waitForPendingWrites
} from '../exp/database';
import { FieldPath as ExpFieldPath } from '../exp/field_path';
Expand Down Expand Up @@ -95,7 +96,6 @@ import {
updateDoc,
Unsubscribe
} from '../exp/reference_impl';
import { DEFAULT_HOST } from '../exp/settings';
import {
DocumentChange as ExpDocumentChange,
DocumentSnapshot as ExpDocumentSnapshot,
Expand All @@ -118,7 +118,7 @@ import {
validateIsNotUsedTogether,
validateSetOptions
} from '../util/input_validation';
import { logWarn, setLogLevel as setClientLogLevel } from '../util/log';
import { setLogLevel as setClientLogLevel } from '../util/log';

import { Blob } from './blob';
import { LoadBundleTask } from './bundle';
Expand Down Expand Up @@ -236,17 +236,7 @@ export class Firestore
}

useEmulator(host: string, port: number): void {
if (this._delegate._getSettings().host !== DEFAULT_HOST) {
logWarn(
'Host has been set in both settings() and useEmulator(), emulator host will be used'
);
}

this.settings({
host: `${host}:${port}`,
ssl: false,
merge: true
});
useFirestoreEmulator(this._delegate, host, port);
}

enableNetwork(): Promise<void> {
Expand Down
2 changes: 2 additions & 0 deletions packages/firestore/src/exp/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ import { Deferred } from '../util/promise';

import { PersistenceSettings, Settings } from './settings';

export { useFirestoreEmulator } from '../lite/database';

/** DOMException error code constants. */
const DOM_EXCEPTION_INVALID_STATE = 11;
const DOM_EXCEPTION_ABORTED = 20;
Expand Down
42 changes: 41 additions & 1 deletion packages/firestore/src/lite/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,15 @@ import {
import { DatabaseId } from '../core/database_info';
import { Code, FirestoreError } from '../util/error';
import { cast } from '../util/input_validation';
import { logWarn } from '../util/log';

import { FirestoreService, removeComponents } from './components';
import { FirestoreSettings, PrivateSettings, Settings } from './settings';
import {
DEFAULT_HOST,
FirestoreSettings,
PrivateSettings,
Settings
} from './settings';

declare module '@firebase/component' {
interface NameServiceMapping {
Expand Down Expand Up @@ -191,6 +197,39 @@ export function getFirestore(app: FirebaseApp): FirebaseFirestore {
).getImmediate() as FirebaseFirestore;
}

/**
* Modify this instance to communicate with the Cloud Firestore emulator.
*
* Note: This must be called before this instance has been used to do any
* operations.
*
* @param firestore - The Firestore instance to configure to connect to the
* emulator.
* @param host - the emulator host (ex: localhost).
* @param port - the emulator port (ex: 9000).
*/
export function useFirestoreEmulator(
firestore: FirebaseFirestore,
host: string,
port: number
): void {
firestore = cast(firestore, FirebaseFirestore);
const settings = firestore._getSettings();

if (settings.host !== DEFAULT_HOST && settings.host !== host) {
logWarn(
'Host has been set in both settings() and useEmulator(), emulator host ' +
'will be used'
);
}

firestore._setSettings({
...settings,
host: `${host}:${port}`,
ssl: false
});
}

/**
* Terminates the provided Firestore instance.
*
Expand All @@ -207,6 +246,7 @@ export function getFirestore(app: FirebaseApp): FirebaseFirestore {
* its resources or in combination with {@link clearIndexedDbPersistence} to
* ensure that all local state is destroyed between test runs.
*
* @param firestore - The Firestore instance to terminate.
* @returns A promise that is resolved when the instance has been successfully
* terminated.
*/
Expand Down