(
field?: Field<Entity, RootAlias, Context> | Field<Entity, RootAlias, Context>[],
distinct?: boolean,
)
| 2662 | */ |
| 2663 | async getCount<F extends Field<Entity, RootAlias, Context>>(field?: F | F[], distinct?: boolean): Promise<number>; |
| 2664 | async getCount( |
| 2665 | field?: Field<Entity, RootAlias, Context> | Field<Entity, RootAlias, Context>[], |
| 2666 | distinct?: boolean, |
| 2667 | ): Promise<number> { |
| 2668 | let res: { count: number }; |
| 2669 | |
| 2670 | if (this.type === QueryType.COUNT) { |
| 2671 | res = await this.execute<{ count: number }>('get', false); |
| 2672 | } else { |
| 2673 | const qb = (this.#state.type === undefined ? this : this.clone()) as unknown as QueryBuilder< |
| 2674 | Entity, |
| 2675 | RootAlias, |
| 2676 | Hint, |
| 2677 | Context |
| 2678 | >; |
| 2679 | qb.processPopulateHint(); // needs to happen sooner so `qb.hasToManyJoins()` reports correctly |
| 2680 | qb.count(field, distinct ?? qb.hasToManyJoins()) |
| 2681 | .limit(undefined) |
| 2682 | .offset(undefined) |
| 2683 | .orderBy([]); |
| 2684 | res = await qb.execute<{ count: number }>('get', false); |
| 2685 | } |
| 2686 | |
| 2687 | return res ? +res.count : 0; |
| 2688 | } |
| 2689 | |
| 2690 | /** |
| 2691 | * Executes the query, returning both array of results and total count query (without offset and limit). |
no test coverage detected