MCPcopy Create free account
hub / github.com/mikro-orm/mikro-orm / getCount

Method getCount

packages/sql/src/query/QueryBuilder.ts:2664–2688  ·  view source on GitHub ↗
(
    field?: Field<Entity, RootAlias, Context> | Field<Entity, RootAlias, Context>[],
    distinct?: boolean,
  )

Source from the content-addressed store, hash-verified

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).

Calls 7

cloneMethod · 0.95
processPopulateHintMethod · 0.80
hasToManyJoinsMethod · 0.80
orderByMethod · 0.65
countMethod · 0.65
offsetMethod · 0.45
limitMethod · 0.45

Tested by

no test coverage detected