6

Our project has following requirements-

  1. User can be part of multiple projects
  2. A project can have multiple users.
  3. User can be either Owner or Admin or Viewer of a project.
  4. Obviously there can only one Owner of a project.
  5. There can be multiple Admin or Viewer of a project.
  6. Every user has a home/default project.

So I have created the following schemas - But I am not able to use populate and I find adding/ updating/ removing a lot of pain. Also when I query projects I want to show the email id of the users not the objectId but dont want to use email as a ref as user can change their email. Please help me with a better design considering the most popular operations on the schema-

  1. User can create project
  2. Add people to a project
  3. View all of his projects and members
  4. Change role of someone in the project
  5. Remove someone from the project
  6. View the projects and owner and members (emails not object ids) according to their right

The following working schemas are present in separate files user.js and project.js with required export of the models.

    var userSchema = mongoose.Schema({
      username: {type:String},
      password: String,
      email: {type:String, required:true, unique: true, index: true},
      createdAt: { type: Date, default: Date.now },
      firstName: String,
      lastName: String,
      home_project: {type:Schema.Types.ObjectId, ref:Project},
      owner: [{type:Schema.Types.ObjectId, ref:Project}],
      admin: [{type:Schema.Types.ObjectId, ref:Project}],
      viewer: [{type:Schema.Types.ObjectId, ref:Project}]
   });

    var projectSchema = mongoose.Schema({
      projectName: {type:String, required:true, unique: true, index: true},
      createdAt: { type: Date, default: Date.now },
      owner: {type:String, ref:User},
      admin: [{type:String, ref:User}],
      viewer: [{type:String, ref:User}]
   });
Mikey
  • 6,728
  • 4
  • 22
  • 45
Arnab Naha
  • 61
  • 2
  • 1
    This [answer](https://stackoverflow.com/q/25101386/1022914) might be useful. I usually go with Option 1 or 2. – Mikey May 30 '17 at 16:13
  • But how do I deal with multiple roles...meaning there can be multiple roles other than admin owner viewer etc at a later point of time – Arnab Naha May 30 '17 at 18:40

0 Answers0