简体   繁体   English

我可以强制 Firestore 文档具有特定的类型或数据 model(即 static 类型)吗?

[英]Can I force a Firestore document to have a specific Type or data model (i.e. static typing)?

For example, if I have a Firestore document that looks like this:例如,如果我有一个如下所示的 Firestore 文档:

{
  threadId: 'abc',
  authorId: 'abc',
  content: 'blah blah blahh'
}

Can I make Firebase throw an error if I accidentally write some code that adds another field, like this:如果我不小心编写了一些添加另一个字段的代码,我可以让 Firebase 引发错误,如下所示:

{
  accident: 'OOPS! THIS FIELD SHOULD NOT BE HERE',
  threadId: 'abc',
  authorId: 'abc',
  content: 'blah blah blahh'
}

Like static typing for the shape of each document and enforced from the Firestore side.就像 static 一样,输入每个文档的形状并从 Firestore 端强制执行。 Is that possible?那可能吗?

A possible solution would be to create a Cloud Function, that reads the content of your document once it is updated.一个可能的解决方案是创建一个 Cloud Function,它会在文档内容更新后读取它。 So for example, if your document has a fixed number of fields, then you should always check against that number, in your example, it would be three.例如,如果您的文档有固定数量的字段,那么您应该始终检查该数字,在您的示例中,它将是三个。 If for example, the result of the check would be four, then you can take some actions.例如,如果检查的结果是四,那么您可以采取一些措施。

Another approach would be to check the existence of a field in a document, before updating it.另一种方法是在更新文档之前检查文档中是否存在字段。 If that particular field exists, do the update, otherwise, throw an error.如果该特定字段存在,请进行更新,否则,抛出错误。

If you write the data from a client-side SDK, you can use security rules to validate the data.如果您从客户端 SDK 写入数据,您可以使用安全规则来验证数据。 For some examples of this, see the Firestore documentation on validating data in security rules .有关这方面的一些示例,请参阅有关在安全规则中验证数据的 Firestore 文档。

For example, to test that the document has only the three fields you mention, you could do:例如,要测试文档是否只有您提到的三个字段,您可以执行以下操作:

allow write: if request.resource.data.keys.hasOnly("threadId", "authorId", "content")

What you actually need is to use FirestoreDataConverter .您真正需要的是使用FirestoreDataConverter You can transform your own objects of type <T> to Firestore data by using the withConverter() method.您可以使用withConverter()方法将您自己的<T>类型的对象转换为 Firestore 数据。 Also you will have your static typed objects (documents) while retrieving them from Firestore.在从 Firestore 中检索它们时,您还将拥有 static 类型的对象(文档)。 All you need is to implement two methods toFirestore() and fromFirestore()您只需要实现两个方法toFirestore()fromFirestore()

  1. Let's say you have:假设您有:
class Thread {
  constructor(threadId: String, authorId: String, content: String) {}
}
  1. Define a converter:定义一个转换器:
const threadsConverter = {
  toFirestore(thread: Thread): DocumentData {
    return { threadId: thread.threadId, authorId: thread.authoId, content: thread.content }
  },
  fromFirestore(snapshot: QueryDocumentSnapshot): Thread {
    const data = snapshot.data();
    const thread = new Thread(data.threadId, data.authorId, data.content);
    return thread
  }
}
  1. Use it:用它:
   // write:
   const myObject = {
     accident: 'OOPS! THIS FIELD SHOULD NOT BE HERE',
     threadId: 'abc',
     authorId: 'abc',
     content: 'blah blah blahh'
   }
   const threadRef = firestore.collection("thread").withConverter(threadsConverter).doc()
   await threadRef.set(myObject);
   
   // read:
   const threads = await firestore.collection("threads").withConverter(threadsConverter).get();

And that's it.就是这样。 Hope it helps.希望能帮助到你。

PS Didn't test the code. PS没有测试代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 有没有办法让 Firestore 文档字段与另外两个的结果相匹配? 即字段 A + 字段 B = 字段 C - Is there a way to make a Firestore document field match the result of two others? I.e. field A + field B = field C Flutter + Cloud firestore + Closures:为什么我可以将文档打印到控制台,但不能添加到列表中? - Flutter + Cloud firestore + Closures: Why can I print the document to the console, but not add to a list? Firestore:我什么时候必须删除 SnapshotListener? - Firestore: When do I have to remove SnapshotListeners? 我无法在 firestore 数据库集合中将列 Kind 更改为 Type - I can't change the column Kind to Type on the firestore database collection 如何按顺序显示 Firebase Firestore 数据? - How can I display Firebase Firestore data in order? 如何在验证电话号码之前访问 Firestore 数据? - How can I access firestore data before authenticating phone number? Firestore 数据到 Model class - Firestore data into a Model class 我想要一个 firebase 文档的下拉列表,但无法获取值 - i want to have a dropdown of firebase document but can`t get the values 如何在 flutter 中获取 firestore 文档的 documentid? - How do I get documentid of a firestore document in flutter? 从 flutter firestore 中的子集合中获取特定文档 - fetching a specific document from a subcollection in flutter firestore
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM