Skip to content
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

Add toJSON() support for Firestore classes #4298

Merged
merged 6 commits into from
Jan 26, 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
7 changes: 7 additions & 0 deletions .changeset/clean-meals-double.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@firebase/app-compat': patch
'@firebase/app': patch
'@firebase/firestore': patch
---

Firestore classes like DocumentReference and Query can now be serialized to JSON (#4258)
8 changes: 8 additions & 0 deletions packages-exp/app-compat/src/firebaseApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,14 @@ export class FirebaseAppImpl implements FirebaseApp {
_addOrOverwriteComponent(component: Component): void {
_addOrOverwriteComponent(this.app, component);
}

toJSON(): object {
return {
name: this.name,
automaticDataCollectionEnabled: this.automaticDataCollectionEnabled,
options: this.options
};
}
}

// TODO: investigate why the following needs to be commented out
Expand Down
8 changes: 8 additions & 0 deletions packages/app/src/firebaseApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,14 @@ export class FirebaseAppImpl implements FirebaseApp {
this.container.addOrOverwriteComponent(component);
}

toJSON(): object {
return {
name: this.name,
automaticDataCollectionEnabled: this.automaticDataCollectionEnabled,
options: this.options
};
}

/**
* This function will throw an Error if the App has already been deleted -
* use before performing API actions on the App.
Expand Down
8 changes: 8 additions & 0 deletions packages/app/src/lite/firebaseAppLite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,12 @@ export class FirebaseAppLiteImpl implements FirebaseApp {
throw ERROR_FACTORY.create(AppError.APP_DELETED, { appName: this.name_ });
}
}

toJSON(): object {
return {
name: this.name,
automaticDataCollectionEnabled: this.automaticDataCollectionEnabled,
options: this.options
};
}
}
5 changes: 5 additions & 0 deletions packages/app/test/firebaseApp.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,11 @@ function firebaseAppTests(
expect(() => firebase.app('toString')).throws(/'toString'.*created/i);
});

it('JSON.stringify() does not throw', () => {
const app = firebase.initializeApp({}, 'new-app');
JSON.stringify(app);
});

describe('Check for bad app names', () => {
const tests = ['', 123, false, null];
for (const data of tests) {
Expand Down
12 changes: 6 additions & 6 deletions packages/firestore/src/exp/snapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,9 +214,9 @@ export interface DocumentChange<T = DocumentData> {
* access will return 'undefined'. You can use the `exists()` method to
* explicitly verify a document's existence.
*/
export class DocumentSnapshot<T = DocumentData> extends LiteDocumentSnapshot<
T
> {
export class DocumentSnapshot<
T = DocumentData
> extends LiteDocumentSnapshot<T> {
private readonly _firestoreImpl: FirebaseFirestore;

/**
Expand Down Expand Up @@ -329,9 +329,9 @@ export class DocumentSnapshot<T = DocumentData> extends LiteDocumentSnapshot<
* `exists` property will always be true and `data()` will never return
* 'undefined'.
*/
export class QueryDocumentSnapshot<T = DocumentData> extends DocumentSnapshot<
T
> {
export class QueryDocumentSnapshot<
T = DocumentData
> extends DocumentSnapshot<T> {
/**
* Retrieves all fields in the document as an `Object`.
*
Expand Down
8 changes: 8 additions & 0 deletions packages/firestore/src/lite/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,14 @@ export class FirebaseFirestore implements FirestoreService {
return this._terminateTask;
}

toJSON(): object {
return {
app: this._app,
databaseId: this._databaseId,
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we include the app as well? I think the developers might want to see the app name here, since it acts as the main identifier for all Firebase components.

Copy link
Author

Choose a reason for hiding this comment

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

Added app.name to the JSON.

settings: this._settings
};
}

/**
* Terminates all components used by this client. Subclasses can override
* this method to clean up their own dependencies, but must also call this
Expand Down
6 changes: 3 additions & 3 deletions packages/firestore/src/lite/snapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,9 +212,9 @@ export class DocumentSnapshot<T = DocumentData> {
* `exists` property will always be true and `data()` will never return
* 'undefined'.
*/
export class QueryDocumentSnapshot<T = DocumentData> extends DocumentSnapshot<
T
> {
export class QueryDocumentSnapshot<
T = DocumentData
> extends DocumentSnapshot<T> {
/**
* Retrieves all fields in the document as an `Object`.
*
Expand Down
4 changes: 1 addition & 3 deletions packages/firestore/test/integration/api/query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,7 @@ apiDescribe('Queries', (persistence: boolean) => {
.onSnapshot(storeLimitEvent.storeEvent);

// Setup mirroring `limitToLast` query
const storeLimitToLastEvent = new EventsAccumulator<
firestore.QuerySnapshot
>();
const storeLimitToLastEvent = new EventsAccumulator<firestore.QuerySnapshot>();
let limitToLastUnlisten = collection
.orderBy('sort', 'desc')
.limitToLast(2)
Expand Down
4 changes: 1 addition & 3 deletions packages/firestore/test/integration/api/type.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,7 @@ apiDescribe('Firestore', (persistence: boolean) => {
docSnapshot = querySnapshot.docs[0];
expect(docSnapshot.data()).to.deep.equal(data);

const eventsAccumulator = new EventsAccumulator<
firestore.QuerySnapshot
>();
const eventsAccumulator = new EventsAccumulator<firestore.QuerySnapshot>();
const unlisten = collection.onSnapshot(eventsAccumulator.storeEvent);
querySnapshot = await eventsAccumulator.awaitEvent();
docSnapshot = querySnapshot.docs[0];
Expand Down
22 changes: 22 additions & 0 deletions packages/firestore/test/unit/api/database.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ describe('CollectionReference', () => {
expectEqual(collectionReference('foo'), collectionReference('foo'));
expectNotEqual(collectionReference('foo'), collectionReference('bar'));
});

it('JSON.stringify() does not throw', () => {
JSON.stringify(collectionReference('foo'));
});
});

describe('DocumentReference', () => {
Expand All @@ -42,6 +46,10 @@ describe('DocumentReference', () => {
documentReference('rooms/bar')
);
});

it('JSON.stringify() does not throw', () => {
JSON.stringify(documentReference('foo/bar'));
});
});

describe('DocumentSnapshot', () => {
Expand Down Expand Up @@ -72,13 +80,21 @@ describe('DocumentSnapshot', () => {
documentSnapshot('rooms/bar', { a: 1 }, false)
);
});

it('JSON.stringify() does not throw', () => {
JSON.stringify(documentSnapshot('foo/bar', { a: 1 }, true));
});
});

describe('Query', () => {
it('support equality checking with isEqual()', () => {
expectEqual(query('foo'), query('foo'));
expectNotEqual(query('foo'), query('bar'));
});

it('JSON.stringify() does not throw', () => {
JSON.stringify(query('foo'));
});
});

describe('QuerySnapshot', () => {
Expand Down Expand Up @@ -123,6 +139,12 @@ describe('QuerySnapshot', () => {
querySnapshot('foo', {}, { a: { a: 1 } }, keys('foo/a'), false, true)
);
});

it('JSON.stringify() does not throw', () => {
JSON.stringify(
querySnapshot('foo', {}, { a: { a: 1 } }, keys(), false, false)
);
});
});

describe('SnapshotMetadata', () => {
Expand Down
6 changes: 3 additions & 3 deletions packages/firestore/test/unit/specs/spec_builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1047,9 +1047,9 @@ export class SpecBuilder {
return {
key: SpecBuilder.keyToSpec(doc.key),
version: doc.version.toMicroseconds(),
value: userDataWriter.convertValue(doc.toProto()) as JsonObject<
unknown
>,
value: userDataWriter.convertValue(
doc.toProto()
) as JsonObject<unknown>,
options: {
hasLocalMutations: doc.hasLocalMutations,
hasCommittedMutations: doc.hasCommittedMutations
Expand Down