Skip to content

Commit 6289e19

Browse files
Add getDocFromCache() & getDocFromServer()
1 parent 64162a2 commit 6289e19

File tree

4 files changed

+79
-12
lines changed

4 files changed

+79
-12
lines changed

packages/firestore/exp/index.node.ts

Lines changed: 1 addition & 1 deletion
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

Lines changed: 42 additions & 1 deletion
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

Lines changed: 27 additions & 1 deletion
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+
});

packages/firestore/src/api/database.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1199,7 +1199,7 @@ export class DocumentReference<T = firestore.DocumentData>
11991199
}
12001200
return addDocSnapshotListener(
12011201
this._firestoreClient,
1202-
this,
1202+
this._key,
12031203
internalOptions,
12041204
observer
12051205
);
@@ -1227,7 +1227,7 @@ export class DocumentReference<T = firestore.DocumentData>
12271227
} else {
12281228
return getDocViaSnapshotListener(
12291229
this._firestoreClient,
1230-
this,
1230+
this._key,
12311231
options
12321232
).then(snapshot => this._convertToDocSnapshot(snapshot));
12331233
}
@@ -1262,9 +1262,9 @@ export class DocumentReference<T = firestore.DocumentData>
12621262
}
12631263

12641264
/** Registers an internal snapshot listener for `ref`. */
1265-
function addDocSnapshotListener<T>(
1265+
function addDocSnapshotListener(
12661266
firestoreClient: FirestoreClient,
1267-
ref: DocumentKeyReference<T>,
1267+
key: DocumentKey,
12681268
options: ListenOptions,
12691269
observer: PartialObserver<ViewSnapshot>
12701270
): Unsubscribe {
@@ -1284,7 +1284,7 @@ function addDocSnapshotListener<T>(
12841284
error: errHandler
12851285
});
12861286
const internalListener = firestoreClient.listen(
1287-
InternalQuery.atPath(ref._key.path),
1287+
InternalQuery.atPath(key.path),
12881288
asyncObserver,
12891289
options
12901290
);
@@ -1299,15 +1299,15 @@ function addDocSnapshotListener<T>(
12991299
* Retrieves a latency-compensated document from the backend via a
13001300
* SnapshotListener.
13011301
*/
1302-
export function getDocViaSnapshotListener<T>(
1302+
export function getDocViaSnapshotListener(
13031303
firestoreClient: FirestoreClient,
1304-
ref: DocumentKeyReference<T>,
1304+
key: DocumentKey,
13051305
options?: firestore.GetOptions
13061306
): Promise<ViewSnapshot> {
13071307
const result = new Deferred<ViewSnapshot>();
13081308
const unlisten = addDocSnapshotListener(
13091309
firestoreClient,
1310-
ref,
1310+
key,
13111311
{
13121312
includeMetadataChanges: true,
13131313
waitForSyncWhenOnline: true
@@ -1318,7 +1318,7 @@ export function getDocViaSnapshotListener<T>(
13181318
// user actions affecting the now stale query.
13191319
unlisten();
13201320

1321-
const exists = snap.docs.has(ref._key);
1321+
const exists = snap.docs.has(key);
13221322
if (!exists && snap.fromCache) {
13231323
// TODO(dimond): If we're online and the document doesn't
13241324
// exist then we resolve with a doc.exists set to false. If

0 commit comments

Comments
 (0)