Skip to content

Commit 61b4cd3

Browse files
author
Brian Chen
authored
add option to merge settings (#3464)
1 parent 8097b97 commit 61b4cd3

File tree

7 files changed

+78
-7
lines changed

7 files changed

+78
-7
lines changed

.changeset/mean-jokes-tan.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
'firebase': patch
3+
'@firebase/firestore': patch
4+
'@firebase/firestore-types': patch
5+
---
6+
feat: Added `merge` option to `firestore.settings()`, which merges the provided settings with
7+
settings from a previous call. This allows adding settings on top of the settings that were applied
8+
by `@firebase/testing`.

packages/firebase/index.d.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7891,10 +7891,18 @@ declare namespace firebase.firestore {
78917891
/**
78927892
* Whether to skip nested properties that are set to `undefined` during
78937893
* object serialization. If set to `true`, these properties are skipped
7894-
* and not written to Firestore. If set `false` or omitted, the SDK throws
7895-
* an exception when it encounters properties of type `undefined`.
7894+
* and not written to Firestore. If set to `false` or omitted, the SDK
7895+
* throws an exception when it encounters properties of type `undefined`.
78967896
*/
78977897
ignoreUndefinedProperties?: boolean;
7898+
7899+
/**
7900+
* Whether to merge the provided settings with the existing settings. If
7901+
* set to `true`, the settings are merged with existing settings. If
7902+
* set to `false` or left unset, the settings replace the existing
7903+
* settings.
7904+
*/
7905+
merge?: boolean;
78987906
}
78997907

79007908
/**

packages/firestore-types/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export interface Settings {
3030
cacheSizeBytes?: number;
3131
experimentalForceLongPolling?: boolean;
3232
ignoreUndefinedProperties?: boolean;
33+
merge?: boolean;
3334
}
3435

3536
export interface PersistenceSettings {

packages/firestore/src/api/database.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ class FirestoreSettings {
160160

161161
readonly cacheSizeBytes: number;
162162

163-
readonly forceLongPolling: boolean;
163+
readonly experimentalForceLongPolling: boolean;
164164

165165
readonly ignoreUndefinedProperties: boolean;
166166

@@ -263,7 +263,7 @@ class FirestoreSettings {
263263
'experimentalForceLongPolling',
264264
settings.experimentalForceLongPolling
265265
);
266-
this.forceLongPolling =
266+
this.experimentalForceLongPolling =
267267
settings.experimentalForceLongPolling ?? DEFAULT_FORCE_LONG_POLLING;
268268
}
269269

@@ -274,7 +274,8 @@ class FirestoreSettings {
274274
this.timestampsInSnapshots === other.timestampsInSnapshots &&
275275
this.credentials === other.credentials &&
276276
this.cacheSizeBytes === other.cacheSizeBytes &&
277-
this.forceLongPolling === other.forceLongPolling &&
277+
this.experimentalForceLongPolling ===
278+
other.experimentalForceLongPolling &&
278279
this.ignoreUndefinedProperties === other.ignoreUndefinedProperties
279280
);
280281
}
@@ -361,6 +362,12 @@ export class Firestore implements firestore.FirebaseFirestore, FirebaseService {
361362
validateExactNumberOfArgs('Firestore.settings', arguments, 1);
362363
validateArgType('Firestore.settings', 'object', 1, settingsLiteral);
363364

365+
if (settingsLiteral.merge) {
366+
settingsLiteral = { ...this._settings, ...settingsLiteral };
367+
// Remove the property from the settings once the merge is completed
368+
delete settingsLiteral.merge;
369+
}
370+
364371
const newSettings = new FirestoreSettings(settingsLiteral);
365372
if (this._firestoreClient && !this._settings.isEqual(newSettings)) {
366373
throw new FirestoreError(
@@ -516,7 +523,7 @@ export class Firestore implements firestore.FirebaseFirestore, FirebaseService {
516523
this._persistenceKey,
517524
this._settings.host,
518525
this._settings.ssl,
519-
this._settings.forceLongPolling
526+
this._settings.experimentalForceLongPolling
520527
);
521528
}
522529

@@ -681,6 +688,11 @@ export class Firestore implements firestore.FirebaseFirestore, FirebaseService {
681688
_areTimestampsInSnapshotsEnabled(): boolean {
682689
return this._settings.timestampsInSnapshots;
683690
}
691+
692+
// Visible for testing.
693+
_getSettings(): firestore.Settings {
694+
return this._settings;
695+
}
684696
}
685697

686698
/**

packages/firestore/test/integration/api/database.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1266,7 +1266,7 @@ apiDescribe('Database', (persistence: boolean) => {
12661266
});
12671267
});
12681268

1269-
it('calling terminate mutiple times should proceed', async () => {
1269+
it('calling terminate multiple times should proceed', async () => {
12701270
await withTestDoc(persistence, async docRef => {
12711271
const firestore = docRef.firestore;
12721272
await firestore.terminate();

packages/firestore/test/unit/api/database.test.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@ import {
1919
collectionReference,
2020
documentReference,
2121
documentSnapshot,
22+
newTestFirestore,
2223
query,
2324
querySnapshot
2425
} from '../../util/api_helpers';
2526
import { expectEqual, expectNotEqual, keys } from '../../util/helpers';
27+
import { expect } from 'chai';
2628

2729
describe('CollectionReference', () => {
2830
it('support equality checking with isEqual()', () => {
@@ -153,3 +155,31 @@ describe('SnapshotMetadata', () => {
153155
);
154156
});
155157
});
158+
159+
describe('Settings', () => {
160+
it('replaces settings by default', () => {
161+
// Use a new instance of Firestore in order to configure settings.
162+
const firestoreClient = newTestFirestore();
163+
firestoreClient.settings({ host: 'other.host' });
164+
firestoreClient.settings({ ignoreUndefinedProperties: true });
165+
166+
expect(firestoreClient._getSettings().ignoreUndefinedProperties).to.be.true;
167+
// Expect host to be replaced with default host.
168+
expect(firestoreClient._getSettings().host).to.equal(
169+
'firestore.googleapis.com'
170+
);
171+
});
172+
173+
it('can merge settings', () => {
174+
// Use a new instance of Firestore in order to configure settings.
175+
const firestoreClient = newTestFirestore();
176+
firestoreClient.settings({ host: 'other.host' });
177+
firestoreClient.settings({
178+
ignoreUndefinedProperties: true,
179+
merge: true
180+
});
181+
182+
expect(firestoreClient._getSettings().ignoreUndefinedProperties).to.be.true;
183+
expect(firestoreClient._getSettings().host).to.equal('other.host');
184+
});
185+
});

packages/firestore/test/util/api_helpers.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,18 @@ export function firestore(): Firestore {
6565
return FIRESTORE;
6666
}
6767

68+
export function newTestFirestore(): Firestore {
69+
return new Firestore(
70+
{
71+
projectId: 'new-project',
72+
database: '(default)'
73+
},
74+
new Provider('auth-internal', new ComponentContainer('default')),
75+
offlineComponentProvider,
76+
onlineComponentProvider
77+
);
78+
}
79+
6880
export function collectionReference(path: string): CollectionReference {
6981
const firestoreClient = firestore();
7082
// eslint-disable-next-line @typescript-eslint/no-floating-promises

0 commit comments

Comments
 (0)