首页 > 解决方案 > 文本注释应用程序的 Cloud Firestore 架构

问题描述

我有一个用于阅读和注释文本的应用程序,想知道如何最好地构建底层 Firestore 数据库。

应用程序本身是一个在浏览器中运行的相对简单的 ReactJS SPA。所有用户都可以独立地将文本文档上传到系统中,然后在用户界面中对这些文档进行注释。要进行注释,用户打开文档,单击一个单词,然后在弹出窗口中输入有关该单词的一些元数据。然后,系统会突出显示该用户的所有文档中该词的每一次出现,并根据所提供的元数据使用不同的颜色。

我最初的计划是创建 2 个独立的集合:

我现在有点担心这样的数据库方案会带来相对较高的运营成本,因为每次显示文本文档时都必须从数据库中加载相关的注释(而 Firestore Blaze 计划将向我收取 0.06 美元的费用) 100.000 次读取)。

是否有更好(更具成本效益)的方式来构建这个数据库?

标签: firebasegoogle-cloud-firestorenosql

解决方案


您应该仅通过仅检索文档的最新版本来获取已更改的数据并使用缓存。如果没有任何变化,这将只导致一次读取操作。通常,该get()调用仅检索文档的最新快照并忽略脱机缓存。但是,如果网络不可用或请求超时,您可以利用离线缓存。

var docRef = db.collection("cities").doc("SF");

// Valid options for source are 'server', 'cache', or
// 'default'. See
// https://firebase.google.com/docs/reference/js/firebase.firestore.GetOptions
// for more information.
var getOptions = {
    source: 'cache'
};

// Get a document, forcing the SDK to fetch from the offline cache.
docRef.get(getOptions).then(function(doc) {
    // Document was found in the cache. If no cached document exists,
    // an error will be returned to the 'catch' block below.
    console.log("Cached document data:", doc.data());
}).catch(function(error) {
    console.log("Error getting cached document:", error);
});

推荐阅读