From a41ce21a63d7f0b723a52d94e9265f3ca3a4f057 Mon Sep 17 00:00:00 2001 From: Appaji Date: Thu, 15 Aug 2024 16:09:27 +0530 Subject: [PATCH] Fix countBuilder --- packages/knex/src/adapter.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/packages/knex/src/adapter.ts b/packages/knex/src/adapter.ts index 8e6c6cb0b0..ca50fc7a3f 100644 --- a/packages/knex/src/adapter.ts +++ b/packages/knex/src/adapter.ts @@ -166,10 +166,11 @@ export class KnexAdapter< async _find(params?: ServiceParams): Promise | Result[]> async _find(params: ServiceParams = {} as ServiceParams): Promise | Result[]> { const { filters, paginate } = this.filterQuery(params) - const { name, id } = this.getOptions(params) + const { Model, name, id } = this.getOptions(params) const builder = params.knex ? params.knex.clone() : this.createQuery(params) - const countBuilder = builder.clone().clearSelect().clearOrder().count(`${name}.${id} as total`) - + const countBuilder = Model.count(`* as total`) + .with('subquery', builder.clone().clearOrder()) + .from('subquery') // Handle $limit if (filters.$limit) { builder.limit(filters.$limit)