[英]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()
class Thread {
constructor(threadId: String, authorId: String, content: String) {}
}
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
}
}
// 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.