0

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.

  1. What's the new database structure should look like if I need to add role (string) to each user within a project?

  2. 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.
Community
  • 1
  • 1
TheUnreal
  • 23,434
  • 46
  • 157
  • 277

1 Answers1

2

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.

Dravidian
  • 9,945
  • 3
  • 34
  • 74
  • Thank you very much this feels right! Can you give an example about how iteration over this structure should look like? Example: https://www.firebase.com/docs/web/guide/structuring-data.html#section-join – TheUnreal Oct 18 '16 at 18:35
  • Please avoid extending queries in comments, Might i suggest that you try first yourself, and if get bumped, post another question on SO. Surely sum1 will help you out... Also you are following depracated firebase docs use these : -https://firebase.google.com/docs/ :) Happy Coding – Dravidian Oct 18 '16 at 18:39
  • Sure thanks. Just one question regards your answer: Why did you choose to use user1/projectkey1 and not projectkey1/user1 .. ? (Users under projects over projects inder users) – TheUnreal Oct 18 '16 at 20:02
  • Because userID can be easily accessed as a key while its pain to retrieve the projectKey , and sometimes you would wanna know which groups the user has subscribed to. If not then , if i think of it now, you wouldn't even need the **`projectsLists`** node , as **`projectsRoles`** has the user list under these sub nodes, which will be a self evident proof for the user that the user has subscribed to this group. – Dravidian Oct 18 '16 at 20:41
  • Great answer Dravidian! But adding a user to a project still requires two updates, doesn't it: one under `projectsList`, one under `projectsRoles`. I think that's totally fine btw, I just want to check if you understanding of your data model is correct. – Frank van Puffelen Oct 18 '16 at 20:44
  • Not really, actually `projectsLists` will only be updated when a user will enroll into a project and `projectsRoles` will only be updated when a user will be given a role in that project. In OP's issue he referred to updating the the DB twice every time a user chooses a projetc once at the user node and the other at the projects node , which can be avoided in this . – Dravidian Oct 18 '16 at 20:51
  • Doesn't adding a user to a project require that you add that project to their list and give them a role in the project? – Frank van Puffelen Oct 18 '16 at 21:02
  • I am not sure about the specifics of the OP's requirement but if he provides the user role directly after adding a projectKey to the list then evidently yes, he would. I was going with the possibility that the user role is given later on...(eg:- Discord app type scenario) – Dravidian Oct 18 '16 at 21:07
  • @FrankvanPuffelen Yes I do. An user must have a role in a project. He can't do anything without a role. I will have to do two updates, but it's not a problem as soon as the structure is fine. Dravidian Would you advice to keep with this structure in this case? I don't see something wrong. – TheUnreal Oct 19 '16 at 06:37