Skip to content

Commit f0d3304

Browse files
committed
Templated converter DocSnapshot.fromJSON
1 parent 7c8ef87 commit f0d3304

File tree

4 files changed

+38
-25
lines changed

4 files changed

+38
-25
lines changed

common/api-review/firestore.api.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ export class DocumentSnapshot<AppModelType = DocumentData, DbModelType extends D
175175
data(options?: SnapshotOptions): AppModelType | undefined;
176176
exists(): this is QueryDocumentSnapshot<AppModelType, DbModelType>;
177177
// (undocumented)
178-
static fromJSON(db: Firestore, json: object): object;
178+
static fromJSON<AppModelType, DbModelType extends DocumentData = DocumentData>(db: Firestore, json: object, converter: FirestoreDataConverter<AppModelType, DbModelType>): DocumentSnapshot<AppModelType, DbModelType>;
179179
get(fieldPath: string | FieldPath, options?: SnapshotOptions): any;
180180
get id(): string;
181181
readonly metadata: SnapshotMetadata;

packages/firestore/src/api/snapshot.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,14 @@ export class DocumentSnapshot<
548548
return result;
549549
}
550550

551-
static fromJSON(db: Firestore, json: object): object {
551+
static fromJSON<
552+
AppModelType,
553+
DbModelType extends DocumentData = DocumentData
554+
>(
555+
db: Firestore,
556+
json: object,
557+
converter: FirestoreDataConverter<AppModelType, DbModelType>
558+
): DocumentSnapshot<AppModelType, DbModelType> {
552559
const requiredFields = ['bundle', 'bundleName', 'bundleSource'];
553560
let error: string | undefined = undefined;
554561
let bundleString: string = '';
@@ -579,7 +586,10 @@ export class DocumentSnapshot<
579586
}
580587
// Parse the bundle data.
581588
const serializer = newSerializer(db._databaseId);
582-
const elements = createBundleReaderSync(bundleString, serializer).getElements();
589+
const elements = createBundleReaderSync(
590+
bundleString,
591+
serializer
592+
).getElements();
583593
if (elements.length === 0) {
584594
error = 'No snapshat data was found in the bundle.';
585595
}
@@ -588,14 +598,16 @@ export class DocumentSnapshot<
588598
!elements[0].payload.documentMetadata ||
589599
!elements[1].payload.document
590600
) {
591-
error = 'DocumentSnapshot bundle data must contain one document metadata and then one document.';
601+
error =
602+
'DocumentSnapshot bundle data must contain one metadata and then one document.';
592603
}
593604
if (error) {
594605
throw new FirestoreError(Code.INVALID_ARGUMENT, error);
595606
}
596607
// convert bundle data into the types that the DocumentSnapshot constructore requires.
597608
const bundleConverter = new BundleConverterImpl(serializer);
598-
const documentSnapshotData = bundleConverter.toDocumentSnapshotData(elements);
609+
const documentSnapshotData =
610+
bundleConverter.toDocumentSnapshotData(elements);
599611
const liteUserDataWriter = new LiteUserDataWriter(db);
600612
return new DocumentSnapshot(
601613
db,
@@ -606,7 +618,7 @@ export class DocumentSnapshot<
606618
/* hasPendingWrites= */ false,
607619
/* fromCache= */ false
608620
),
609-
/* converter= */ null
621+
converter
610622
);
611623
}
612624
}

packages/firestore/src/core/bundle_impl.ts

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import { MutableDocument } from '../model/document';
2727
import { DocumentKey } from '../model/document_key';
2828
import { ResourcePath } from '../model/path';
2929
import {
30-
BundledDocumentMetadata,
3130
BundleMetadata as ProtoBundleMetadata,
3231
NamedQuery as ProtoNamedQuery
3332
} from '../protos/firestore_bundle_proto';
@@ -54,7 +53,7 @@ import { SnapshotVersion } from './snapshot_version';
5453
* Helper to convert objects from bundles to model objects in the SDK.
5554
*/
5655
export class BundleConverterImpl implements BundleConverter {
57-
constructor(private readonly serializer: JsonProtoSerializer) { }
56+
constructor(private readonly serializer: JsonProtoSerializer) {}
5857

5958
toDocumentKey(name: string): DocumentKey {
6059
return fromName(this.serializer, name);
@@ -78,26 +77,28 @@ export class BundleConverterImpl implements BundleConverter {
7877
}
7978
}
8079

81-
toDocumentSnapshotData(
82-
bundleElements: SizedBundleElement[]): {
83-
documentKey: DocumentKey;
84-
mutableDoc: MutableDocument;
85-
} {
80+
toDocumentSnapshotData(bundleElements: SizedBundleElement[]): {
81+
documentKey: DocumentKey;
82+
mutableDoc: MutableDocument;
83+
} {
8684
const metadata = bundleElements[0]?.payload?.documentMetadata!;
8785
const document = bundleElements[1]?.payload?.document!;
88-
let error : string | undefined = undefined;
89-
if( !metadata || !document) {
90-
error = 'DocumentSnapshot bundle data requires both document metadata and document data';
91-
} else if( metadata.name !== document.name ) {
92-
error = 'DocumentSnapshot metadata is not related to the document in the bundle.';
86+
let error: string | undefined = undefined;
87+
if (!metadata || !document) {
88+
error =
89+
'DocumentSnapshot bundle data requires both document metadata and document data';
90+
} else if (metadata.name !== document.name) {
91+
error =
92+
'DocumentSnapshot metadata is not related to the document in the bundle.';
9393
}
94-
if ( error ) {
94+
if (error) {
9595
throw new FirestoreError(Code.INVALID_ARGUMENT, error);
96-
}
96+
}
9797
const bundleConverter = new BundleConverterImpl(this.serializer);
9898
const documentKey = bundleConverter.toDocumentKey(metadata.name!);
9999
const mutableDoc = bundleConverter.toMutableDocument({
100-
metadata, document
100+
metadata,
101+
document
101102
});
102103
return {
103104
documentKey,
@@ -163,8 +164,8 @@ export class BundleLoader {
163164
} else if (element.payload.document) {
164165
debugAssert(
165166
this.documents.length > 0 &&
166-
this.documents[this.documents.length - 1].metadata.name ===
167-
element.payload.document.name,
167+
this.documents[this.documents.length - 1].metadata.name ===
168+
element.payload.document.name,
168169
'The document being added does not match the stored metadata.'
169170
);
170171
this.documents[this.documents.length - 1].document =
@@ -208,7 +209,7 @@ export class BundleLoader {
208209
async complete(): Promise<BundleLoadResult> {
209210
debugAssert(
210211
this.documents[this.documents.length - 1]?.metadata.exists !== true ||
211-
!!this.documents[this.documents.length - 1].document,
212+
!!this.documents[this.documents.length - 1].document,
212213
'Bundled documents end with a document metadata element instead of a document.'
213214
);
214215
debugAssert(!!this.bundleMetadata.id, 'Bundle ID must be set.');

packages/firestore/src/util/bundle_reader_sync_impl.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export class BundleReaderSyncImpl implements BundleReaderSync {
4646
element = this.nextElement();
4747
if (element !== null) {
4848
this.elements.push(element);
49-
}
49+
}
5050
} while (element !== null);
5151
}
5252

0 commit comments

Comments
 (0)