Sequelize js添加相关模型的总数

我试图修改一个查询,所以它也返回一个元素的数量,我想要统计与“出版物”有关的“出版物评论”的数量。 现在查询看起来像这样:

let query = { attributes: { exclude: attributesToExclude, }, where: { 'club_id': this.id, }, order: [['date', 'DESC']], include: [ { model: models.PublicationComment, association: models.Publication.hasMany(models.PublicationComment, { as: 'lastComments' }), order: [['date', 'DESC']], limit: 3, include: [ { model: models.User, as: 'user', attributes: { exclude: attributesToExclude, }, } ], }, { model: models.PublicationLike, association: models.Publication.hasMany(models.PublicationLike, { as: 'likes' }), attributes: ['userId'] } ] }; 

我试图获得的结果是这样的:

 [ { id: '2', clubId: '26', teamId: null, userId: '67', date: '2017-04-18T11:23:05.628Z', card: { type: 'status', text: 'publication example text', video: [Object] }, created_at: '2017-04-18T11:23:05.629Z', updated_at: '2017-04-18T11:23:05.629Z', deleted_at: null, likes: [], commentsCount: 4, lastComments: [ [Object], [Object], [Object] ] }] 

请注意有一个CommentCount属性与希望的计数查询的结果

我试着通过注释来添加包含属性的计数function,并重复与无限制参数(我需要总数)的PublicationComment模型的关联。 像这样的东西:

  attributes: { include: [[sequelize.fn('count', sequelize.col(allComments.id), 'commentsCount']] exclude: attributesToExclude, }, 

并在icludde数组中添加新的关联:

  { model: models.PublicationComment, association: models.Publication.hasMany(models.PublicationComment, { as: 'allComments' }) } 

但经过两天的努力浪费,我无法做到这一点。

有没有人有任何build议,资源或澄清,我可以用来推进? 谢谢。