I have relations defined like,
device_data.belongsTo(models.device)
device.belongsTo(models.category)
category.belongsTo(models.role)
What I am trying to get is all the device data which falls under a specific role. Right now I have the following working:
const query = {
include: [{
model: device,
attributes: ['id'],
include: [{
model: category,
attributes: ['id'],
include: [{
model: role,
attributes: ['id'],
where: {
id: {
[Op.eq]: roleId,
},
},
required: true,
}],
required: true,
}],
required: true,
}],
};
Obviously, this gives returns me formatted data like following:
{
"id": 1,
"created_timestamp": "2019-08-12T12:28:36.194Z",
"device_id": 1,
"device_value": 0,
"initiator_id": 2,
"device": {
"id": 1,
"category": {
"id": 1,
"role": {
"id": 1
}
}
}
},
Where role is nested in 3rd level and the role.id in 4th.
What I am trying to achieve is:
{
"id": 1,
"created_timestamp": "2019-08-12T12:28:36.194Z",
"device_id": 1,
"device_value": 0,
"initiator_id": 2,
"device": 1,
"category": 1,
"role": 1
},
I would appreciate a lot if someone could point me a hint or possibly the query to generate data like this from sequelize. Thank you very much.