My mysql table models :
//mysql
//table structures start
var Contact = sequelize.define('contact', {
gsm: {
type: Sequelize.STRING,
unique: true,
required: true
},
firstName: Sequelize.STRING,
lastName: Sequelize.STRING,
street: Sequelize.STRING,
city: Sequelize.STRING,
})
var Group = sequelize.define('group', {
groupname: Sequelize.STRING
})
var ContactGroup = sequelize.define('contactgroup', {
/* Empty */
})
ContactGroup.belongsTo(Contact, {
onDelete: 'CASCADE'
});
Contact.hasMany(ContactGroup, {
onDelete: 'CASCADE'
});
ContactGroup.belongsTo(Group, {
onDelete: 'CASCADE'
});
Group.hasMany(ContactGroup, {
onDelete: 'CASCADE'
});
This is my first api code:
exports.getNewGroup = (req, res) => {
Group.findAll({
attributes: ['id', 'groupname', [sequelize.fn('COUNT', sequelize.col('contactgroups.groupId')), 'contactsCount']],
include: [{
model: ContactGroup,
attributes: ['id']
}],
group: ['id']
}).then(data => {
return res.status(200).send(data);
}).catch(err => {
return res.status(400).send(err.message);
});
};
I got the first api response like this:
[
{
"id": 14,
"groupname": "Angular",
"contactsCount": 2,
"contactgroups": [
{
"id": 1
}
]
},
{
"id": 15,
"groupname": "React",
"contactsCount": 1,
"contactgroups": [
{
"id": 3
}
]
},
{
"id": 16,
"groupname": "Vue",
"contactsCount": 0,
"contactgroups": []
},
{
"id": 17,
"groupname": "NodeJs",
"contactsCount": 0,
"contactgroups": []
},
{
"id": 18,
"groupname": "RxJS",
"contactsCount": 0,
"contactgroups": []
}
]
This is my second api code:
exports.getNewGroupForProfsms = (req, res) => {
Group.findAll({
include: [{
model: ContactGroup,
attributes: ['id'],
include: [{
model: Contact,
attributes: ['id', 'gsm']
}]
}],
}).then(data => {
return res.status(200).send(data);
}).catch(err => {
return res.status(400).send(err.message);
});
};
This is my second api response:
[
{
"id": 14,
"groupname": "Angular",
"createdAt": "2017-12-15T15:06:39.000Z",
"updatedAt": "2017-12-15T15:06:39.000Z",
"contactgroups": [
{
"id": 1,
"contact": {
"id": 20,
"gsm": "987654321"
}
},
{
"id": 2,
"contact": {
"id": 21,
"gsm": "123456789"
}
}
]
},
{
"id": 15,
"groupname": "React",
"createdAt": "2017-12-15T15:06:45.000Z",
"updatedAt": "2017-12-15T15:06:45.000Z",
"contactgroups": [
{
"id": 3,
"contact": {
"id": 21,
"gsm": "123456789"
}
}
]
},
{
"id": 16,
"groupname": "Vue",
"createdAt": "2017-12-15T15:06:51.000Z",
"updatedAt": "2017-12-15T15:06:51.000Z",
"contactgroups": []
},
{
"id": 17,
"groupname": "NodeJs",
"createdAt": "2017-12-17T16:07:38.000Z",
"updatedAt": "2017-12-17T16:07:38.000Z",
"contactgroups": []
},
{
"id": 18,
"groupname": "RxJS",
"createdAt": "2017-12-21T05:50:50.000Z",
"updatedAt": "2017-12-21T05:50:50.000Z",
"contactgroups": []
}
]
In above code, i wrote two apis, now i want to write single api,
I need this type of response: (i have contactsCount in first api response, i want that in second api response)
Finally I want this type of response with single api code:
[
{
"id": 14,
"groupname": "Angular",
"contactsCount": 2,
"createdAt": "2017-12-15T15:06:39.000Z",
"updatedAt": "2017-12-15T15:06:39.000Z",
"contactgroups": [
{
"id": 1,
"contact": {
"id": 20,
"gsm": "987654321"
}
},
{
"id": 2,
"contact": {
"id": 21,
"gsm": "123456789"
}
}
]
},
{
"id": 15,
"groupname": "React",
"contactsCount": 1,
"createdAt": "2017-12-15T15:06:45.000Z",
"updatedAt": "2017-12-15T15:06:45.000Z",
"contactgroups": [
{
"id": 3,
"contact": {
"id": 21,
"gsm": "123456789"
}
}
]
},
{
"id": 16,
"groupname": "Vue",
"contactsCount": 0,
"createdAt": "2017-12-15T15:06:51.000Z",
"updatedAt": "2017-12-15T15:06:51.000Z",
"contactgroups": []
},
{
"id": 17,
"groupname": "NodeJs",
"contactsCount": 0,
"createdAt": "2017-12-17T16:07:38.000Z",
"updatedAt": "2017-12-17T16:07:38.000Z",
"contactgroups": []
},
{
"id": 18,
"groupname": "RxJS",
"contactsCount": 0,
"createdAt": "2017-12-21T05:50:50.000Z",
"updatedAt": "2017-12-21T05:50:50.000Z",
"contactgroups": []
}
]
will anyone help me? i need single api code.
I tried nested query, and modified my first api code like this:
exports.getNewGroup = (req, res) => {
Group.findAll({
attributes: ['id', 'groupname', [sequelize.fn('COUNT', sequelize.col('contactgroups.groupId')), 'contactsCount']],
include: [{
model: ContactGroup,
attributes: ['id'],
include: [{
model: Contact,
attributes: ['id', 'gsm']
}]
}],
group: ['id']
}).then(data => {
return res.status(200).send(data);
}).catch(err => {
return res.status(400).send(err.message);
});
};
But i got this type of response not my requirement
[
{
"id": 14,
"groupname": "Angular",
"contactsCount": 2,
"contactgroups": [
{
"id": 1,
"contact": {
"id": 20,
"gsm": "987654321"
}
}
]
},
{
"id": 15,
"groupname": "React",
"contactsCount": 1,
"contactgroups": [
{
"id": 3,
"contact": {
"id": 21,
"gsm": "123456789"
}
}
]
},
{
"id": 16,
"groupname": "Vue",
"contactsCount": 0,
"contactgroups": []
},
{
"id": 17,
"groupname": "NodeJs",
"contactsCount": 0,
"contactgroups": []
},
{
"id": 18,
"groupname": "RxJS",
"contactsCount": 0,
"contactgroups": []
}
]