【问题标题】:Get count from Objection.js query using withGraphJoined使用 withGraphJoined 从 Objection.js 查询中获取计数
【发布时间】:2020-06-05 16:34:15
【问题描述】:

我有一个非常简单的查询,我正在尝试获取计数。

const query = Product.query()
  .withGraphJoined('collections')
  .whereIn('collections.id', [1,3]);

我尝试了resultSize(),它可以处理我所做的其他未使用withGraphJoined 的查询。唯一有效的是,如果我对查询执行count(),它将返回以下内容:

Product {
  'count(*)': 6,
  id: 1,
  name: 'product',
  slug: 'slug',
  price: 32,
}

有没有更简洁的方法来对 withGraphJoined 的查询进行总计数?

【问题讨论】:

    标签: knex.js objection.js


    【解决方案1】:

    您正在尝试计算与产品关联的集合?你可以这样做:

    const query = Product.query()
      .whereIn('id', [1,3])
      .select([
         Product.ref('*'),
         Product.relatedQuery('collections').count().as('collectionsCount')
      ])
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-07-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-09
      相关资源
      最近更新 更多