Cloud Firestore Lite Web SDK

Firestore 是可靠的可擴充資料庫解決方案,可保持網路資料同步 用戶端。

對許多應用程式而言,Firestore 的代管離線支援尤其重要。 可讓您建構回應式應用程式,在網路延遲或網路延遲的情況下運作 網際網路連線。但功能豐富的 SDK 需要耗費大量資源。內容 Firebase 是針對只需要使用基本建立、讀取、更新的應用程式 不需要代管離線支援?

解決方案:Firestore Lite

Firestore Lite 是輕量、獨立且僅支援 REST 的 Firestore SDK, 在單一 Pod 中, 是一般 Web SDK 大小的一部分Firestore Lite 會略過延遲時間補償, 離線快取、查詢恢復和快照事件監聽器,但尤其需要 但程式庫大小和啟動時間縮減,因此可做出極佳的取捨。

匯入 Firestore Lite

Firestore Lite 可透過 npm 取得, 模組化 SDK:是 因此可完全模組化且可進行樹狀變焦

支援下列匯入樣式。

import { initializeApp } from "firebase/app";
import {
   getFirestore,
   getDoc,
   updateDoc
} from 'firebase/firestore/lite';

Firestore Lite 不支援的 API 功能

就大小和速度而言,Firestore Lite 會在標準中省略這些功能 Firestore SDK:

  • DocumentSnapshot 事件處理常式onSnapshot 方法和 DocumentChangeSnapshotListenerOptionsSnapshotMetadata、 不包含 SnapshotOptionsUnsubscribe 物件。
  • 持續性輔助程式enableIndexedDBPersistence enableMultiTabIndexedDbPersistenceclearIndexedDbPersistence 方法 未包含。
  • Firestore 套裝組合loadBundle 方法和相關 方法,以及 LoadBundleTaskLoadBundleTaskProgress 物件則不是 包含。

實作文件擷取、查詢和更新作業

匯入 Firestore Lite 之後,您就能使用熟悉的所有 API 和更新通話。新增資料的用途 以及取得資料

import {
 getFirestore,
 getDoc,
 updateDoc,
 doc
} from '@firebase/firestore/lite';

const firestore = getFirestore(app);
const docRef = doc(firestore, 'collection/doc');
const docSnap = await getDoc(docRef);
await updateDoc(docRef, "field", 'value');

使用 Firestore Lite 的時機

停止使用標準 Firestore SDK 時 包括離線持續性和快取功能您應該瞭解這些功能 後再考慮更換,以降低 Firestore Lite 的負擔。於 一般而言,在決定是否使用 Firestore Lite 時,請考量下列因素:

  • 線上狀態 - Firestore Lite 適用於不需要實際運作的應用程式 並且具備網路連線
  • 大小限制 - 如要降低 整體 JavaScript 套件大小。