Skip to content

Commit 7c1c7f1

Browse files
Add firestore-exp/firestore-lite docs (#3843)
1 parent 01bb65b commit 7c1c7f1

20 files changed

+1823
-75
lines changed

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

Lines changed: 141 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,9 @@ export interface Settings extends LiteSettings {
7171
}
7272

7373
/**
74-
* The root reference to the Firestore database and the entry point for the
75-
* tree-shakeable SDK.
74+
* The Cloud Firestore service interface.
75+
*
76+
* Do not call this constructor directly. Instead, use {@link getFirestore()}.
7677
*/
7778
export class FirebaseFirestore
7879
extends LiteFirestore
@@ -173,6 +174,17 @@ export class FirebaseFirestore
173174
}
174175
}
175176

177+
/**
178+
* Initializes a new instance of Cloud Firestore with the provided settings.
179+
* Can only be called before any other function, including
180+
* {@link getFirestore()}. If the custom settings are empty, this function is
181+
* equivalent to calling {@link getFirestore()}.
182+
*
183+
* @param app The {@link FirebaseApp} with which the `Firestore` instance will
184+
* be associated.
185+
* @param settings A settings object to configure the `Firestore` instance.
186+
* @return A newly initialized `Firestore` instance.
187+
*/
176188
export function initializeFirestore(
177189
app: FirebaseApp,
178190
settings: Settings
@@ -197,10 +209,41 @@ export function initializeFirestore(
197209
return firestore;
198210
}
199211

212+
/**
213+
* Returns the existing instance of Firestore that is associated with the
214+
* provided {@link FirebaseApp}. If no instance exists, initializes a new
215+
* instance with default settings.
216+
*
217+
* @param app The {@link FirebaseApp} instance that the returned Firestore
218+
* instance is associated with.
219+
* @return The `Firestore` instance of the provided app.
220+
*/
200221
export function getFirestore(app: FirebaseApp): FirebaseFirestore {
201222
return _getProvider(app, 'firestore-exp').getImmediate() as FirebaseFirestore;
202223
}
203224

225+
/**
226+
* Attempts to enable persistent storage, if possible.
227+
*
228+
* Must be called before any other functions (other than
229+
* {@link initializeFirestore()}, {@link getFirestore()} or
230+
* {@link clearIndexedDbPersistence()}.
231+
*
232+
* If this fails, `enableIndexedDbPersistence()` will reject the promise it
233+
* returns. Note that even after this failure, the `Firestore` instance will
234+
* remain usable, however offline persistence will be disabled.
235+
*
236+
* There are several reasons why this can fail, which can be identified by
237+
* the `code` on the error.
238+
*
239+
* * failed-precondition: The app is already open in another browser tab.
240+
* * unimplemented: The browser is incompatible with the offline
241+
* persistence implementation.
242+
*
243+
* @param firestore The `Firestore` instance to enable persistence for.
244+
* @param persistenceSettings Optional settings object to configure persistence.
245+
* @return A promise that represents successfully enabling persistent storage.
246+
*/
204247
export function enableIndexedDbPersistence(
205248
firestore: FirebaseFirestore,
206249
persistenceSettings?: PersistenceSettings
@@ -234,6 +277,28 @@ export function enableIndexedDbPersistence(
234277
});
235278
}
236279

280+
/**
281+
* Attempts to enable multi-tab persistent storage, if possible. If enabled
282+
* across all tabs, all operations share access to local persistence, including
283+
* shared execution of queries and latency-compensated local document updates
284+
* across all connected instances.
285+
*
286+
* If this fails, `enableMultiTabIndexedDbPersistence()` will reject the promise
287+
* it returns. Note that even after this failure, the `Firestore` instance will
288+
* remain usable, however offline persistence will be disabled.
289+
*
290+
* There are several reasons why this can fail, which can be identified by
291+
* the `code` on the error.
292+
*
293+
* * failed-precondition: The app is already open in another browser tab and
294+
* multi-tab is not enabled.
295+
* * unimplemented: The browser is incompatible with the offline
296+
* persistence implementation.
297+
*
298+
* @param firestore The `Firestore` instance to enable persistence for.
299+
* @return A promise that represents successfully enabling persistent
300+
* storage.
301+
*/
237302
export function enableMultiTabIndexedDbPersistence(
238303
firestore: FirebaseFirestore
239304
): Promise<void> {
@@ -265,6 +330,28 @@ export function enableMultiTabIndexedDbPersistence(
265330
});
266331
}
267332

