Skip to content

Commit

Permalink
Add getDocFromCache() & getDocFromServer()
Browse files Browse the repository at this point in the history
  • Loading branch information
schmidt-sebastian committed Jun 25, 2020
1 parent 64162a2 commit 6289e19
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 12 deletions.
2 changes: 1 addition & 1 deletion packages/firestore/exp/index.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export {

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

export { getDoc } from './src/api/reference';
export { getDoc, getDocFromCache, getDocFromServer } from './src/api/reference';

export {
FieldValue,
Expand Down
43 changes: 42 additions & 1 deletion packages/firestore/exp/src/api/reference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,55 @@ import {
} from '../../../src/api/database';
import { ViewSnapshot } from '../../../src/core/view_snapshot';
import { DocumentReference } from '../../../lite/src/api/reference';
import { Document } from '../../../src/model/document';

export function getDoc<T>(
reference: firestore.DocumentReference<T>
): Promise<firestore.DocumentSnapshot<T>> {
const ref = cast<DocumentReference<T>>(reference, DocumentReference);
const firestore = cast<Firestore>(ref.firestore, Firestore);
return firestore._getFirestoreClient().then(async firestoreClient => {
const viewSnapshot = await getDocViaSnapshotListener(firestoreClient, ref);
const viewSnapshot = await getDocViaSnapshotListener(
firestoreClient,
ref._key
);
return convertToDocSnapshot(firestore, ref, viewSnapshot);
});
}

// TODO(firestorexp): Make sure we don't include Datastore/RemoteStore in builds
// that only include `getDocFromCache`.
export function getDocFromCache<T>(
reference: firestore.DocumentReference<T>
): Promise<firestore.DocumentSnapshot<T>> {
const ref = cast<DocumentReference<T>>(reference, DocumentReference);
const firestore = cast<Firestore>(ref.firestore, Firestore);
return firestore._getFirestoreClient().then(async firestoreClient => {
const doc = await firestoreClient.getDocumentFromLocalCache(ref._key);
return new DocumentSnapshot(
firestore,
ref._key,
doc,
ref._converter,
new SnapshotMetadata(
doc instanceof Document ? doc.hasLocalMutations : false,
/* fromCache= */ true
)
);
});
}

export function getDocFromServer<T>(
reference: firestore.DocumentReference<T>
): Promise<firestore.DocumentSnapshot<T>> {
const ref = cast<DocumentReference<T>>(reference, DocumentReference);
const firestore = cast<Firestore>(ref.firestore, Firestore);
return firestore._getFirestoreClient().then(async firestoreClient => {
const viewSnapshot = await getDocViaSnapshotListener(
firestoreClient,
ref._key,
{ source: 'server' }
);
return convertToDocSnapshot(firestore, ref, viewSnapshot);
});
}
Expand Down
28 changes: 27 additions & 1 deletion packages/firestore/exp/test/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ import {
initializeFirestore
} from '../src/api/database';
import { withTestDoc } from './helpers';
import { getDoc } from '../src/api/reference';
import {
getDoc,
getDocFromCache,
getDocFromServer
} from '../src/api/reference';

use(chaiAsPromised);

Expand Down Expand Up @@ -61,3 +65,25 @@ describe('getDoc()', () => {
});
});
});

describe('getDocFromCache()', () => {
it('can get a non-existing document', () => {
return withTestDoc(async docRef => {
await expect(getDocFromCache(docRef)).to.eventually.be.rejectedWith(
/Failed to get document from cache./
);
});
});
});

describe('getDocFromServer()', () => {
it('can get a non-existing document', () => {
return withTestDoc(async docRef => {
const docSnap = await getDocFromServer(docRef);
expect(docSnap.metadata.fromCache).to.be.false;
expect(docSnap.metadata.hasPendingWrites).to.be.false;
expect(docSnap.data()).to.be.undefined;
expect(docSnap.exists()).to.be.false;
});
});
});
18 changes: 9 additions & 9 deletions packages/firestore/src/api/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1199,7 +1199,7 @@ export class DocumentReference<T = firestore.DocumentData>
}
return addDocSnapshotListener(
this._firestoreClient,
this,
this._key,
internalOptions,
observer
);
Expand Down Expand Up @@ -1227,7 +1227,7 @@ export class DocumentReference<T = firestore.DocumentData>
} else {
return getDocViaSnapshotListener(
this._firestoreClient,
this,
this._key,
options
).then(snapshot => this._convertToDocSnapshot(snapshot));
}
Expand Down Expand Up @@ -1262,9 +1262,9 @@ export class DocumentReference<T = firestore.DocumentData>
}

/** Registers an internal snapshot listener for `ref`. */
function addDocSnapshotListener<T>(
function addDocSnapshotListener(
firestoreClient: FirestoreClient,
ref: DocumentKeyReference<T>,
key: DocumentKey,
options: ListenOptions,
observer: PartialObserver<ViewSnapshot>
): Unsubscribe {
Expand All @@ -1284,7 +1284,7 @@ function addDocSnapshotListener<T>(
error: errHandler
});
const internalListener = firestoreClient.listen(
InternalQuery.atPath(ref._key.path),
InternalQuery.atPath(key.path),
asyncObserver,
options
);
Expand All @@ -1299,15 +1299,15 @@ function addDocSnapshotListener<T>(
* Retrieves a latency-compensated document from the backend via a
* SnapshotListener.
*/
export function getDocViaSnapshotListener<T>(
export function getDocViaSnapshotListener(
firestoreClient: FirestoreClient,
ref: DocumentKeyReference<T>,
key: DocumentKey,
options?: firestore.GetOptions
): Promise<ViewSnapshot> {
const result = new Deferred<ViewSnapshot>();
const unlisten = addDocSnapshotListener(
firestoreClient,
ref,
key,
{
includeMetadataChanges: true,
waitForSyncWhenOnline: true
Expand All @@ -1318,7 +1318,7 @@ export function getDocViaSnapshotListener<T>(
// user actions affecting the now stale query.
unlisten();

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

0 comments on commit 6289e19

Please sign in to comment.