我刚刚开始了解 Firestore 规则,而且我的头脑正在迅速扩大。
我正在尝试研究如何将规则应用于一个集合,并将另一条规则应用于所有其他集合及其子集合。
所以我从 Firestore 似乎附带的默认规则开始:
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write;
}
}
}
这将允许对所有集合及其文档进行读写访问。
但是假设我想对一个集合中的文档应用规则并保留所有其他集合的默认规则。以下将不起作用:
service cloud.firestore {
match /databases/{database}/documents {
match /suppliers/{supplier} {
allow create: if !exists(/databases/$(database)/documents/supplierABNs/1260)
}
match /{document=**} {
allow read, write;
}
}
}
因为第二条规则将覆盖第一条规则。
有没有办法做我想做的事?
最佳答案
我了解您希望对一个集合中的文档应用规则,并为所有其他集合保留默认规则。有一种方法可以做你想做的事,但你不会喜欢它。
您必须明确指定所有其他集合的默认规则。
这是一个示例。
service cloud.firestore {
match /databases/{database}/documents {
//Rule for Suppliers collection
match /suppliers/{supplier} {
allow create: if !exists(/databases/$(database)/documents/supplierABNs/1260)
}
//Rule for Changelog collection allowing complete access
match /Changelog/{id} {
allow read: if true;
allow write: if true;
}
//Rule for Vendors collection allowing complete access
match /Vendors/{id} {
allow read: if true;
allow write: if true;
}
}
}
注意:Firestore 规则不支持 if else 语句。但是您可以使用 AND 和 OR 条件作为模拟相同的解决方法。
关于firebase - Firestore 规则 if..else,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50710650/