【问题标题】:Is there some way of put count in "with" clause of Query builder?有没有办法在查询构建器的“with”子句中添加计数?
【发布时间】:2020-07-14 18:16:29
【问题描述】:

我在 AdonisJS 中复制了一个 twitter,我得到了这样的“推文”:

const tweets = await Post.query()
  .whereIn('user_id', followersIds)
  .with('user')
  .with('favorites')
  .with('replies')
  .orderBy('created_at', 'DESC')
  .paginate(params.page, 3)

问题是,在这种方法中,我得到所有回复表的推文,我只需要推文计数或replies.lenght

我想为此使用带扣,但可能存在另一种方式 4 在查询生成器中实现...

感谢您的帮助。

【问题讨论】:

    标签: javascript node.js query-builder knex.js adonis.js


    【解决方案1】:

    你可以使用.withCount()

    尝试:

    const tweets = await Post.query()
      .whereIn('user_id', followersIds)
      .with('user')
      .with('favorites')
      .withCount('replies')
      .orderBy('created_at', 'DESC')
      .paginate(params.page, 3)
    

    您应该会看到一个名为 replies_count 的属性

    https://adonisjs.com/docs/4.1/relationships#_counts

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-05
      • 2021-09-13
      • 1970-01-01
      • 2021-12-14
      • 2018-12-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多