'INNER JOIN on array of id's which returns array of objects by using sequelize raw query in node.js
I have data in the below format
{
"userId": 1
"stateId": 199,
"productId": [
1,
2
]
}
I want to get this type of response by using a sequelize raw query, I can get a response for userId and stateId by using inner join,
const List = await db.sequelize.query(
'SELECT `user`.`id` AS `user.id`,`user`.`role` AS `user.role`,`user`.`name` AS `user.name`,`state`.`stateId` AS `state.stateId`,`state`.`stateName` AS `state.stateName`,state`.`Specialization` AS `state.Specialization` FROM details,INNER JOIN user ON `user`.`userId`=details.userId INNER JOIN state ON `state`.`stateId`=details.stateId WHERE ORDER BY ASC LIMIT :offset, :limit',
{
nest: true, mapToModel: true, replacements: {
limit: limit,
offset: offset
}
});
}
but cannot get it for a product from an array of IDs.
{
"user": {
"id": 1,
"role": "USER",
"name": "user1"
},
"state": {
"stateId": 199,
"stateName": "test",
"Specialization": null
},
"products": [
{
"productId": 1,
"productName": "Test product 1"
},
{
"productId": 2,
"productName": "Test product 2"
}
]
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|