333+
/**
334+
* Clears the persistent storage. This includes pending writes and cached
335+
* documents.
336+
*
337+
* Must be called while the `Firestore` instance is not started (after the app is
338+
* terminated or when the app is first initialized). On startup, this function
339+
* must be called before other functions (other than {@link
340+
* initializeFirestore()} or {@link getFirestore()})). If the `Firestore`
341+
* instance is still running, the promise will be rejected with the error code
342+
* of `failed-precondition`.
343+
*
344+
* Note: `clearIndexedDbPersistence()` is primarily intended to help write
345+
* reliable tests that use Cloud Firestore. It uses an efficient mechanism for
346+
* dropping existing data but does not attempt to securely overwrite or
347+
* otherwise make cached data unrecoverable. For applications that are sensitive
348+
* to the disclosure of cached data in between user sessions, we strongly
349+
* recommend not enabling persistence at all.
350+
*
351+
* @param firestore The `Firestore` instance to clear persistence for.
352+
* @return A promise that is resolved when the persistent storage is
353+
* cleared. Otherwise, the promise is rejected with an error.
354+
*/
268355
export function clearIndexedDbPersistence(
269356
firestore: FirebaseFirestore
270357
): Promise<void> {
@@ -290,6 +377,22 @@ export function clearIndexedDbPersistence(
290377
return deferred.promise;
291378
}
292379

380+
/**
381+
* Waits until all currently pending writes for the active user have been
382+
* acknowledged by the backend.
383+
*
384+
* The returned Promise resolves immediately if there are no outstanding writes.
385+
* Otherwise, the Promise waits for all previously issued writes (including
386+
* those written in a previous app session), but it does not wait for writes
387+
* that were added after the function is called. If you want to wait for
388+
* additional writes, call `waitForPendingWrites()` again.
389+
*
390+
* Any outstanding `waitForPendingWrites()` Promises are rejected during user
391+
* changes.
392+
*
393+
* @return A Promise which resolves when all currently pending writes have been
394+
* acknowledged by the backend.
395+
*/
293396
export function waitForPendingWrites(
294397
firestore: FirebaseFirestore
295398
): Promise<void> {
@@ -303,6 +406,12 @@ export function waitForPendingWrites(
303406
return deferred.promise;
304407
}
305408

409+
/**
410+
* Re-enables use of the network for this Firestore instance after a prior
411+
* call to {@link disableNetwork()}.
412+
*
413+
* @return A promise that is resolved once the network has been enabled.
414+
*/
306415
export function enableNetwork(firestore: FirebaseFirestore): Promise<void> {
307416
firestore._verifyNotTerminated();
308417

@@ -314,6 +423,14 @@ export function enableNetwork(firestore: FirebaseFirestore): Promise<void> {
314423
});
315424
}
316425

426+
/**
427+
* Disables network usage for this instance. It can be re-enabled via {@link
428+
* enableNetwork()}. While the network is disabled, any snapshot listeners,
429+
* `getDoc()` or `getDocs()` calls will return results from cache, and any write
430+
* operations will be queued until the network is restored.
431+
*
432+
* @return A promise that is resolved once the network has been disabled.
433+
*/
317434
export function disableNetwork(firestore: FirebaseFirestore): Promise<void> {
318435
firestore._verifyNotTerminated();
319436

@@ -325,6 +442,28 @@ export function disableNetwork(firestore: FirebaseFirestore): Promise<void> {
325442
});
326443
}
327444

445+
/**
446+
* Terminates the provided Firestore instance.
447+
*
448+
* After calling `terminate()` only the `clearIndexedDbPersistence()` function
449+
* may be used. Any other function will throw a `FirestoreError`.
450+
*
451+
* To restart after termination, create a new instance of FirebaseFirestore with
452+
* {@link getFirestore()}.
453+
*
454+
* Termination does not cancel any pending writes, and any promises that are
455+
* awaiting a response from the server will not be resolved. If you have
456+
* persistence enabled, the next time you start this instance, it will resume
457+
* sending these writes to the server.
458+
*
459+
* Note: Under normal circumstances, calling `terminate()` is not required. This
460+
* function is useful only when you want to force this instance to release all
461+
* of its resources or in combination with `clearIndexedDbPersistence()` to
462+
* ensure that all local state is destroyed between test runs.
463+
*
464+
* @return A promise that is resolved when the instance has been successfully
465+
* terminated.
466+
*/
328467
export function terminate(firestore: FirebaseFirestore): Promise<void> {
329468
_removeServiceInstance(firestore.app, 'firestore-exp');
330469
return firestore._delete();

0 commit comments

Comments
 (0)