'Sails nested array association of Models

A Mongodb collection (say 'Competency') whose structure is as follows:

{
    "staff_id": "CEB",
    "competency": [{
        "workflow": "Workflow A",
        "task_competency": ["Workflow_A_Task_1", "Workflow_A_Task_2"]
    }, {
        "workflow": "Workflow B",
        "task_competency": ["Workflow_B_Task_1", "Workflow_B_Task_2"]
    }]
},
{
    "staff_id": "XD",
    "competency": [{
        "workflow": "Workflow A",
        "task_competency": ["Workflow_A_Task_3"]
    }, {
        "workflow": "Workflow B",
        "task_competency": ["Workflow_B_Task_2", "Workflow_B_Task_3"]
    }]
}

Competency collection's Model is as follows:

// Competency.js
attributes: {
    staff_id:{
      type: 'string'
    },
    competency:{
      type: 'json'
    }
  }

Another Mongodb collection (say 'Workflows') structure is as follows:

{
    "workflow": "Workflow B",
    "tasks": [{
        "task_id": "Workflow_B_Task_1",
        "task_name": "Loading B",
        "task_abbr": "LB"
    }, {
        "task_id": "Workflow_B_Task_2",
        "task_name": "Planning B",
        "task_abbr": "PB"
    },{
        "task_id": "Workflow_B_Task_3",
        "task_name": "Checking B",
        "task_abbr": "CB"
    },{
        "task_id": "Workflow_B_Task_4",
        "task_name": "Review B",
        "task_abbr": "RB"
    }]
},
{
    "workflow": "Workflow A",
    "tasks": [{
        "task_id": "Workflow_A_Task_1",
        "task_name": "Loading A",
        "task_abbr": "LA"
    }, {
        "task_id": "Workflow_A_Task_2",
        "task_name": "Planning A",
        "task_abbr": "PA"
    },{
        "task_id": "Workflow_A_Task_3",
        "task_name": "Checking A",
        "task_abbr": "CA"
    },{
        "task_id": "Workflow_A_Task_4",
        "task_name": "Review A",
        "task_abbr": "RA"
    }]
}

Workflows collection's Model is as follows:

// Workflows.js
attributes: {
    workflow:{
      type: 'string'
    },
    tasks:{
      type: 'json' 
    }
  }

I need to associate the competency attribute of Competency to Workflows such that the result will be as follows:

{
    "staff_id": "CEB",
    "competency": [{
        "workflow": "Workflow A",
        "task_competency": [{
        "task_id": "Workflow_A_Task_1",
        "task_name": "Loading A",
        "task_abbr": "LA"
     },{
        "task_id": "Workflow_A_Task_2",
        "task_name": "Planning A",
        "task_abbr": "PA"
    }]
    }, {
        "workflow": "Workflow B",
        "task_competency": [{
        "task_id": "Workflow_B_Task_1",
        "task_name": "Loading B",
        "task_abbr": "LB"
    },{
        "task_id": "Workflow_B_Task_2",
        "task_name": "Planning B",
        "task_abbr": "PB"
    }]
    }]
},
{
    "staff_id": "XD",
    "competency": [{
        "workflow": "Workflow A",
        "task_competency": [{
        "task_id": "Workflow_A_Task_3",
        "task_name": "Checking A",
        "task_abbr": "CA"
    }]
    }, {
        "workflow": "Workflow B",
        "task_competency": [{
        "task_id": "Workflow_B_Task_2",
        "task_name": "Planning B",
        "task_abbr": "PB"
    }, {
        "task_id": "Workflow_B_Task_3",
        "task_name": "Checking B",
        "task_abbr": "CB"
    }]
    }]
}

Is the above scenario achievable with Sails Model association or should I have to do this manually? If manual, any efficient suggestion in terms of runtime performance would be even more helpful.

Any help would be appreciated!

Thanks



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source