这更像是一个理论上应该如何设置数据库,而不是编程。
假设我有一个充满卡片的新闻源,每个卡片都包含一条消息和类似的计数。每个用户都能够喜欢一个mesesage。如果他们已经喜欢该特定卡,我希望它显示给用户。 (就像你在Facebook上看到你喜欢的帖子一样,即使你几天后回来)
您将如何使用此Firestore类型数据库实现该功能?速度绝对是一个问题..
故事本地不是一个选项,我的猜测是在每个卡片对象上,你必须引用一个只保留喜欢它的人的列表的集合。唯一的问题就是更多的问题......感觉它会很慢......
有更好的方法吗?
答案 0 :(得分:0)
此方法需要更多设置,即cron服务,Firestore Security Rules和Cloud Functions for Firebase的知识。话虽如此,以下是我提出的最佳方法。请注意,只显示所需的伪规则。
/*
allow read
allow update if auth.uid == admin_uid and the
admin is updating total_likes ...
*/
messages/{message_key} : {
total_likes: <int>,
other_field:
[,...]
}
/*allow read
allow write if newData == {updated: true} and
docId exists under /messages
*/
messages_updated/{message_key} : {
updated: true
}
/*
allow read
allow create if auth.uid == liker_uid && !counted && !delete and
liker_uid/message_key match those in the docId...
allow update if auth.uid == admin_uid && the admin is
toggling counted from false -> true ...
allow update if auth.uid == liker_uid && the liker is
toggling delete ...
allow delete if auth.uid == admin_uid && delete == true and
counted == true
*/
likes/{liker_uid + '@' + message_key} : {
liker_uid:,
message_key:,
counted: <bool>,
delete: <bool>,
other_field:
[,...]
}
count_likes/{request_id}: {
message_key:,
request_time: <timestamp>
}
每隔X分钟触发一次,以计算可能包含所有消息的消息。
/messages_updated
了解BATCH_SIZE文档/count_likes
doc w / fields request_time和message_key。触发onCreate of count_likes / {request_id}
/messages_updated
删除已创建的docs message_key。/likes
以获取文档,其中message_key ==创建了docs message_key,其中count == false。/likes
以获取文档,其中message_key ==创建了docs message_key,其中delete == true,其中count == true。/messages
。/count_likes
删除此文档。 每Y分钟触发一次,删除从未遇到的/count_likes
次请求。
/count_likes
下查询request_time早于Z。/likes
下查询liker_uid等于您的uid的文档,其中message_key等于消息的密钥,其中delete == false。如果存在doc,你就喜欢它了。/likes
和batch.set a /messages_updated
下添加消息,批量设置。如果此批处理失败,请尝试使用batch_two.update,方法是将其delete字段更新为false,并将batch_two.set更新为/messages_updated
。/messages_updated
来批量更新。/likes
来查看哪些人喜欢哪条消息。/likes
来查看用户喜欢的所有邮件。/likes
下执行安全写入。在实时数据库中,这是possible。在Firestore中,它是可能的,但有点hacky。如果您可以等待并且不想使用hacky方法,请在开发中使用常规无限制管理员,直到Firestore支持使用有限权限进行身份验证。