I have formulated my query as follows...
{
where: {
id: event.pathParameters.picId
},
'include': [{
'model': db.reputations,
'where': {
'type': 'V_UP'
},
'as': 'up_reputations',
'required': false
}, {
'model': db.reputations,
'where': {
'type': 'V_DONW'
},
'as': 'down_reputations',
'required': false
}],
'attributes': [
'id',
'title',
'data',
'createdAt',
[db.Sequelize.fn('count', db.Sequelize.col('up_reputations.type')), 'upVotes'],
[db.Sequelize.fn('count', db.Sequelize.col('down_reputations.type')), 'downVotes']
]
}
What I am missing is an additional attribute called score
, which should be calculated as upVotes
minus downVotes
. However, I am unsure how to accomplish this.