MongoDB / Mongoose for many relationships - user, project and role

Our project has the following requirements:

  • A user can be part of several projects.
  • A project can have several users.
  • The user can be either the owner, or the administrator or viewer of the project.
  • Obviously, there is only one project owner.
  • There may be several Admin or Viewer projects.
  • Each user has a home / default project.

So, I created the following schemes. But I can't use populate, and I find adding / updating / removing a lot of pain. Also, when I request projects, I want to show the email id of users, not objectId, but I don’t want to use email as a link, as the user can change his email address. Please help me with the best design, given the most popular operations on the circuit -

  • User can create a project.
  • Add people to the project
  • View all its projects and participants
  • Change the role of someone in the project
  • Remove someone from the project
  • View projects and owners and members (emails, not object identifiers) according to their right <

The following workflows are present in separate user.js and project.js files with the required model export.

    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}]
   });
+4

Source: https://habr.com/ru/post/1678187/


All Articles