Firestore:活动 /饲料 /时间表的数据建模
我正在尝试为我的应用程序找到适合某种活动的Firestore数据模型。
:
- 公司A的用户创建新任务并将其分配给公司B的
示例
- B。
- 用户 至少有两家公司(发件人和接收器)
- 用户应该能够归档活动。如果用户存档活动,则仅为该特定用户存档。其他用户仍然可以访问此活动,直到他们自己存档为止。
当前状态:
// data model: activities collection
affected_companies: Array,
affected_user_roles: Map,
description: String
// Example
affected_companies:['c1', 'c2'],
affected_user_roles: { '2': true, '5': true },
description: 'Task A1'
信息:因为我们不能使用多个array-contains
运算符,而且我的数量有限user_role_ids
,所以我只使用affected_user_roles
的字段名称。
查询此集合非常容易:
let company_id = 'c1';
let user_role_id = '2';
// Query activities based on given company_id and user_role_id
db.collection('activities').ref
.where('affected_companies', 'array-contains', company_id)
.where('affected_user_roles.' + user_role_id, '==', true)
问题:如何仅查询非囚禁活动? 如上所述,用户应该能够为自己存档一项活动(=将其隐藏起来)。
方法1: 实际上,我只需将另一个数组添加到我的活动
文档中,所有user_ids
他们已自行存档该活动。然后,我会在我的查询中添加。尽管这行不通。不允许多个
阵列包容器
。
方法2: 我还考虑过类似于affected_user_roles
:使用user_ids作为字段名称并设置boolean value。尽管我需要查询不存在值的文档,但据我所知,这是不可能的:
// Using user_ids as field names
archived_by_users: { '111': true, '241': true }
// Does not work because field 'archived_by_users.557' does not exist.
let user_id = '557';
.where('archived_by_users.' + user_id, '==', false)
方法3: 在每个活动> Acception
文档中,使用子策略Archived_by_user
我将在其中存储每个已存档活动的用户的user_id
。尽管不可能根据其子集合中的值查询文档。
我没有提出一个很好的解决方案。有效的是:
- 在客户端上过滤(不是一个选择,因为活动提要将增长到数千个文档。目标是仅获取非囚禁的文档)
- 添加另一个集合
users_activities
跟踪用户存档的活动
。尽管这需要其他查询,并且可能不会为我节省任何阅读。
有人可以在这里想到一个好的方法吗?欢迎每个答案。先感谢您!
I'm trying to find a suitable Firestore data model for some kind of activity feed for my application.
Example:
- A user from company A creates new task and assigns it to a user of company B.
Requirements:
- Users should see activities based on their company_id and user_role_id
- Every activity affects at least two companies (sender and receiver)
- Users should be able to archive activities. If a user archives an activity, it is archived for this particular user only. Other users can still access this activity until they archive it for themselves.
Current state:
// data model: activities collection
affected_companies: Array,
affected_user_roles: Map,
description: String
// Example
affected_companies:['c1', 'c2'],
affected_user_roles: { '2': true, '5': true },
description: 'Task A1'
Info: because we can't use multiple array-contains
operators and I have a limited amount of user_role_ids
, I simply use the field names for affected_user_roles
.
Querying this collection is quite easy:
let company_id = 'c1';
let user_role_id = '2';
// Query activities based on given company_id and user_role_id
db.collection('activities').ref
.where('affected_companies', 'array-contains', company_id)
.where('affected_user_roles.' + user_role_id, '==', true)
Problem: How to query only non-archived activities?
As described above users should be able to archive an activity for themselves (= hide it from their feed).
Approach 1:
Practically I would simply add another array to my activity
documents with all user_ids
who have archived the activity for themselves. I would then add .where('core.archived_by_users', 'array-contains', user_id)
to my query. Although this does not work. Multiple array-contains
are not allowed.
Approach 2:
I also thought about doing it similar to affected_user_roles
: use the user_ids as field names and set boolean values. Although I would need to query for documents where a value does not exist, which is impossible as far as I know:
// Using user_ids as field names
archived_by_users: { '111': true, '241': true }
// Does not work because field 'archived_by_users.557' does not exist.
let user_id = '557';
.where('archived_by_users.' + user_id, '==', false)
Approach 3:
Using a subcollection archived_by_users
in every activity
document where I would store the user_id
of each user who has archived the activity. Although it's not possible to query documents based on a value in their subcollection.
I did not come up with a good solution. What would work is:
- Filter on client-side (not really an option since the activity feed will grow to thousands of documents. The goal is to fetch only the non-archived ones)
- Add another collection
users_activities
which keeps track of theactivities
archived by users. Although this would require additional queries and probably won't save me any reads.
Can anybody think of a good approach here? Every answer is welcome. Thank you in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论