Skip to content

Commit e90782b

Browse files
dhennekemikehardy
authored andcommitted
feat(firestore): implement the getDoc modular api
1 parent 3446ca3 commit e90782b

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

packages/firestore/lib/modular/query.d.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,42 @@ export function startAfter<AppModelType, DbModelType extends DocumentData>(
239239
*/
240240
export function limit(limit: number): QueryLimitConstraint;
241241

242+
/**
243+
* Reads the document referred to by this `DocumentReference`.
244+
*
245+
* Note: `getDoc()` attempts to provide up-to-date data when possible by waiting
246+
* for data from the server, but it may return cached data or fail if you are
247+
* offline and the server cannot be reached. To specify this behavior, invoke
248+
* {@link getDocFromCache} or {@link getDocFromServer}.
249+
*
250+
* @param reference - The reference of the document to fetch.
251+
* @returns A Promise resolved with a `DocumentSnapshot` containing the
252+
* current document contents.
253+
*/
254+
export declare function getDoc<T>(reference: DocumentReference<T>): Promise<DocumentSnapshot<T>>;
255+
256+
/**
257+
* Reads the document referred to by this `DocumentReference` from cache.
258+
* Returns an error if the document is not currently cached.
259+
*
260+
* @returns A `Promise` resolved with a `DocumentSnapshot` containing the
261+
* current document contents.
262+
*/
263+
export declare function getDocFromCache<T>(
264+
reference: DocumentReference<T>,
265+
): Promise<DocumentSnapshot<T>>;
266+
267+
/**
268+
* Reads the document referred to by this `DocumentReference` from the server.
269+
* Returns an error if the network is not available.
270+
*
271+
* @returns A `Promise` resolved with a `DocumentSnapshot` containing the
272+
* current document contents.
273+
*/
274+
export declare function getDocFromServer<T>(
275+
reference: DocumentReference<T>,
276+
): Promise<DocumentSnapshot<T>>;
277+
242278
/**
243279
* Executes the query and returns the results as a `QuerySnapshot`.
244280
*

packages/firestore/lib/modular/query.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,30 @@ export function limitToLast(limit) {
169169
return new QueryConstraint('limitToLast', limit);
170170
}
171171

172+
/**
173+
* @param {DocumentReference} query
174+
* @returns {Promise<DocumentSnapshot>}
175+
*/
176+
export function getDoc(reference) {
177+
return reference.get({ source: 'default' });
178+
}
179+
180+
/**
181+
* @param {DocumentReference} query
182+
* @returns {Promise<DocumentSnapshot>}
183+
*/
184+
export function getDocFromCache(reference) {
185+
return reference.get({ source: 'cache' });
186+
}
187+
188+
/**
189+
* @param {DocumentReference} query
190+
* @returns {Promise<DocumentSnapshot>}
191+
*/
192+
export function getDocFromServer(reference) {
193+
return reference.get({ source: 'server' });
194+
}
195+
172196
/**
173197
* @param {Query} query
174198
* @returns {Promise<QuerySnapshot>}

0 commit comments

Comments
 (0)