NoSQL& Firebase:存储&迭代多对多的关系 [英] NoSQL & Firebase: Storing & Iterating over many-to-many relationship
问题描述
每个用户都可以分配到多个项目,在不同的角色。
在阅读了关于如何构建多对多关系之后,我看到它应该是这样的:
用户:{
user1:{
名称:blah,
电子邮件地址:a@a.com,
项目:{
projects1key:true,
projects2key:true
}
}
}
项目:{
project1key:{
name:blahserve,
category:blahbers,
providers:{
user1:true,
user7:true
}
}
}
我无法弄清楚如何为每个用户分配一个角色在每个项目中。
-
如果我需要添加
角色,新的数据库结构应该是什么样子
(string)给项目中的每个用户?
$ b $ p更新
由于这里给出的答案是正确的,但不能确定它适合我的用例,所以这是我的用例:
- ( 1)遍历所有项目用户的列表
- (2)遍历所有成员项目的列表
- (3)检查项目角色(4)项目 - >用户页面允许你添加一个新的用户到现有的项目中。当用户访问一个项目,并给予他的权限取决于他的角色 用户角色与用户一起以相同的形式被选取。用户在项目中必须具有角色。 或者,也许只是你可以添加一个不同的节点:$ b
$ blockquote
如果我需要添加角色,新的数据库结构应该是什么样子(字符串)给项目中的每个用户?
用户:{
user1 :{
name:blah,
email:a@a.com,
}
},
项目:{
projectKey1: {
名称:blahserve,
类别:blahbers,
}
},
项目列表:{
user1:
projectKey1:true,
projectKey2:true
}
},
projectsRoles:{
projectKey1:{
user1:provider,
user2:editor,
....
}
}
纠正我,如果我错了:当我分配用户到一个项目,我需要cr eate 2个新节点:我的用户节点中的一个projectkey节点,以及我的projectkeynode中的一个用户键节点。是吗?
总是比较平淡的DB结构。所以使用这个结构,你只能追加或更新你的用户的角色一次。
I have 2 main nodes in my database: Users and Projects.
Each user can be assigned to multiple projects, in different roles.
After reading about how to structure many to many relationship, I ended up that it should look like this:
users : { user1 : { name : blah, email : a@a.com, projects : { projects1key : true, projects2key : true } } } projects : { project1key : { name : blahserve, category : blahbers, providers : { user1 : true, user7 : true } } }
What I couldn't figure out is how I can assign every user a role in each project.
What's the new database structure should look like if I need to add
role
(string) to each user within a project?Correct me if I'm wrong: When I assign user to a project, I need to create 2 new nodes: a projectkey node in my user node, and a userkey node in my projectkeynode. Is that right?
Update
Since the answer given here is correct but not sure it will fit my use case, this is my use case:
- (1) Iterating over a list of all project users
- (2) Iterating over a list of all member project
- (3) Check for project role when the user access a project and give give him permission depends on his role
- (4) The "Project -> users" page allow you to add a new user to existing project. the user role is picked in the same form together with the user. the user must have a role in a project.
解决方案Or maybe just simply you could add a different node all together:-
What's the new database structure should look like if I need to add role (string) to each user within a project?
users : { user1 : { name : blah, email : a@a.com, } }, projects : { projectKey1 : { name : blahserve, category : blahbers, } }, projectsLists:{ user1 :{ projectKey1 : true, projectKey2 : true } }, projectsRoles :{ projectKey1 : { user1 : provider, user2 : editor, .... } }
Correct me if I'm wrong: When I assign user to a project, I need to create 2 new nodes: a projectkey node in my user node, and a userkey node in my projectkeynode. Is that right?
Always prefer the flatter DB structure. So using this structure you only gotta append or update your user's role once.
这篇关于NoSQL& Firebase:存储&迭代多对多的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!