Skip to content

Commit 90ade11

Browse files
committed
Add QuerySnapshot.fromJSON converter variant.
1 parent 52bc217 commit 90ade11

File tree

3 files changed

+65
-5
lines changed

3 files changed

+65
-5
lines changed

common/api-review/firestore.api.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -655,7 +655,8 @@ export class QuerySnapshot<AppModelType = DocumentData, DbModelType extends Docu
655655
get docs(): Array<QueryDocumentSnapshot<AppModelType, DbModelType>>;
656656
get empty(): boolean;
657657
forEach(callback: (result: QueryDocumentSnapshot<AppModelType, DbModelType>) => void, thisArg?: unknown): void;
658-
static fromJSON<AppModelType, DbModelType extends DocumentData = DocumentData>(db: Firestore, json: object): QuerySnapshot<AppModelType, DbModelType>;
658+
static fromJSON(db: Firestore, json: object): QuerySnapshot;
659+
static fromJSONUsingConverter<AppModelType, DbModelType extends DocumentData = DocumentData>(db: Firestore, json: object, converter: FirestoreDataConverter<AppModelType, DbModelType>): QuerySnapshot<AppModelType, DbModelType>;
659660
readonly metadata: SnapshotMetadata;
660661
readonly query: Query<AppModelType, DbModelType>;
661662
get size(): number;

