Skip to content

Commit c38fc71

Browse files
Add getDocFromCache() & getDocFromServer() (#3285)
1 parent a4ff036 commit c38fc71

File tree

6 files changed

+244
-61
lines changed

6 files changed

+244
-61
lines changed

.changeset/tall-pots-brush.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
---
2+
---

packages/firestore/exp/index.node.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export {
4444

4545
export { runTransaction, Transaction } from '../lite/src/api/transaction';
4646

47-
export { getDoc } from './src/api/reference';
47+
export { getDoc, getDocFromCache, getDocFromServer } from './src/api/reference';
4848

4949
export {
5050
FieldValue,

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

+42-1
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,55 @@ import {
3030
} from '../../../src/api/database';
3131
import { ViewSnapshot } from '../../../src/core/view_snapshot';
3232
import { DocumentReference } from '../../../lite/src/api/reference';
33+
import { Document } from '../../../src/model/document';
3334

3435
export function getDoc<T>(
3536
reference: firestore.DocumentReference<T>
3637
): Promise<firestore.DocumentSnapshot<T>> {
3738
const ref = cast<DocumentReference<T>>(reference, DocumentReference);
3839
const firestore = cast<Firestore>(ref.firestore, Firestore);
3940
return firestore._getFirestoreClient().then(async firestoreClient => {
40-
const viewSnapshot = await getDocViaSnapshotListener(firestoreClient, ref);
41+
const viewSnapshot = await getDocViaSnapshotListener(
42+
firestoreClient,
43+
ref._key
44+
);
45+
return convertToDocSnapshot(firestore, ref, viewSnapshot);
46+
});
47+
}
48+
49+
// TODO(firestorexp): Make sure we don't include Datastore/RemoteStore in builds
50+
// that only include `getDocFromCache`.
51+
export function getDocFromCache<T>(
52+
reference: firestore.DocumentReference<T>
53+
): Promise<firestore.DocumentSnapshot<T>> {
54+
const ref = cast<DocumentReference<T>>(reference, DocumentReference);
55+
const firestore = cast<Firestore>(ref.firestore, Firestore);
56+
return firestore._getFirestoreClient().then(async firestoreClient => {
57+
const doc = await firestoreClient.getDocumentFromLocalCache(ref._key);
58+
return new DocumentSnapshot(
59+
firestore,
60+
ref._key,
61+
doc,
62+
ref._converter,
63+
new SnapshotMetadata(
64+
doc instanceof Document ? doc.hasLocalMutations : false,
65+
/* fromCache= */ true
66+
)
67+
);
68+
});
69+
}
70+
71+
export function getDocFromServer<T>(
72+
reference: firestore.DocumentReference<T>
73+
): Promise<firestore.DocumentSnapshot<T>> {
74+
const ref = cast<DocumentReference<T>>(reference, DocumentReference);
75+
const firestore = cast<Firestore>(ref.firestore, Firestore);
76+
return firestore._getFirestoreClient().then(async firestoreClient => {
77+
const viewSnapshot = await getDocViaSnapshotListener(
78+
firestoreClient,
79+
ref._key,
80+
{ source: 'server' }
81+
);
4182
return convertToDocSnapshot(firestore, ref, viewSnapshot);
4283
});
4384
}

packages/firestore/exp/test/integration.test.ts

+27-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@ import {
2525
initializeFirestore
2626
} from '../src/api/database';
2727
import { withTestDoc } from './helpers';
28-
import { getDoc } from '../src/api/reference';
28+
import {
29+
getDoc,
30+
getDocFromCache,
31+
getDocFromServer
32+
} from '../src/api/reference';
2933

3034
use(chaiAsPromised);
3135

@@ -61,3 +65,25 @@ describe('getDoc()', () => {
6165
});
6266
});
6367
});
68+
69+
describe('getDocFromCache()', () => {
70+
it('can get a non-existing document', () => {
71+
return withTestDoc(async docRef => {
72+
await expect(getDocFromCache(docRef)).to.eventually.be.rejectedWith(
73+
/Failed to get document from cache./
74+
);
75+
});
76+
});
77+
});
78+
79+
describe('getDocFromServer()', () => {
80+
it('can get a non-existing document', () => {
81+
return withTestDoc(async docRef => {
82+
const docSnap = await getDocFromServer(docRef);
83+
expect(docSnap.metadata.fromCache).to.be.false;
84+
expect(docSnap.metadata.hasPendingWrites).to.be.false;
85+
expect(docSnap.data()).to.be.undefined;
86+
expect(docSnap.exists()).to.be.false;
87+
});
88+
});
89+
});

0 commit comments

Comments
 (0)