Skip to content

Commit 30c25d2

Browse files
Compat class for DocumentSnapshot
1 parent 484e90a commit 30c25d2

File tree

12 files changed

+260
-261
lines changed

12 files changed

+260
-261
lines changed

packages/firestore/exp/src/api/reference.ts

+16-4
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import {
3636
CollectionReference,
3737
doc,
3838
DocumentReference,
39+
newExpUserDataWriter,
3940
newUserDataReader,
4041
Query,
4142
SetOptions,
@@ -140,6 +141,7 @@ export function getDocFromCache<T>(
140141
): Promise<DocumentSnapshot<T>> {
141142
const firestore = cast(reference.firestore, FirebaseFirestore);
142143
const client = ensureFirestoreConfigured(firestore);
144+
const userDataWriter = newExpUserDataWriter(firestore, reference._converter);
143145

144146
const deferred = new Deferred<Document | null>();
145147
firestore._queue.enqueueAndForget(async () => {
@@ -150,6 +152,7 @@ export function getDocFromCache<T>(
150152
doc =>
151153
new DocumentSnapshot(
152154
firestore,
155+
userDataWriter,
153156
reference._key,
154157
doc,
155158
new SnapshotMetadata(
@@ -203,6 +206,7 @@ export function getDocFromServer<T>(
203206
export function getDocs<T>(query: Query<T>): Promise<QuerySnapshot<T>> {
204207
const firestore = cast(query.firestore, FirebaseFirestore);
205208
const client = ensureFirestoreConfigured(firestore);
209+
const userDataWriter = newExpUserDataWriter(firestore, query._converter);
206210

207211
validateHasExplicitOrderByForLimitToLast(query._query);
208212

@@ -218,7 +222,7 @@ export function getDocs<T>(query: Query<T>): Promise<QuerySnapshot<T>> {
218222
);
219223
});
220224
return deferred.promise.then(
221-
snapshot => new QuerySnapshot(firestore, query, snapshot)
225+
snapshot => new QuerySnapshot(firestore, userDataWriter, query, snapshot)
222226
);
223227
}
224228

@@ -233,14 +237,15 @@ export function getDocsFromCache<T>(
233237
): Promise<QuerySnapshot<T>> {
234238
const firestore = cast(query.firestore, FirebaseFirestore);
235239
const client = ensureFirestoreConfigured(firestore);
240+
const userDataWriter = newExpUserDataWriter(firestore, query._converter);
236241

237242
const deferred = new Deferred<ViewSnapshot>();
238243
firestore._queue.enqueueAndForget(async () => {
239244
const localStore = await getLocalStore(client);
240245
await executeQueryFromCache(localStore, query._query, deferred);
241246
});
242247
return deferred.promise.then(
243-
snapshot => new QuerySnapshot(firestore, query, snapshot)
248+
snapshot => new QuerySnapshot(firestore, userDataWriter, query, snapshot)
244249
);
245250
}
246251

@@ -255,6 +260,7 @@ export function getDocsFromServer<T>(
255260
): Promise<QuerySnapshot<T>> {
256261
const firestore = cast(query.firestore, FirebaseFirestore);
257262
const client = ensureFirestoreConfigured(firestore);
263+
const userDataWriter = newExpUserDataWriter(firestore, query._converter);
258264

259265
const deferred = new Deferred<ViewSnapshot>();
260266
firestore._queue.enqueueAndForget(async () => {
@@ -268,7 +274,7 @@ export function getDocsFromServer<T>(
268274
);
269275
});
270276
return deferred.promise.then(
271-
snapshot => new QuerySnapshot(firestore, query, snapshot)
277+
snapshot => new QuerySnapshot(firestore, userDataWriter, query, snapshot)
272278
);
273279
}
274280

@@ -701,12 +707,16 @@ export function onSnapshot<T>(
701707
} else {
702708
firestore = cast(reference.firestore, FirebaseFirestore);
703709
internalQuery = reference._query;
710+
const userDataWriter = newExpUserDataWriter(
711+
firestore,
712+
reference._converter
713+
);
704714

705715
observer = {
706716
next: snapshot => {
707717
if (args[currArg]) {
708718
(args[currArg] as NextFn<QuerySnapshot<T>>)(
709-
new QuerySnapshot(firestore, reference, snapshot)
719+
new QuerySnapshot(firestore, userDataWriter, reference, snapshot)
710720
);
711721
}
712722
},
@@ -836,8 +846,10 @@ function convertToDocSnapshot<T>(
836846
);
837847
const doc = snapshot.docs.get(ref._key);
838848

849+
const userDataWriter = newExpUserDataWriter(firestore, ref._converter);
839850
return new DocumentSnapshot(
840851
firestore,
852+
userDataWriter,
841853
ref._key,
842854
doc,
843855
new SnapshotMetadata(snapshot.hasPendingWrites, snapshot.fromCache),

packages/firestore/exp/src/api/snapshot.ts

+16-25
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,14 @@
1717

1818
import { DocumentKey } from '../../../src/model/document_key';
1919
import { Document } from '../../../src/model/document';
20-
import {
21-
ServerTimestampBehavior,
22-
UserDataWriter
23-
} from '../../../src/api/user_data_writer';
20+
import { UserDataWriter } from '../../../src/api/user_data_writer';
2421
import {
2522
DocumentSnapshot as LiteDocumentSnapshot,
2623
fieldPathFromArgument
2724
} from '../../../lite/src/api/snapshot';
2825
import { FirebaseFirestore } from './database';
2926
import {
3027
DocumentData,
31-
DocumentReference,
3228
Query,
3329
queryEqual,
3430
SetOptions
@@ -41,9 +37,7 @@ import { Code, FirestoreError } from '../../../src/util/error';
4137
import { ViewSnapshot } from '../../../src/core/view_snapshot';
4238
import { FieldPath } from '../../../lite/src/api/field_path';
4339
import { SnapshotListenOptions } from './reference';
44-
import { Bytes } from '../../../lite/src/api/bytes';
45-
46-
const DEFAULT_SERVER_TIMESTAMP_BEHAVIOR: ServerTimestampBehavior = 'none';
40+
import { UntypedFirestoreDataConverter } from '../../../src/api/user_data_reader';
4741

4842
/**
4943
* Converter used by `withConverter()` to transform user objects of type `T`
@@ -187,12 +181,13 @@ export class DocumentSnapshot<T = DocumentData> extends LiteDocumentSnapshot<
187181

188182
constructor(
189183
readonly _firestore: FirebaseFirestore,
184+
userDataWriter: UserDataWriter,
190185
key: DocumentKey,
191186
document: Document | null,
192187
metadata: SnapshotMetadata,
193-
converter: FirestoreDataConverter<T> | null
188+
converter: UntypedFirestoreDataConverter<T> | null
194189
) {
195-
super(_firestore, key, document, converter);
190+
super(_firestore, userDataWriter, key, document, converter);
196191
this._firestoreImpl = _firestore;
197192
this.metadata = metadata;
198193
}
@@ -219,29 +214,26 @@ export class DocumentSnapshot<T = DocumentData> extends LiteDocumentSnapshot<
219214
* @return An `Object` containing all fields in the document or `undefined` if
220215
* the document doesn't exist.
221216
*/
222-
data(options?: SnapshotOptions): T | undefined {
217+
data(options: SnapshotOptions = {}): T | undefined {
223218
if (!this._document) {
224219
return undefined;
225220
} else if (this._converter) {
226221
// We only want to use the converter and create a new DocumentSnapshot
227222
// if a converter has been provided.
228223
const snapshot = new QueryDocumentSnapshot(
229224
this._firestore,
225+
this._userDataWriter,
230226
this._key,
231227
this._document,
232228
this.metadata,
233229
/* converter= */ null
234230
);
235231
return this._converter.fromFirestore(snapshot, options);
236232
} else {
237-
const userDataWriter = new UserDataWriter(
238-
this._firestoreImpl._databaseId,
239-
options?.serverTimestamps || DEFAULT_SERVER_TIMESTAMP_BEHAVIOR,
240-
key =>
241-
new DocumentReference(this._firestore, /* converter= */ null, key),
242-
bytes => new Bytes(bytes)
243-
);
244-
return userDataWriter.convertValue(this._document.toProto()) as T;
233+
return this._userDataWriter.convertValue(
234+
this._document.toProto(),
235+
options.serverTimestamps
236+
) as T;
245237
}
246238
}
247239

@@ -269,13 +261,10 @@ export class DocumentSnapshot<T = DocumentData> extends LiteDocumentSnapshot<
269261
.data()
270262
.field(fieldPathFromArgument('DocumentSnapshot.get', fieldPath));
271263
if (value !== null) {
272-
const userDataWriter = new UserDataWriter(
273-
this._firestoreImpl._databaseId,
274-
options.serverTimestamps || DEFAULT_SERVER_TIMESTAMP_BEHAVIOR,
275-
key => new DocumentReference(this._firestore, this._converter, key),
276-
bytes => new Bytes(bytes)
264+
return this._userDataWriter.convertValue(
265+
value,
266+
options.serverTimestamps
277267
);
278-
return userDataWriter.convertValue(value);
279268
}
280269
}
281270
return undefined;
@@ -339,6 +328,7 @@ export class QuerySnapshot<T = DocumentData> {
339328

340329
constructor(
341330
readonly _firestore: FirebaseFirestore,
331+
readonly _userDataWriter: UserDataWriter,
342332
query: Query<T>,
343333
readonly _snapshot: ViewSnapshot
344334
) {
@@ -431,6 +421,7 @@ export class QuerySnapshot<T = DocumentData> {
431421
): QueryDocumentSnapshot<T> {
432422
return new QueryDocumentSnapshot<T>(
433423
this._firestore,
424+
this._userDataWriter,
434425
doc.key,
435426
doc,
436427
new SnapshotMetadata(hasPendingWrites, fromCache),

packages/firestore/exp/src/api/transaction.ts

+8
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ import { Transaction as InternalTransaction } from '../../../src/core/transactio
2929
import { validateReference } from '../../../lite/src/api/write_batch';
3030
import { getDatastore } from '../../../lite/src/api/components';
3131
import { DocumentReference } from '../../../lite/src/api/reference';
32+
import { UserDataWriter } from '../../../src/api/user_data_writer';
33+
import { Bytes } from '../../../lite/src/api/bytes';
3234

3335
/**
3436
* A reference to a transaction.
@@ -56,12 +58,18 @@ export class Transaction extends LiteTransaction {
5658
*/
5759
get<T>(documentRef: DocumentReference<T>): Promise<DocumentSnapshot<T>> {
5860
const ref = validateReference<T>(documentRef, this._firestore);
61+
const userDataWriter = new UserDataWriter(
62+
this._firestore._databaseId,
63+
key => new DocumentReference(this._firestore, ref._converter, key),
64+
bytes => new Bytes(bytes)
65+
);
5966
return super
6067
.get(documentRef)
6168
.then(
6269
liteDocumentSnapshot =>
6370
new DocumentSnapshot(
6471
this._firestore,
72+
userDataWriter,
6573
ref._key,
6674
liteDocumentSnapshot._document,
6775
new SnapshotMetadata(

packages/firestore/exp/test/shim.ts

+2-66
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ import { Compat } from '../../src/compat/compat';
4949
import {
5050
Firestore,
5151
DocumentReference,
52+
DocumentSnapshot,
53+
QueryDocumentSnapshot,
5254
wrapObserver,
5355
extractSnapshotOptions
5456
} from '../../src/api/database';
@@ -184,48 +186,6 @@ export class WriteBatch
184186
}
185187
}
186188

187-
export class DocumentSnapshot<T = legacy.DocumentData>
188-
extends Compat<exp.DocumentSnapshot<T>>
189-
implements legacy.DocumentSnapshot<T> {
190-
constructor(
191-
private readonly _firestore: Firestore,
192-
delegate: exp.DocumentSnapshot<T>
193-
) {
194-
super(delegate);
195-
}
196-
197-
readonly ref = new DocumentReference<T>(this._firestore, this._delegate.ref);
198-
readonly id = this._delegate.id;
199-
readonly metadata = this._delegate.metadata;
200-
201-
get exists(): boolean {
202-
return this._delegate.exists();
203-
}
204-
205-
data(options?: legacy.SnapshotOptions): T | undefined {
206-
return wrap(this._firestore, this._delegate.data(options));
207-
}
208-
209-
get(fieldPath: string | FieldPath, options?: legacy.SnapshotOptions): any {
210-
return wrap(
211-
this._firestore,
212-
this._delegate.get(unwrap(fieldPath), options)
213-
);
214-
}
215-
216-
isEqual(other: DocumentSnapshot<T>): boolean {
217-
return snapshotEqual(this._delegate, other._delegate);
218-
}
219-
}
220-
221-
export class QueryDocumentSnapshot<T = legacy.DocumentData>
222-
extends DocumentSnapshot<T>
223-
implements legacy.QueryDocumentSnapshot<T> {
224-
data(options?: legacy.SnapshotOptions): T {
225-
return this._delegate.data(options)!;
226-
}
227-
}
228-
229189
export class Query<T = legacy.DocumentData>
230190
extends Compat<exp.Query<T>>
231191
implements legacy.Query<T> {
@@ -496,30 +456,6 @@ export class Blob extends Compat<BytesExp> implements legacy.Blob {
496456
}
497457
}
498458

499-
/**
500-
* Takes document data that uses the firestore-exp API types and replaces them
501-
* with the API types defined in this shim.
502-
*/
503-
function wrap(firestore: Firestore, value: any): any {
504-
if (Array.isArray(value)) {
505-
return value.map(v => wrap(firestore, v));
506-
} else if (value instanceof FieldPathExp) {
507-
return new FieldPath(...value._internalPath.toArray());
508-
} else if (value instanceof BytesExp) {
509-
return new Blob(value);
510-
} else if (isPlainObject(value)) {
511-
const obj: any = {};
512-
for (const key in value) {
513-
if (value.hasOwnProperty(key)) {
514-
obj[key] = wrap(firestore, value[key]);
515-
}
516-
}
517-
return obj;
518-
} else {
519-
return value;
520-
}
521-
}
522-
523459
/**
524460
* Takes user data that uses API types from this shim and replaces them
525461
* with the the firestore-exp API types.

0 commit comments

Comments
 (0)