Skip to content

add option to merge settings #3464

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 13 commits into from
Aug 19, 2020
7 changes: 7 additions & 0 deletions .changeset/mean-jokes-tan.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'firebase': patch
'@firebase/firestore': patch
'@firebase/firestore-types': patch
---

feat: Added `inherit` setting to merge existing settings with new settings when calling `firestore.settings()`.
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe something with "the settings from a previous call. This allows customizing custom settings on top of the settings that were applied by @firestore/testing. Yadadada."

Copy link
Author

Choose a reason for hiding this comment

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

updated.

12 changes: 10 additions & 2 deletions packages/firebase/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7758,10 +7758,18 @@ declare namespace firebase.firestore {
/**
* Whether to skip nested properties that are set to `undefined` during
* object serialization. If set to `true`, these properties are skipped
* and not written to Firestore. If set `false` or omitted, the SDK throws
* an exception when it encounters properties of type `undefined`.
* and not written to Firestore. If set to `false` or omitted, the SDK
* throws an exception when it encounters properties of type `undefined`.
*/
ignoreUndefinedProperties?: boolean;

/**
* Whether to merge the provided settings with the existing settings. If
* set to `true`, the settings will be merged with existing settings. If
Copy link
Contributor

Choose a reason for hiding this comment

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

We avoid "will" when possible. So this could be "are merged" and the line below just "settings replace..."

* set to `false` or left unset, the settings will replace the existing
* settings.
*/
inherit?: boolean;
}

/**
Expand Down
1 change: 1 addition & 0 deletions packages/firestore-types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export interface Settings {
cacheSizeBytes?: number;
experimentalForceLongPolling?: boolean;
ignoreUndefinedProperties?: boolean;
inherit?: boolean;
}

export interface PersistenceSettings {
Expand Down
38 changes: 38 additions & 0 deletions packages/firestore/src/api/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,12 @@ export class Firestore implements firestore.FirebaseFirestore, FirebaseService {
validateExactNumberOfArgs('Firestore.settings', arguments, 1);
validateArgType('Firestore.settings', 'object', 1, settingsLiteral);

if (settingsLiteral.inherit) {
settingsLiteral = this.mergeSettings(settingsLiteral);
// Remove the property from the settings once the merge is completed.
delete settingsLiteral.inherit;
}

const newSettings = new FirestoreSettings(settingsLiteral);
if (this._firestoreClient && !this._settings.isEqual(newSettings)) {
throw new FirestoreError(
Expand All @@ -379,6 +385,33 @@ export class Firestore implements firestore.FirebaseFirestore, FirebaseService {
}
}

private mergeSettings(
settingsLiteral: firestore.Settings
): firestore.Settings {
settingsLiteral.host =
settingsLiteral.host === undefined
? this._settings.host
: settingsLiteral.host;
settingsLiteral.ssl =
settingsLiteral.ssl === undefined
? this._settings.ssl
: settingsLiteral.ssl;
settingsLiteral.timestampsInSnapshots =
settingsLiteral.timestampsInSnapshots === undefined
? this._settings.timestampsInSnapshots
: settingsLiteral.timestampsInSnapshots;
settingsLiteral.cacheSizeBytes =
settingsLiteral.cacheSizeBytes === undefined
? this._settings.cacheSizeBytes
: settingsLiteral.cacheSizeBytes;
settingsLiteral.ignoreUndefinedProperties =
settingsLiteral.ignoreUndefinedProperties === undefined
? this._settings.ignoreUndefinedProperties
: settingsLiteral.ignoreUndefinedProperties;

return settingsLiteral;
Copy link
Contributor

Choose a reason for hiding this comment

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

return {...this._settings, ...settingsLiteral }

Copy link
Author

Choose a reason for hiding this comment

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

done. Renamed forceLongPolling to experimentalForceLongPolling in order to use spread operator properly.

}

enableNetwork(): Promise<void> {
this.ensureClientConfigured();
return this._firestoreClient!.enableNetwork();
Expand Down Expand Up @@ -674,6 +707,11 @@ export class Firestore implements firestore.FirebaseFirestore, FirebaseService {
_areTimestampsInSnapshotsEnabled(): boolean {
return this._settings.timestampsInSnapshots;
}

// Visible for testing.
_getSettings(): firestore.Settings {
return this._settings;
}
}

/** Registers the listener for onSnapshotsInSync() */
Expand Down
2 changes: 1 addition & 1 deletion packages/firestore/test/integration/api/database.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1219,7 +1219,7 @@ apiDescribe('Database', (persistence: boolean) => {
});
});

it('calling terminate mutiple times should proceed', async () => {
it('calling terminate multiple times should proceed', async () => {
await withTestDoc(persistence, async docRef => {
const firestore = docRef.firestore;
await firestore.terminate();
Expand Down
30 changes: 30 additions & 0 deletions packages/firestore/test/unit/api/database.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ import {
collectionReference,
documentReference,
documentSnapshot,
namedFirestore,
query,
querySnapshot
} from '../../util/api_helpers';
import { expectEqual, expectNotEqual, keys } from '../../util/helpers';
import { expect } from 'chai';

describe('CollectionReference', () => {
it('support equality checking with isEqual()', () => {
Expand Down Expand Up @@ -153,3 +155,31 @@ describe('SnapshotMetadata', () => {
);
});
});

describe('Settings', () => {
it('replaces settings by default', () => {
// Use a new instance of Firestore in order to configure settings.
const firestoreClient = namedFirestore('new-project');
firestoreClient.settings({ host: 'other.host' });
firestoreClient.settings({ ignoreUndefinedProperties: true });

expect(firestoreClient._getSettings().ignoreUndefinedProperties).to.be.true;
// Expect host to be replaced with default host.
expect(firestoreClient._getSettings().host).to.equal(
'firestore.googleapis.com'
);
});

it('can merge settings', () => {
// Use a new instance of Firestore in order to configure settings.
const firestoreClient = namedFirestore('new-project');
firestoreClient.settings({ host: 'other.host' });
firestoreClient.settings({
ignoreUndefinedProperties: true,
inherit: true
});

expect(firestoreClient._getSettings().ignoreUndefinedProperties).to.be.true;
expect(firestoreClient._getSettings().host).to.equal('other.host');
});
});
11 changes: 11 additions & 0 deletions packages/firestore/test/util/api_helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,17 @@ export function firestore(): Firestore {
return FIRESTORE;
}

export function namedFirestore(projectId: string): Firestore {
return new Firestore(
Copy link
Contributor

Choose a reason for hiding this comment

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

You don't need the name, do you? I think you just need to return a new instance. This could just be newTestFirstore().

Copy link
Author

Choose a reason for hiding this comment

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

done.

{
projectId,
database: '(default)'
},
new Provider('auth-internal', new ComponentContainer('default')),
new MultiTabIndexedDbComponentProvider()
);
}

export function collectionReference(path: string): CollectionReference {
const firestoreClient = firestore();
// eslint-disable-next-line @typescript-eslint/no-floating-promises
Expand Down