我正在开发一个具有(哇哇惊喜!)聊天功能的 iOS 应用程序。整个应用程序大量使用 Firebase 工具,对于数据库,我使用的是新的 Cloud Firestore 解决方案。
目前,我正在使用数据库规则加强安全性,但我在使用自己的数据模型时遇到了一些困难:) 这可能意味着我的数据模型选择不当,但我对此非常满意,除了实现规则部分。
模型的对话部分如下所示。在我的数据库的根目录下,我有一个 conversations
收藏:
/conversations/$conversationId
- owner // id of the user that created the conversation
- ts // timestamp when the conversation was created
- members: {
$user_id_1: true // usually the same as 'owner'
$user_id_2: true // the other person in this conversation
...
}
- memberInfo: {
// some extra info about user typing, names, last message etc.
...
}
然后我在每个对话中都有一个子集,名为 留言 .消息文档是一个非常简单的文件,仅包含有关每个已发送消息的信息。
/conversations/$conversationId/messages/$messageId
- body
- sender
- ts
以及模型的截图:
上的规则对话 文档相当简单且易于实现:
match /conversations/{conversationId} {
allow read, write: if resource.data.members[(request.auth.uid)] == true;
match /messages/{messageId} {
allow read, write: if get(/databases/$(database)/documents/conversations/$(conversationId)).data.members[(request.auth.uid)] == true;
}
}
问题
我的问题是该对话中的消息子集合。以上有效,但我不喜欢使用
get()
在那里打电话。每个
get()
call 执行读取操作,因此会影响我月底的帐单,请参阅 documentation .如果我正在构建的应用程序成功,这可能会成为一个问题,当然读取的文档确实很少,但是每次用户打开对话时都这样做似乎有点低效。我真的很喜欢我模型中的子集合解决方案,但不确定如何有效地实现这里的规则。
我对任何数据模型更改持开放态度,我的目标是在没有这些的情况下评估规则
get()
调用。任何想法都非常受欢迎。
最佳答案
老实说,我认为你对你的结构没问题,get
按原样调用。原因如下:
关于firebase - 子集合的 Cloud Firestore 规则,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47809552/