0

I have 2 models - User & Project.

Relations are -

  1. Users can have multiple projects (One to many)
  2. One projects may be shared with multiple other users. (Many to many)
const UserSchema = new Schema({
    name: {
        type: String,
        required: true,
    },
    mobile: {
        type: Number,
        required: true,
        unique: true
    },
    email: {
        type: String,
        required: true,
        unique: true
    },
    password: {
        type: String,
        required: true,
    },
    role: {
        type: String,
        default: 'basic',
        enum: ["basic", "admin"]
    },
    projects: [
        {
            type: Schema.Types.ObjectId,
            ref: 'Project'
        }
    ],
    shared_projects: [
        {
            type: Schema.Types.ObjectId,
            ref: 'Project'
        }
    ]
}, { timestamps: true });
const ProjectSchema = new Schema({
    name: {
        type: String,
        required: true,
    },
    active: {
        type: Boolean,
        default: false,
    },
    expiry: {
        type: Date,
        default: null,
    },
    owner: {
        type: Schema.Types.ObjectId,
        ref: 'User'
    },
    shared_users: [
        {
            type: Schema.Types.ObjectId,
            ref: 'User'
        }
    ]
}, { timestamps: true });

I want to have a feature that the owner of the project may disable one or many shared user. So, I need a status field in ProjectSchema like -

shared_users: [
        {
            type: Schema.Types.ObjectId,
            ref: 'User'
        },
        status: {
            type: Boolean,
            default: true
        }
    ]

So, do I need to have the same definition in UserSchema also & update both schemas when user disable or enable a shared user?

Debarshi Das
  • 49
  • 1
  • 9
  • 1
    You will need to wrap each shared_user to object, shared_users:[{shared_user:{ user: {type:Schema.Types.ObjectId, ref: 'User.},status: {type: Boolean,default: true}}}] – pixy Jun 20 '21 at 16:40
  • So, I have to do the same for shared_projects in the ProjectScema right? And each time a user enable or disable any shared_user, I have to update both models? – Debarshi Das Jun 20 '21 at 16:53
  • What do you want to achieve with shared_projects? If you say that the project may disable one or many shared user, how do you specify the project was shared or not? – pixy Jun 20 '21 at 17:04
  • Sorry, I mean shared_projects in the UserSchema. So that users can interact with the enabled shared_projects only. – Debarshi Das Jun 20 '21 at 17:12
  • 1
    You can rely on the project schema only and populate the projects, if you need selection then add the "shared_user" onto the array. Then you will get the list of shared_user inculded.. once get the list you can do anything, such as filter wheter the projects shared or not – pixy Jun 20 '21 at 17:29
  • Or you can use match opt when you populating. This might help https://stackoverflow.com/q/36371190/12499558 – pixy Jun 20 '21 at 17:38

0 Answers0