docs-devsite/firestore_.querysnapshot.md

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export declare class QuerySnapshot<AppModelType = DocumentData, DbModelType exte
3535
| [docChanges(options)](./firestore_.querysnapshot.md#querysnapshotdocchanges) | | Returns an array of the documents changes since the last snapshot. If this is the first snapshot, all documents will be in the list as 'added' changes. |
3636
| [forEach(callback, thisArg)](./firestore_.querysnapshot.md#querysnapshotforeach) | | Enumerates all of the documents in the <code>QuerySnapshot</code>. |
3737
| [fromJSON(db, json)](./firestore_.querysnapshot.md#querysnapshotfromjson) | <code>static</code> | Builds a <code>QuerySnapshot</code> instance from a JSON object created by [QuerySnapshot.toJSON()](./firestore_.querysnapshot.md#querysnapshottojson)<!-- -->. |
38+
| [fromJSONUsingConverter(db, json, converter)](./firestore_.querysnapshot.md#querysnapshotfromjsonusingconverter) | <code>static</code> | Builds a <code>QuerySnapshot</code> instance from a JSON object created by [QuerySnapshot.toJSON()](./firestore_.querysnapshot.md#querysnapshottojson)<!-- -->. |
3839
| [toJSON()](./firestore_.querysnapshot.md#querysnapshottojson) | | Returns a JSON-serializable representation of this <code>QuerySnapshot</code> instance. |
3940

4041
## QuerySnapshot.docs
@@ -135,7 +136,7 @@ Builds a `QuerySnapshot` instance from a JSON object created by [QuerySnapshot.t
135136
<b>Signature:</b>
136137

137138
```typescript
138-
static fromJSON<AppModelType, DbModelType extends DocumentData = DocumentData>(db: Firestore, json: object): QuerySnapshot<AppModelType, DbModelType>;
139+
static fromJSON(db: Firestore, json: object): QuerySnapshot;
139140
```
140141

141142
#### Parameters
@@ -147,6 +148,30 @@ static fromJSON<AppModelType, DbModelType extends DocumentData = DocumentData>(d
147148

148149
<b>Returns:</b>
149150

151+
[QuerySnapshot](./firestore_.querysnapshot.md#querysnapshot_class)
152+
153+
an instance of [QuerySnapshot](./firestore_.querysnapshot.md#querysnapshot_class) if the JSON object could be parsed. Throws a [FirestoreError](./firestore_.firestoreerror.md#firestoreerror_class) if an error occurs.
154+
155+
## QuerySnapshot.fromJSONUsingConverter()
156+
157+
Builds a `QuerySnapshot` instance from a JSON object created by [QuerySnapshot.toJSON()](./firestore_.querysnapshot.md#querysnapshottojson)<!-- -->.
158+
159+
<b>Signature:</b>
160+
161+
```typescript
162+
static fromJSONUsingConverter<AppModelType, DbModelType extends DocumentData = DocumentData>(db: Firestore, json: object, converter: FirestoreDataConverter<AppModelType, DbModelType>): QuerySnapshot<AppModelType, DbModelType>;
163+
```
164+
165+
#### Parameters
166+
167+
| Parameter | Type | Description |
168+
| --- | --- | --- |
169+
| db | [Firestore](./firestore_.firestore.md#firestore_class) | |
170+
| json | object | a JSON object represention of a <code>QuerySnapshot</code> instance. |
171+
| converter | [FirestoreDataConverter](./firestore_.firestoredataconverter.md#firestoredataconverter_interface)<!-- -->&lt;AppModelType, DbModelType&gt; | Converts objects to and from Firestore. |
172+
173+
<b>Returns:</b>
174+
150175
[QuerySnapshot](./firestore_.querysnapshot.md#querysnapshot_class)<!-- -->&lt;AppModelType, DbModelType&gt;
151176

152177
an instance of [QuerySnapshot](./firestore_.querysnapshot.md#querysnapshot_class) if the JSON object could be parsed. Throws a [FirestoreError](./firestore_.firestoreerror.md#firestoreerror_class) if an error occurs.

packages/firestore/src/api/snapshot.ts

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -862,10 +862,44 @@ export class QuerySnapshot<
862862
* @returns an instance of {@link QuerySnapshot} if the JSON object could be
863863
* parsed. Throws a {@link FirestoreError} if an error occurs.
864864
*/
865-
static fromJSON<
865+
static fromJSON(db: Firestore, json: object): QuerySnapshot {
866+
return QuerySnapshot.fromJSONInternal(db, json, /* converter = */ null);
867+
}
868+
869+
/**
870+
* Builds a `QuerySnapshot` instance from a JSON object created by
871+
* {@link QuerySnapshot.toJSON}.
872+
*
873+
* @param firestore - The {@link Firestore} instance the snapshot should be loaded for.
874+
* @param json - a JSON object represention of a `QuerySnapshot` instance.
875+
* @param converter - Converts objects to and from Firestore.
876+
* @returns an instance of {@link QuerySnapshot} if the JSON object could be
877+
* parsed. Throws a {@link FirestoreError} if an error occurs.
878+
*/
879+
static fromJSONUsingConverter<
866880
AppModelType,
867881
DbModelType extends DocumentData = DocumentData
868-
>(db: Firestore, json: object): QuerySnapshot<AppModelType, DbModelType> {
882+
>(
883+
db: Firestore,
884+
json: object,
885+
converter: FirestoreDataConverter<AppModelType, DbModelType>
886+
): QuerySnapshot<AppModelType, DbModelType> {
887+
return QuerySnapshot.fromJSONInternal(db, json, converter);
888+
}
889+
890+
/**
891+
* Internal implementation for 'fromJSON' and 'fromJSONUsingCoverter'.
892+
* @internal
893+
* @private
894+
*/
895+
private static fromJSONInternal<
896+
AppModelType,
897+
DbModelType extends DocumentData = DocumentData
898+
>(
899+
db: Firestore,
900+
json: object,
901+
converter: FirestoreDataConverter<AppModelType, DbModelType> | null
902+
): QuerySnapshot<AppModelType, DbModelType> {
869903
if (validateJSON(json, QuerySnapshot._jsonSchema)) {
870904
// Parse the bundle data.
871905
const serializer = newSerializer(db._databaseId);
@@ -914,7 +948,7 @@ export class QuerySnapshot<
914948
// Create an external Query object, required to construct the QuerySnapshot.
915949
const externalQuery = new Query<AppModelType, DbModelType>(
916950
db,
917-
null,
951+
converter,
918952
query
919953
);
920954

0 commit comments

Comments
 (0)