Skip to content

Commit 00b92d8

Browse files
Return Firestore-Compat types from QuerySnapshot.docs
1 parent fe9524c commit 00b92d8

File tree

3 files changed

+34
-4
lines changed

3 files changed

+34
-4
lines changed

packages/firestore/src/api/database.ts

+15-1
Original file line numberDiff line numberDiff line change
@@ -967,8 +967,11 @@ export class QueryDocumentSnapshot<T = PublicDocumentData>
967967
export class Query<T = PublicDocumentData>
968968
extends Compat<ExpQuery<T>>
969969
implements PublicQuery<T> {
970+
private readonly _userDataWriter: UserDataWriter;
971+
970972
constructor(readonly firestore: Firestore, delegate: ExpQuery<T>) {
971973
super(delegate);
974+
this._userDataWriter = new UserDataWriter(firestore);
972975
}
973976

974977
where(
@@ -1076,7 +1079,18 @@ export class Query<T = PublicDocumentData>
10761079
} else {
10771080
query = getDocs(this._delegate);
10781081
}
1079-
return query.then(result => new QuerySnapshot(this.firestore, result));
1082+
return query.then(
1083+
result =>
1084+
new QuerySnapshot(
1085+
this.firestore,
1086+
new ExpQuerySnapshot<T>(
1087+
this.firestore._delegate,
1088+
this._userDataWriter,
1089+
this._delegate,
1090+
result._snapshot
1091+
)
1092+
)
1093+
);
10801094
}
10811095

10821096
onSnapshot(observer: PartialObserver<PublicQuerySnapshot<T>>): Unsubscribe;

packages/firestore/test/integration/api/type.test.ts

+9-3
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,18 @@ apiDescribe('Firestore', (persistence: boolean) => {
3232
db: firestore.FirebaseFirestore,
3333
data: {}
3434
): Promise<void> {
35-
const doc = db.collection('rooms').doc();
35+
const collection = db.collection(db.collection('a').doc().id);
36+
const doc = collection.doc();
3637
return doc
3738
.set(data)
3839
.then(() => doc.get())
39-
.then(snapshot => {
40-
expect(snapshot.data()).to.deep.equal(data);
40+
.then(docSnapshot => {
41+
expect(docSnapshot.data()).to.deep.equal(data);
42+
return collection.get();
43+
})
44+
.then(querySnapshot => {
45+
const [docSnapshot] = querySnapshot.docs;
46+
expect(docSnapshot.data()).to.deep.equal(data);
4147
});
4248
}
4349

packages/firestore/test/util/equality_matcher.ts

+10
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717

1818
import { use } from 'chai';
19+
import { Indexable } from '../../src/util/misc';
1920

2021
/**
2122
* Duck-typed interface for objects that have an isEqual() method.
@@ -57,6 +58,15 @@ function customDeepEqual(
5758
return customMatcher.equalsFn(left, right);
5859
}
5960
}
61+
if (left && typeof left === 'object' && right && typeof right === 'object') {
62+
// The `isEqual` check below returns true if firestore-exp types are
63+
// compared with API types from Firestore classic. We do want to
64+
// differentiate between these types in our tests to ensure that the we do
65+
// not return firestore-exp types in the classic SDK.
66+
if ((left as Indexable).constructor !== (right as Indexable).constructor) {
67+
return false;
68+
}
69+
}
6070
if (typeof left === 'object' && left && 'isEqual' in left) {
6171
return (left as Equatable<unknown>).isEqual(right);
6272
}

0 commit comments

Comments
 (0)