0

This question might be related to database schema design in sails.js waterline.

One task need to record different roles of users. For example, a Task need to know who are fooUsers and barUsers in this task. What is the best way to handle this? (foo and bar are different roles for a user)

More Context

  • A User will have different Roles.
  • The roles of the user might change in the future.
  • A User will only be one Role in one Task.
  • The Role of a User in a specific Task will never change.
  • A Task will have many users involved.
  • All tasks are the SAME type.
  • The total number of Roles will NOT change.

For example:

One task has four users: UserA with Role1, UserB with Role2, UserC with Role3, UserD with Role1.

In the future, UserA might have Role2 and Role3. But that doesn't affect what role of the UserA is in the above task.

My Final Design

// User.js
attributes: {
    tasks: {
        collection: 'task',
        via: 'users'
    },
    roles: {
        collection: 'role',
        via: 'users'
    },
    userTaskRoles: {
        collection: 'userTaskRole',
        via: 'user'
    }
}

// Task.js
attributes: {
    users: {
        collection: "user",
        via: "tasks"
    },
    userTaskRoles: {
        collection: 'userTaskRole',
        via: 'task'
    }
}

// Role.js
attributes: {
    users: {
        collection: 'user',
        via: 'roles'
    },
    userTaskRoles: {
        collection: 'userTaskRole',
        via: 'role'
    }
}

// UserTaskRole.js
attributes: {
    task: {
        model: 'task'
    },
    user: {
        model: 'user'
    },
    role: {
        model: 'role'
    }
}

I updated this question a lot. The above is my final design. Please give me some feedback whether or not this is good or bad and are there any better design patterns?

Thanks.

user2309998
  • 697
  • 2
  • 7
  • 11

1 Answers1

0

You need to provide more context for your use case. Without knowing why you want to do what you want to do, I can provide this answer.

Differentiate foo / bar with a column in your tasks and reference users in a single column like so:

// User.js

attributes:{
  tasks: {
    collection: "task",
    via: 'users'
  }
}

// Task.js

attributes: {
  Users: {
    collection: "user",
    via: "tasks"
  },
  type: {
    type: 'string',
    enum: ['foo','bar']
  }
}
Meeker
  • 5,979
  • 2
  • 20
  • 38
  • I am new to stackoverflow. Sorry about last two comments. I just figured out I can even delete comments. I deleted them. I have already updated the question and added/appended more context in the end of my question. Hopefully, you can understand my case this time. Thank you very much for your help and reply. – user2309998 Feb 12 '15 at 16:57
  • I updated my question again and added my two model designs. I am looking forward to your suggestions. Thanks a lot. – user2309998 Feb 12 '15 at 17:51
  • your current answer will not work in my case because a task will not be able to distinguish a user is which role (either foo or bar) in this task. All tasks are the **same** type. Please check my *PS1* and *PS2* in the question for more context. Thank you very much for your help. – user2309998 Feb 13 '15 at 02:47